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

Last change on this file since 13920 was 13812, checked in by Don-vip, 6 years ago

https access to OSM Dev API

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.junit.Assert.assertNull;
5import static org.junit.Assert.assertTrue;
6import static org.junit.Assert.fail;
7
8import java.io.File;
9import java.io.IOException;
10import java.nio.file.Paths;
11import java.security.GeneralSecurityException;
12import java.text.MessageFormat;
13import java.util.Locale;
14import java.util.TimeZone;
15
16import org.openstreetmap.josm.actions.DeleteAction;
17import org.openstreetmap.josm.command.DeleteCommand;
18import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
19import org.openstreetmap.josm.data.projection.Projections;
20import org.openstreetmap.josm.gui.MainApplication;
21import org.openstreetmap.josm.gui.MainApplicationTest;
22import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
23import org.openstreetmap.josm.gui.util.GuiHelper;
24import org.openstreetmap.josm.io.CertificateAmendment;
25import org.openstreetmap.josm.io.OsmApi;
26import org.openstreetmap.josm.spi.preferences.Config;
27import org.openstreetmap.josm.testutils.JOSMTestRules;
28import org.openstreetmap.josm.tools.I18n;
29import org.openstreetmap.josm.tools.JosmRuntimeException;
30import org.openstreetmap.josm.tools.Logging;
31
32/**
33 * Fixture to define a proper and safe environment before running tests.
34 */
35public class JOSMFixture {
36
37 /**
38 * Returns a new test fixture initialized to "unit" home.
39 * @return A new test fixture for unit tests
40 */
41 public static JOSMFixture createUnitTestFixture() {
42 return new JOSMFixture("test/config/unit-josm.home");
43 }
44
45 /**
46 * Returns a new test fixture initialized to "functional" home.
47 * @return A new test fixture for functional tests
48 */
49 public static JOSMFixture createFunctionalTestFixture() {
50 return new JOSMFixture("test/config/functional-josm.home");
51 }
52
53 /**
54 * Returns a new test fixture initialized to "performance" home.
55 * @return A new test fixture for performance tests
56 */
57 public static JOSMFixture createPerformanceTestFixture() {
58 return new JOSMFixture("test/config/performance-josm.home");
59 }
60
61 private final String josmHome;
62
63 /**
64 * Constructs a new text fixture initialized to a given josm home.
65 * @param josmHome The user home where preferences are to be read/written
66 */
67 public JOSMFixture(String josmHome) {
68 this.josmHome = josmHome;
69 }
70
71 /**
72 * Initializes the test fixture, without GUI.
73 */
74 public void init() {
75 init(false);
76 }
77
78 /**
79 * Initializes the test fixture, with or without GUI.
80 * @param createGui if {@code true} creates main GUI components
81 */
82 public void init(boolean createGui) {
83
84 // check josm.home
85 //
86 if (josmHome == null) {
87 fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
88 } else {
89 File f = new File(josmHome);
90 if (!f.exists() || !f.canRead()) {
91 fail(MessageFormat.format(
92 // CHECKSTYLE.OFF: LineLength
93 "property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
94 // CHECKSTYLE.ON: LineLength
95 "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
96 }
97 }
98 System.setProperty("josm.home", josmHome);
99 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
100 Config.setPreferencesInstance(Main.pref);
101 Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
102 Main.pref.resetToInitialState();
103 Main.pref.enableSaveOnPut(false);
104 I18n.init();
105 // initialize the plaform hook, and
106 Main.determinePlatformHook();
107 // call the really early hook before we anything else
108 Main.platform.preStartupHook();
109
110 Logging.setLogLevel(Logging.LEVEL_INFO);
111 Main.pref.init(false);
112 String url = Config.getPref().get("osm-server.url");
113 if (url == null || url.isEmpty() || isProductionApiUrl(url)) {
114 Config.getPref().put("osm-server.url", "https://api06.dev.openstreetmap.org/api");
115 }
116 I18n.set(Config.getPref().get("language", "en"));
117
118 try {
119 CertificateAmendment.addMissingCertificates();
120 } catch (IOException | GeneralSecurityException ex) {
121 throw new JosmRuntimeException(ex);
122 }
123
124 // init projection
125 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
126
127 // setup projection grid files
128 MainApplication.setupNadGridSources();
129
130 // make sure we don't upload to or test against production
131 url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
132 if (isProductionApiUrl(url)) {
133 fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
134 }
135
136 // Setup callbacks
137 DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
138
139 if (createGui) {
140 GuiHelper.runInEDTAndWaitWithException(() -> setupGUI());
141 }
142 }
143
144 private static boolean isProductionApiUrl(String url) {
145 return url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
146 || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org");
147 }
148
149 @SuppressWarnings("deprecation")
150 private void setupGUI() {
151 JOSMTestRules.cleanLayerEnvironment();
152 assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
153 assertNull(MainApplication.getLayerManager().getEditLayer());
154 assertNull(MainApplication.getLayerManager().getActiveLayer());
155
156 initContentPane();
157 initMainPanel(false);
158 initToolbar();
159 if (Main.main == null) {
160 new MainApplication().initialize();
161 }
162 // Add a test layer to the layer manager to get the MapFrame
163 MainApplication.getLayerManager().addLayer(new TestLayer());
164 }
165
166 /**
167 * Make sure {@code MainApplication.contentPanePrivate} is initialized.
168 */
169 public static void initContentPane() {
170 MainApplicationTest.initContentPane();
171 }
172
173 /**
174 * Make sure {@code MainApplication.mainPanel} is initialized.
175 */
176 public static void initMainPanel() {
177 initMainPanel(false);
178 }
179
180 /**
181 * Make sure {@code MainApplication.mainPanel} is initialized.
182 * @param reAddListeners {@code true} to re-add listeners
183 */
184 public static void initMainPanel(boolean reAddListeners) {
185 MainApplicationTest.initMainPanel(reAddListeners);
186 }
187
188 /**
189 * Make sure {@code MainApplication.toolbar} is initialized.
190 */
191 public static void initToolbar() {
192 MainApplicationTest.initToolbar();
193 }
194
195 /**
196 * Make sure {@code MainApplication.menu} is initialized.
197 */
198 public static void initMainMenu() {
199 MainApplicationTest.initMainMenu();
200 }
201}
Note: See TracBrowser for help on using the repository browser.