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

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

add more info in case of test fixture creation fails

File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.junit.Assert.fail;
5
6import java.io.File;
7import java.nio.file.Paths;
8import java.text.MessageFormat;
9
10import org.openstreetmap.josm.data.projection.Projections;
11import org.openstreetmap.josm.io.OsmApi;
12import org.openstreetmap.josm.tools.I18n;
13
14/**
15 * Fixture to define a proper and safe environment before running tests.
16 */
17public class JOSMFixture {
18
19 /**
20 * Returns a new test fixture initialized to "unit" home.
21 * @return A new test fixture for unit tests
22 */
23 static public JOSMFixture createUnitTestFixture() {
24 return new JOSMFixture("test/config/unit-josm.home");
25 }
26
27 /**
28 * Returns a new test fixture initialized to "functional" home.
29 * @return A new test fixture for functional tests
30 */
31 static public JOSMFixture createFunctionalTestFixture() {
32 return new JOSMFixture("test/config/functional-josm.home");
33 }
34
35 /**
36 * Returns a new test fixture initialized to "performance" home.
37 * @return A new test fixture for performance tests
38 */
39 static public JOSMFixture createPerformanceTestFixture() {
40 return new JOSMFixture("test/config/performance-josm.home");
41 }
42
43 private final String josmHome;
44
45 /**
46 * Constructs a new text fixture initialized to a given josm home.
47 * @param josmHome The user home where preferences are to be read/written
48 */
49 public JOSMFixture(String josmHome) {
50 this.josmHome = josmHome;
51 }
52
53 /**
54 * Initializes the test fixture.
55 */
56 public void init() {
57
58 // check josm.home
59 //
60 if (josmHome == null) {
61 fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
62 } else {
63 File f = new File(josmHome);
64 if (! f.exists() || ! f.canRead()) {
65 fail(MessageFormat.format("property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
66 "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
67 }
68 }
69 System.setProperty("josm.home", josmHome);
70 Main.initApplicationPreferences();
71 Main.pref.enableSaveOnPut(false);
72 I18n.init();
73 // initialize the plaform hook, and
74 Main.determinePlatformHook();
75 // call the really early hook before we anything else
76 Main.platform.preStartupHook();
77
78 Main.pref.init(false);
79 I18n.set(Main.pref.get("language", "en"));
80
81 // init projection
82 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
83
84 // make sure we don't upload to or test against production
85 //
86 String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase().trim();
87 if (url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
88 || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org")) {
89 fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
90 }
91 }
92}
Note: See TracBrowser for help on using the repository browser.