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

Last change on this file since 8193 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

  • Property svn:eol-style set to native
File size: 3.8 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.gui.MainApplication;
12import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
13import org.openstreetmap.josm.io.OsmApi;
14import org.openstreetmap.josm.tools.I18n;
15
16/**
17 * Fixture to define a proper and safe environment before running tests.
18 */
19public class JOSMFixture {
20
21 /**
22 * Returns a new test fixture initialized to "unit" home.
23 * @return A new test fixture for unit tests
24 */
25 static public JOSMFixture createUnitTestFixture() {
26 return new JOSMFixture("test/config/unit-josm.home");
27 }
28
29 /**
30 * Returns a new test fixture initialized to "functional" home.
31 * @return A new test fixture for functional tests
32 */
33 static public JOSMFixture createFunctionalTestFixture() {
34 return new JOSMFixture("test/config/functional-josm.home");
35 }
36
37 /**
38 * Returns a new test fixture initialized to "performance" home.
39 * @return A new test fixture for performance tests
40 */
41 static public JOSMFixture createPerformanceTestFixture() {
42 return new JOSMFixture("test/config/performance-josm.home");
43 }
44
45 private final String josmHome;
46
47 /**
48 * Constructs a new text fixture initialized to a given josm home.
49 * @param josmHome The user home where preferences are to be read/written
50 */
51 public JOSMFixture(String josmHome) {
52 this.josmHome = josmHome;
53 }
54
55 /**
56 * Initializes the test fixture, without GUI.
57 */
58 public void init() {
59 init(false);
60 }
61
62 /**
63 * Initializes the test fixture, with or without GUI.
64 * @param createGui if {@code true} creates main GUI components
65 */
66 public void init(boolean createGui) {
67
68 // check josm.home
69 //
70 if (josmHome == null) {
71 fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
72 } else {
73 File f = new File(josmHome);
74 if (! f.exists() || ! f.canRead()) {
75 fail(MessageFormat.format("property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
76 "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
77 }
78 }
79 System.setProperty("josm.home", josmHome);
80 Main.initApplicationPreferences();
81 Main.pref.enableSaveOnPut(false);
82 I18n.init();
83 // initialize the plaform hook, and
84 Main.determinePlatformHook();
85 // call the really early hook before we anything else
86 Main.platform.preStartupHook();
87
88 Main.pref.init(false);
89 I18n.set(Main.pref.get("language", "en"));
90
91 // init projection
92 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
93
94 // make sure we don't upload to or test against production
95 //
96 String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase().trim();
97 if (url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
98 || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org")) {
99 fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
100 }
101
102 if (createGui) {
103 if (Main.toolbar == null) {
104 Main.toolbar = new ToolbarPreferences();
105 }
106 if (Main.main == null) {
107 new MainApplication();
108 }
109 if (Main.map == null) {
110 Main.main.createMapFrame(null, null);
111 }
112 }
113 }
114}
Note: See TracBrowser for help on using the repository browser.