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

Last change on this file since 12747 was 12643, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.main.menu. Replacement: gui.MainApplication.getMenu()

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