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

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

fix #13102 - Tests requiring Main.main.map fail (patch by michael2402) - gsoc-core

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