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

Last change on this file since 9862 was 9801, checked in by stoecker, 8 years ago

fix SessionReaderTest

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