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