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

Last change on this file since 10373 was 10364, checked in by stoecker, 8 years ago

gsoc-core - patch by Michael Zangl - see #12953 - remove deprecation usage

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