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

Last change on this file since 14100 was 14079, checked in by Don-vip, 6 years ago

see #16550 - use UTC by default for GPX timestamps

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