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

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

see #15182 - deprecate shortcut handling and mapframe listener methods in Main. Replacement: same methods in gui.MainApplication

  • Property svn:eol-style set to native
File size: 6.6 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 private void setupGUI() {
143 JOSMTestRules.cleanLayerEnvironment();
144 assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
145 assertNull(MainApplication.getLayerManager().getEditLayer());
146 assertNull(MainApplication.getLayerManager().getActiveLayer());
147
148 initContentPane();
149 initMainPanel(false);
150 initToolbar();
151 if (Main.main == null) {
152 new MainApplication().initialize();
153 } else {
154 if (Main.main.panel == null) {
155 initMainPanel(false);
156 Main.main.panel = MainApplicationTest.getMainPanel();
157 }
158 Main.main.panel.reAddListeners();
159 }
160 // Add a test layer to the layer manager to get the MapFrame
161 MainApplication.getLayerManager().addLayer(new TestLayer());
162 }
163
164 /**
165 * Make sure {@code MainApplication.contentPanePrivate} is initialized.
166 */
167 public static void initContentPane() {
168 MainApplicationTest.initContentPane();
169 }
170
171 /**
172 * Make sure {@code MainApplication.mainPanel} is initialized.
173 */
174 public static void initMainPanel() {
175 initMainPanel(false);
176 }
177
178 /**
179 * Make sure {@code MainApplication.mainPanel} is initialized.
180 * @param reAddListeners {@code true} to re-add listeners
181 */
182 public static void initMainPanel(boolean reAddListeners) {
183 MainApplicationTest.initMainPanel(reAddListeners);
184 }
185
186 /**
187 * Make sure {@code MainApplication.toolbar} is initialized.
188 */
189 public static void initToolbar() {
190 MainApplicationTest.initToolbar();
191 }
192}
Note: See TracBrowser for help on using the repository browser.