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

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

fix #14649 - load Dutch Government (G2 & G3) certificates from Windows keystore if not found in Java keystore

  • Property svn:eol-style set to native
File size: 5.6 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.data.projection.Projections;
17import org.openstreetmap.josm.gui.MainApplication;
18import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
19import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
20import org.openstreetmap.josm.gui.util.GuiHelper;
21import org.openstreetmap.josm.io.CertificateAmendment;
22import org.openstreetmap.josm.io.OsmApi;
23import org.openstreetmap.josm.tools.I18n;
24import org.openstreetmap.josm.tools.JosmRuntimeException;
25import org.openstreetmap.josm.tools.Logging;
26
27/**
28 * Fixture to define a proper and safe environment before running tests.
29 */
30public 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}
Note: See TracBrowser for help on using the repository browser.