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

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

see #12974 - Unit tests hang (patch by michael2402) - gsoc-core

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