1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.fixtures;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.fail;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.text.MessageFormat;
|
---|
8 | import java.util.Properties;
|
---|
9 | import java.util.logging.Level;
|
---|
10 | import java.util.logging.Logger;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.Preferences;
|
---|
14 | import org.openstreetmap.josm.data.projection.Mercator;
|
---|
15 | import org.openstreetmap.josm.io.OsmApi;
|
---|
16 | import org.openstreetmap.josm.tools.I18n;
|
---|
17 |
|
---|
18 | public class JOSMFixture {
|
---|
19 | static private final Logger logger = Logger.getLogger(JOSMFixture.class.getName());
|
---|
20 |
|
---|
21 | static public JOSMFixture createUnitTestFixture() {
|
---|
22 | return new JOSMFixture("/test-unit-env.properties");
|
---|
23 | }
|
---|
24 |
|
---|
25 | static public JOSMFixture createFunctionalTestFixture() {
|
---|
26 | return new JOSMFixture("/test-functional-env.properties");
|
---|
27 | }
|
---|
28 |
|
---|
29 | private Properties testProperties;
|
---|
30 | private String testPropertiesResourceName;
|
---|
31 |
|
---|
32 | public JOSMFixture(String testPropertiesResourceName) {
|
---|
33 | this.testPropertiesResourceName = testPropertiesResourceName;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void init() {
|
---|
37 | testProperties = new Properties();
|
---|
38 |
|
---|
39 | // load properties
|
---|
40 | //
|
---|
41 | try {
|
---|
42 | testProperties.load(JOSMFixture.class.getResourceAsStream(testPropertiesResourceName));
|
---|
43 | } catch(Exception e){
|
---|
44 | logger.log(Level.SEVERE, MessageFormat.format("failed to load property file ''{0}''", testPropertiesResourceName));
|
---|
45 | fail(MessageFormat.format("failed to load property file ''{0}''. \nMake sure the path ''$project_root/test/config'' is on the classpath.", testPropertiesResourceName));
|
---|
46 | }
|
---|
47 |
|
---|
48 | // check josm.home
|
---|
49 | //
|
---|
50 | String josmHome = testProperties.getProperty("josm.home");
|
---|
51 | if (josmHome == null) {
|
---|
52 | fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
|
---|
53 | } else {
|
---|
54 | File f = new File(josmHome);
|
---|
55 | if (! f.exists() || ! f.canRead()) {
|
---|
56 | fail(MessageFormat.format("property ''{0}'' points to ''{1}'' which is either not existing or not readable.\nEdit ''{2}'' and update the value ''josm.home''. ", "josm.home", josmHome,testPropertiesResourceName ));
|
---|
57 | }
|
---|
58 | }
|
---|
59 | System.setProperty("josm.home", josmHome);
|
---|
60 | Main.pref = new Preferences();
|
---|
61 | I18n.init();
|
---|
62 | // initialize the plaform hook, and
|
---|
63 | Main.determinePlatformHook();
|
---|
64 | // call the really early hook before we anything else
|
---|
65 | Main.platform.preStartupHook();
|
---|
66 |
|
---|
67 | Main.pref.init(false);
|
---|
68 |
|
---|
69 | // init projection
|
---|
70 | Main.proj = new Mercator();
|
---|
71 |
|
---|
72 | // make sure we don't upload to or test against production
|
---|
73 | //
|
---|
74 | String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase().trim();
|
---|
75 | if (url.startsWith("http://www.openstreetmap.org")
|
---|
76 | || url.startsWith("http://api.openstreetmap.org")) {
|
---|
77 | fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|