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