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

Last change on this file since 14120 was 14120, checked in by Don-vip, 8 years ago

see #15229 - deprecate all Main methods related to projections. New ProjectionRegistry class

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