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

Last change on this file since 9504 was 8540, checked in by Don-vip, 9 years ago

fix remaining checkstyle issues

  • Property svn:eol-style set to native
File size: 4.0 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 public static 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 public static 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 public static 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(
76 // CHECKSTYLE.OFF: LineLength
77 "property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
78 // CHECKSTYLE.ON: LineLength
79 "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
80 }
81 }
82 System.setProperty("josm.home", josmHome);
83 Main.initApplicationPreferences();
84 Main.pref.enableSaveOnPut(false);
85 I18n.init();
86 // initialize the plaform hook, and
87 Main.determinePlatformHook();
88 // call the really early hook before we anything else
89 Main.platform.preStartupHook();
90
91 Main.pref.init(false);
92 I18n.set(Main.pref.get("language", "en"));
93
94 // init projection
95 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
96
97 // make sure we don't upload to or test against production
98 //
99 String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase().trim();
100 if (url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
101 || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org")) {
102 fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
103 }
104
105 if (createGui) {
106 if (Main.toolbar == null) {
107 Main.toolbar = new ToolbarPreferences();
108 }
109 if (Main.main == null) {
110 new MainApplication();
111 }
112 if (Main.map == null) {
113 Main.main.createMapFrame(null, null);
114 }
115 }
116 }
117}
Note: See TracBrowser for help on using the repository browser.