[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm;
|
---|
| 3 |
|
---|
[10443] | 4 | import static org.junit.Assert.assertNull;
|
---|
| 5 | import static org.junit.Assert.assertTrue;
|
---|
[7937] | 6 | import static org.junit.Assert.fail;
|
---|
| 7 |
|
---|
| 8 | import java.io.File;
|
---|
[10019] | 9 | import java.io.IOException;
|
---|
[7937] | 10 | import java.nio.file.Paths;
|
---|
[10235] | 11 | import java.security.GeneralSecurityException;
|
---|
[7937] | 12 | import java.text.MessageFormat;
|
---|
[10222] | 13 | import java.util.Locale;
|
---|
[10467] | 14 | import java.util.TimeZone;
|
---|
[7937] | 15 |
|
---|
| 16 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
| 17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[10467] | 18 | import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
|
---|
[7937] | 19 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
[10400] | 20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[10019] | 21 | import org.openstreetmap.josm.io.CertificateAmendment;
|
---|
[7937] | 22 | import org.openstreetmap.josm.io.OsmApi;
|
---|
| 23 | import org.openstreetmap.josm.tools.I18n;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Fixture to define a proper and safe environment before running tests.
|
---|
| 27 | */
|
---|
| 28 | public class JOSMFixture {
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Returns a new test fixture initialized to "unit" home.
|
---|
| 32 | * @return A new test fixture for unit tests
|
---|
| 33 | */
|
---|
[8511] | 34 | public static JOSMFixture createUnitTestFixture() {
|
---|
[7937] | 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 | */
|
---|
[8511] | 42 | public static JOSMFixture createFunctionalTestFixture() {
|
---|
[7937] | 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 | */
|
---|
[8511] | 50 | public static JOSMFixture createPerformanceTestFixture() {
|
---|
[7937] | 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);
|
---|
[8510] | 83 | if (!f.exists() || !f.canRead()) {
|
---|
[8509] | 84 | fail(MessageFormat.format(
|
---|
[8540] | 85 | // CHECKSTYLE.OFF: LineLength
|
---|
[8509] | 86 | "property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
|
---|
[8540] | 87 | // CHECKSTYLE.ON: LineLength
|
---|
[7937] | 88 | "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
[3988] | 91 | System.setProperty("josm.home", josmHome);
|
---|
[10467] | 92 | TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
---|
[7937] | 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 |
|
---|
[10467] | 101 | Main.logLevel = 3;
|
---|
[7937] | 102 | Main.pref.init(false);
|
---|
| 103 | I18n.set(Main.pref.get("language", "en"));
|
---|
| 104 |
|
---|
[10019] | 105 | try {
|
---|
| 106 | CertificateAmendment.addMissingCertificates();
|
---|
[10235] | 107 | } catch (IOException | GeneralSecurityException ex) {
|
---|
[10019] | 108 | throw new RuntimeException(ex);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[7937] | 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 | //
|
---|
[10222] | 116 | String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
|
---|
[7937] | 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) {
|
---|
[10400] | 123 | GuiHelper.runInEDTAndWaitWithException(new Runnable() {
|
---|
| 124 | @Override
|
---|
| 125 | public void run() {
|
---|
| 126 | setupGUI();
|
---|
[9801] | 127 | }
|
---|
[10400] | 128 | });
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private void setupGUI() {
|
---|
[10467] | 133 | Main.getLayerManager().resetState();
|
---|
| 134 | assertTrue(Main.getLayerManager().getLayers().isEmpty());
|
---|
| 135 | assertNull(Main.getLayerManager().getEditLayer());
|
---|
| 136 | assertNull(Main.getLayerManager().getActiveLayer());
|
---|
| 137 |
|
---|
[10400] | 138 | if (Main.toolbar == null) {
|
---|
| 139 | Main.toolbar = new ToolbarPreferences();
|
---|
| 140 | }
|
---|
| 141 | if (Main.main == null) {
|
---|
| 142 | new MainApplication().initialize();
|
---|
[10510] | 143 | } else {
|
---|
| 144 | Main.mainPanel.reAddListeners();
|
---|
[10400] | 145 | }
|
---|
[10467] | 146 | // Add a test layer to the layer manager to get the MapFrame
|
---|
| 147 | Main.getLayerManager().addLayer(new TestLayer());
|
---|
[7937] | 148 | }
|
---|
| 149 | }
|
---|