source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java@ 10432

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

fix #13001 - Add MainPanel + some new methods (patch by michael2402, modified) - gsoc-core

File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.testutils;
3
4import java.io.File;
5import java.io.IOException;
6
7import org.junit.rules.TemporaryFolder;
8import org.junit.rules.TestRule;
9import org.junit.rules.Timeout;
10import org.junit.runner.Description;
11import org.junit.runners.model.InitializationError;
12import org.junit.runners.model.Statement;
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.projection.Projections;
15import org.openstreetmap.josm.gui.util.GuiHelper;
16import org.openstreetmap.josm.io.OsmApi;
17import org.openstreetmap.josm.io.OsmApiInitializationException;
18import org.openstreetmap.josm.io.OsmTransferCanceledException;
19import org.openstreetmap.josm.tools.I18n;
20
21/**
22 * This class runs a test in an environment that resembles the one used by the JOSM main application.
23 * <p>
24 * The environment is reset before every test. You can specify the components to which you need access using the methods of this class.
25 * For example, invoking {@link #preferences()} gives you access to the (default) preferences.
26 *
27 * @author Michael Zangl
28 */
29public class JOSMTestRules implements TestRule {
30 //We should make this the default when running from ant: Timeout.seconds(10);
31 private Timeout timeout = null;
32 private TemporaryFolder josmHome;
33 private boolean usePreferences = false;
34 private APIType useAPI = APIType.NONE;
35 private String i18n = null;
36 private boolean platform;
37 private boolean useProjection;
38
39 /**
40 * Disable the default timeout for this test. Use with care.
41 * @return this instance, for easy chaining
42 */
43 public JOSMTestRules noTimeout() {
44 timeout = null;
45 return this;
46 }
47
48 /**
49 * Set a timeout for all tests in this class. Local method timeouts may only reduce this timeout.
50 * @param millis The timeout duration in milliseconds.
51 * @return this instance, for easy chaining
52 */
53 public JOSMTestRules timeout(int millis) {
54 timeout = Timeout.millis(millis);
55 return this;
56 }
57
58 /**
59 * Enable the use of default preferences.
60 * @return this instance, for easy chaining
61 */
62 public JOSMTestRules preferences() {
63 josmHome();
64 usePreferences = true;
65 return this;
66 }
67
68 /**
69 * Set JOSM home to a valid, empty directory.
70 * @return this instance, for easy chaining
71 */
72 private JOSMTestRules josmHome() {
73 josmHome = new TemporaryFolder();
74 return this;
75 }
76
77 /**
78 * Enables the i18n module for this test in english.
79 * @return this instance, for easy chaining
80 */
81 public JOSMTestRules i18n() {
82 return i18n("en");
83 }
84
85 /**
86 * Enables the i18n module for this test.
87 * @param language The language to use.
88 * @return this instance, for easy chaining
89 */
90 public JOSMTestRules i18n(String language) {
91 i18n = language;
92 return this;
93 }
94
95 /**
96 * Enable {@link Main#platform} global variable.
97 * @return this instance, for easy chaining
98 */
99 public JOSMTestRules platform() {
100 platform = true;
101 return this;
102 }
103
104 /**
105 * Enable the dev.openstreetmap.org API for this test.
106 * @return this instance, for easy chaining
107 */
108 public JOSMTestRules devAPI() {
109 preferences();
110 useAPI = APIType.DEV;
111 return this;
112 }
113
114 /**
115 * Use the {@link FakeOsmApi} for testing.
116 * @return this instance, for easy chaining
117 */
118 public JOSMTestRules fakeAPI() {
119 useAPI = APIType.FAKE;
120 return this;
121 }
122
123 /**
124 * Set up default projection (Mercator)
125 * @return this instance, for easy chaining
126 */
127 public JOSMTestRules projection() {
128 useProjection = true;
129 return this;
130 }
131
132 @Override
133 public Statement apply(final Statement base, Description description) {
134 Statement statement = new Statement() {
135 @Override
136 public void evaluate() throws Throwable {
137 before();
138 try {
139 base.evaluate();
140 } finally {
141 after();
142 }
143 }
144 };
145 if (timeout != null) {
146 statement = timeout.apply(statement, description);
147 }
148 if (josmHome != null) {
149 statement = josmHome.apply(statement, description);
150 }
151 return statement;
152 }
153
154 /**
155 * Set up before running a test
156 * @throws InitializationError If an error occured while creating the required environment.
157 */
158 protected void before() throws InitializationError {
159 // Tests are running headless by default.
160 System.setProperty("java.awt.headless", "true");
161
162 // Set up i18n
163 if (i18n != null) {
164 I18n.set(i18n);
165 }
166
167 // Add JOSM home
168 if (josmHome != null) {
169 try {
170 File home = josmHome.newFolder();
171 System.setProperty("josm.home", home.getAbsolutePath());
172 } catch (IOException e) {
173 throw new InitializationError(e);
174 }
175 }
176
177 // Add preferences
178 if (usePreferences) {
179 Main.initApplicationPreferences();
180 Main.pref.enableSaveOnPut(false);
181 // No pref init -> that would only create the preferences file.
182 // We force the use of a wrong API server, just in case anyone attempts an upload
183 Main.pref.put("osm-server.url", "http://invalid");
184 }
185
186 if (useProjection) {
187 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
188 }
189
190 // Set API
191 if (useAPI == APIType.DEV) {
192 Main.pref.put("osm-server.url", "http://api06.dev.openstreetmap.org/api");
193 } else if (useAPI == APIType.FAKE) {
194 FakeOsmApi api = FakeOsmApi.getInstance();
195 Main.pref.put("osm-server.url", api.getServerUrl());
196 }
197
198 // Initialize API
199 if (useAPI != APIType.NONE) {
200 try {
201 OsmApi.getOsmApi().initialize(null);
202 } catch (OsmTransferCanceledException | OsmApiInitializationException e) {
203 throw new InitializationError(e);
204 }
205 }
206
207 // Set Platform
208 if (platform) {
209 Main.determinePlatformHook();
210 }
211 }
212
213 /**
214 * Clean up after running a test
215 */
216 protected void after() {
217 // Sync AWT Thread
218 GuiHelper.runInEDTAndWait(new Runnable() {
219 @Override
220 public void run() {
221 }
222 });
223 // Remove all layers
224 Main.getLayerManager().resetState();
225
226 // TODO: Remove global listeners and other global state.
227 Main.pref = null;
228 Main.platform = null;
229 // Parts of JOSM uses weak references - destroy them.
230 System.gc();
231 }
232
233 enum APIType {
234 NONE, FAKE, DEV
235 }
236}
Note: See TracBrowser for help on using the repository browser.