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