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

Last change on this file since 12795 was 12795, checked in by bastiK, 7 years ago

see #15273, see #15229 - fix unit tests, PMD, etc.

  • Property svn:eol-style set to native
File size: 7.1 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.projection.Projections;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.gui.MainApplicationTest;
21import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
22import org.openstreetmap.josm.gui.util.GuiHelper;
23import org.openstreetmap.josm.io.CertificateAmendment;
24import org.openstreetmap.josm.io.OsmApi;
25import org.openstreetmap.josm.testutils.JOSMTestRules;
26import org.openstreetmap.josm.tools.I18n;
27import org.openstreetmap.josm.tools.JosmRuntimeException;
28import org.openstreetmap.josm.tools.Logging;
29
30/**
31 * Fixture to define a proper and safe environment before running tests.
32 */
33public 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}
Note: See TracBrowser for help on using the repository browser.