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