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

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

see #15229 - see #15182 - see #13036 - remove more GUI stuff from DeleteCommand

  • 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.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 // make sure we don't upload to or test against production
124 url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
125 if (isProductionApiUrl(url)) {
126 fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
127 }
128
129 // Setup callbacks
130 DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
131
132 if (createGui) {
133 GuiHelper.runInEDTAndWaitWithException(new Runnable() {
134 @Override
135 public void run() {
136 setupGUI();
137 }
138 });
139 }
140 }
141
142 private static boolean isProductionApiUrl(String url) {
143 return url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
144 || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org");
145 }
146
147 @SuppressWarnings("deprecation")
148 private void setupGUI() {
149 JOSMTestRules.cleanLayerEnvironment();
150 assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
151 assertNull(MainApplication.getLayerManager().getEditLayer());
152 assertNull(MainApplication.getLayerManager().getActiveLayer());
153
154 initContentPane();
155 initMainPanel(false);
156 initToolbar();
157 if (Main.main == null) {
158 new MainApplication().initialize();
159 } else {
160 if (Main.main.panel == null) {
161 initMainPanel(false);
162 Main.main.panel = MainApplication.getMainPanel();
163 }
164 Main.main.panel.reAddListeners();
165 }
166 // Add a test layer to the layer manager to get the MapFrame
167 MainApplication.getLayerManager().addLayer(new TestLayer());
168 }
169
170 /**
171 * Make sure {@code MainApplication.contentPanePrivate} is initialized.
172 */
173 public static void initContentPane() {
174 MainApplicationTest.initContentPane();
175 }
176
177 /**
178 * Make sure {@code MainApplication.mainPanel} is initialized.
179 */
180 public static void initMainPanel() {
181 initMainPanel(false);
182 }
183
184 /**
185 * Make sure {@code MainApplication.mainPanel} is initialized.
186 * @param reAddListeners {@code true} to re-add listeners
187 */
188 public static void initMainPanel(boolean reAddListeners) {
189 MainApplicationTest.initMainPanel(reAddListeners);
190 }
191
192 /**
193 * Make sure {@code MainApplication.toolbar} is initialized.
194 */
195 public static void initToolbar() {
196 MainApplicationTest.initToolbar();
197 }
198
199 /**
200 * Make sure {@code MainApplication.menu} is initialized.
201 */
202 public static void initMainMenu() {
203 MainApplicationTest.initMainMenu();
204 }
205}
Note: See TracBrowser for help on using the repository browser.