source: josm/trunk/test/unit/org/openstreetmap/josm/JOSMFixture.java@ 12859

Last change on this file since 12859 was 12855, checked in by bastiK, 8 years ago

see #15229 - add separate interface IBaseDirectories to look up pref, user data and cache dir

  • Property svn:eol-style set to native
File size: 7.3 KB
RevLine 
[7937]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
[10443]4import static org.junit.Assert.assertNull;
5import static org.junit.Assert.assertTrue;
[7937]6import static org.junit.Assert.fail;
7
8import java.io.File;
[10019]9import java.io.IOException;
[7937]10import java.nio.file.Paths;
[10235]11import java.security.GeneralSecurityException;
[7937]12import java.text.MessageFormat;
[10222]13import java.util.Locale;
[10467]14import java.util.TimeZone;
[7937]15
[12757]16import org.openstreetmap.josm.actions.DeleteAction;
17import org.openstreetmap.josm.command.DeleteCommand;
[7937]18import org.openstreetmap.josm.data.projection.Projections;
19import org.openstreetmap.josm.gui.MainApplication;
[12637]20import org.openstreetmap.josm.gui.MainApplicationTest;
[10467]21import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
[10400]22import org.openstreetmap.josm.gui.util.GuiHelper;
[10019]23import org.openstreetmap.josm.io.CertificateAmendment;
[7937]24import org.openstreetmap.josm.io.OsmApi;
[12848]25import org.openstreetmap.josm.spi.preferences.Config;
[12070]26import org.openstreetmap.josm.testutils.JOSMTestRules;
[7937]27import org.openstreetmap.josm.tools.I18n;
[11943]28import org.openstreetmap.josm.tools.JosmRuntimeException;
[10899]29import org.openstreetmap.josm.tools.Logging;
[7937]30
31/**
32 * Fixture to define a proper and safe environment before running tests.
33 */
34public class JOSMFixture {
35
36 /**
37 * Returns a new test fixture initialized to "unit" home.
38 * @return A new test fixture for unit tests
39 */
[8511]40 public static JOSMFixture createUnitTestFixture() {
[7937]41 return new JOSMFixture("test/config/unit-josm.home");
42 }
43
44 /**
45 * Returns a new test fixture initialized to "functional" home.
46 * @return A new test fixture for functional tests
47 */
[8511]48 public static JOSMFixture createFunctionalTestFixture() {
[7937]49 return new JOSMFixture("test/config/functional-josm.home");
50 }
51
52 /**
53 * Returns a new test fixture initialized to "performance" home.
54 * @return A new test fixture for performance tests
55 */
[8511]56 public static JOSMFixture createPerformanceTestFixture() {
[7937]57 return new JOSMFixture("test/config/performance-josm.home");
58 }
59
60 private final String josmHome;
61
62 /**
63 * Constructs a new text fixture initialized to a given josm home.
64 * @param josmHome The user home where preferences are to be read/written
65 */
66 public JOSMFixture(String josmHome) {
67 this.josmHome = josmHome;
68 }
69
70 /**
71 * Initializes the test fixture, without GUI.
72 */
73 public void init() {
74 init(false);
75 }
76
77 /**
78 * Initializes the test fixture, with or without GUI.
79 * @param createGui if {@code true} creates main GUI components
80 */
81 public void init(boolean createGui) {
82
83 // check josm.home
84 //
85 if (josmHome == null) {
86 fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
87 } else {
88 File f = new File(josmHome);
[8510]89 if (!f.exists() || !f.canRead()) {
[8509]90 fail(MessageFormat.format(
[8540]91 // CHECKSTYLE.OFF: LineLength
[8509]92 "property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
[8540]93 // CHECKSTYLE.ON: LineLength
[7937]94 "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
95 }
96 }
[3988]97 System.setProperty("josm.home", josmHome);
[10467]98 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
[12848]99 Config.setPreferencesInstance(Main.pref);
[12855]100 Config.setBaseDirectoriesProvider(Main.pref);
[10876]101 Main.pref.resetToInitialState();
[7937]102 Main.pref.enableSaveOnPut(false);
103 I18n.init();
104 // initialize the plaform hook, and
105 Main.determinePlatformHook();
106 // call the really early hook before we anything else
107 Main.platform.preStartupHook();
108
[10899]109 Logging.setLogLevel(Logging.LEVEL_INFO);
[7937]110 Main.pref.init(false);
[12849]111 String url = Config.getPref().get("osm-server.url");
[10850]112 if (url == null || url.isEmpty() || isProductionApiUrl(url)) {
[12849]113 Config.getPref().put("osm-server.url", "http://api06.dev.openstreetmap.org/api");
[10850]114 }
[12849]115 I18n.set(Config.getPref().get("language", "en"));
[7937]116
[10019]117 try {
118 CertificateAmendment.addMissingCertificates();
[10235]119 } catch (IOException | GeneralSecurityException ex) {
[11943]120 throw new JosmRuntimeException(ex);
[10019]121 }
122
[7937]123 // init projection
124 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
125
[12795]126 // setup projection grid files
127 MainApplication.setupNadGridSources();
128
[7937]129 // make sure we don't upload to or test against production
[10850]130 url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
131 if (isProductionApiUrl(url)) {
[7937]132 fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
133 }
134
[12757]135 // Setup callbacks
[12760]136 DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
[12757]137
[7937]138 if (createGui) {
[10400]139 GuiHelper.runInEDTAndWaitWithException(new Runnable() {
140 @Override
141 public void run() {
142 setupGUI();
[9801]143 }
[10400]144 });
145 }
146 }
147
[10850]148 private static boolean isProductionApiUrl(String url) {
149 return url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
150 || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org");
151 }
152
[12642]153 @SuppressWarnings("deprecation")
[10400]154 private void setupGUI() {
[12070]155 JOSMTestRules.cleanLayerEnvironment();
[12636]156 assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
157 assertNull(MainApplication.getLayerManager().getEditLayer());
158 assertNull(MainApplication.getLayerManager().getActiveLayer());
[10467]159
[12221]160 initContentPane();
[12563]161 initMainPanel(false);
[12221]162 initToolbar();
163 if (Main.main == null) {
164 new MainApplication().initialize();
165 } else {
[12559]166 if (Main.main.panel == null) {
[12563]167 initMainPanel(false);
[12642]168 Main.main.panel = MainApplication.getMainPanel();
[12559]169 }
[12221]170 Main.main.panel.reAddListeners();
171 }
172 // Add a test layer to the layer manager to get the MapFrame
[12636]173 MainApplication.getLayerManager().addLayer(new TestLayer());
[12221]174 }
175
176 /**
[12639]177 * Make sure {@code MainApplication.contentPanePrivate} is initialized.
[12221]178 */
179 public static void initContentPane() {
[12639]180 MainApplicationTest.initContentPane();
[12221]181 }
182
183 /**
[12639]184 * Make sure {@code MainApplication.mainPanel} is initialized.
[12569]185 */
186 public static void initMainPanel() {
187 initMainPanel(false);
188 }
189
190 /**
[12639]191 * Make sure {@code MainApplication.mainPanel} is initialized.
[12563]192 * @param reAddListeners {@code true} to re-add listeners
[12221]193 */
[12563]194 public static void initMainPanel(boolean reAddListeners) {
[12639]195 MainApplicationTest.initMainPanel(reAddListeners);
[12221]196 }
197
198 /**
[12639]199 * Make sure {@code MainApplication.toolbar} is initialized.
[12221]200 */
201 public static void initToolbar() {
[12637]202 MainApplicationTest.initToolbar();
[7937]203 }
[12643]204
205 /**
206 * Make sure {@code MainApplication.menu} is initialized.
207 */
208 public static void initMainMenu() {
209 MainApplicationTest.initMainMenu();
210 }
[7937]211}
Note: See TracBrowser for help on using the repository browser.