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

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

findbugs - fix/suppress most of warnings reported in unit tests + enable low confidence warnings for core

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