| 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.actions.DeleteAction;
|
|---|
| 17 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 18 | import org.openstreetmap.josm.data.projection.Projections;
|
|---|
| 19 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 20 | import org.openstreetmap.josm.gui.MainApplicationTest;
|
|---|
| 21 | import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
|
|---|
| 22 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 23 | import org.openstreetmap.josm.io.CertificateAmendment;
|
|---|
| 24 | import org.openstreetmap.josm.io.OsmApi;
|
|---|
| 25 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 26 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
|---|
| 27 | import org.openstreetmap.josm.tools.I18n;
|
|---|
| 28 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
|---|
| 29 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Fixture to define a proper and safe environment before running tests.
|
|---|
| 33 | */
|
|---|
| 34 | public class JOSMFixture {
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Returns a new test fixture initialized to "unit" home.
|
|---|
| 38 | * @return A new test fixture for unit tests
|
|---|
| 39 | */
|
|---|
| 40 | public static JOSMFixture createUnitTestFixture() {
|
|---|
| 41 | return new JOSMFixture("test/config/unit-josm.home");
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Returns a new test fixture initialized to "functional" home.
|
|---|
| 46 | * @return A new test fixture for functional tests
|
|---|
| 47 | */
|
|---|
| 48 | public static JOSMFixture createFunctionalTestFixture() {
|
|---|
| 49 | return new JOSMFixture("test/config/functional-josm.home");
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Returns a new test fixture initialized to "performance" home.
|
|---|
| 54 | * @return A new test fixture for performance tests
|
|---|
| 55 | */
|
|---|
| 56 | public static JOSMFixture createPerformanceTestFixture() {
|
|---|
| 57 | return new JOSMFixture("test/config/performance-josm.home");
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | private final String josmHome;
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Constructs a new text fixture initialized to a given josm home.
|
|---|
| 64 | * @param josmHome The user home where preferences are to be read/written
|
|---|
| 65 | */
|
|---|
| 66 | public JOSMFixture(String josmHome) {
|
|---|
| 67 | this.josmHome = josmHome;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Initializes the test fixture, without GUI.
|
|---|
| 72 | */
|
|---|
| 73 | public void init() {
|
|---|
| 74 | init(false);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Initializes the test fixture, with or without GUI.
|
|---|
| 79 | * @param createGui if {@code true} creates main GUI components
|
|---|
| 80 | */
|
|---|
| 81 | public void init(boolean createGui) {
|
|---|
| 82 |
|
|---|
| 83 | // check josm.home
|
|---|
| 84 | //
|
|---|
| 85 | if (josmHome == null) {
|
|---|
| 86 | fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
|
|---|
| 87 | } else {
|
|---|
| 88 | File f = new File(josmHome);
|
|---|
| 89 | if (!f.exists() || !f.canRead()) {
|
|---|
| 90 | fail(MessageFormat.format(
|
|---|
| 91 | // CHECKSTYLE.OFF: LineLength
|
|---|
| 92 | "property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
|
|---|
| 93 | // CHECKSTYLE.ON: LineLength
|
|---|
| 94 | "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | System.setProperty("josm.home", josmHome);
|
|---|
| 98 | TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
|---|
| 99 | Config.setPreferencesInstance(Main.pref);
|
|---|
| 100 | Config.setBaseDirectoriesProvider(Main.pref);
|
|---|
| 101 | Main.pref.resetToInitialState();
|
|---|
| 102 | Main.pref.enableSaveOnPut(false);
|
|---|
| 103 | I18n.init();
|
|---|
| 104 | // initialize the plaform hook, and
|
|---|
| 105 | Main.determinePlatformHook();
|
|---|
| 106 | // call the really early hook before we anything else
|
|---|
| 107 | Main.platform.preStartupHook();
|
|---|
| 108 |
|
|---|
| 109 | Logging.setLogLevel(Logging.LEVEL_INFO);
|
|---|
| 110 | Main.pref.init(false);
|
|---|
| 111 | String url = Config.getPref().get("osm-server.url");
|
|---|
| 112 | if (url == null || url.isEmpty() || isProductionApiUrl(url)) {
|
|---|
| 113 | Config.getPref().put("osm-server.url", "http://api06.dev.openstreetmap.org/api");
|
|---|
| 114 | }
|
|---|
| 115 | I18n.set(Config.getPref().get("language", "en"));
|
|---|
| 116 |
|
|---|
| 117 | try {
|
|---|
| 118 | CertificateAmendment.addMissingCertificates();
|
|---|
| 119 | } catch (IOException | GeneralSecurityException ex) {
|
|---|
| 120 | throw new JosmRuntimeException(ex);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | // init projection
|
|---|
| 124 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
|---|
| 125 |
|
|---|
| 126 | // setup projection grid files
|
|---|
| 127 | MainApplication.setupNadGridSources();
|
|---|
| 128 |
|
|---|
| 129 | // make sure we don't upload to or test against production
|
|---|
| 130 | url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
|
|---|
| 131 | if (isProductionApiUrl(url)) {
|
|---|
| 132 | fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | // Setup callbacks
|
|---|
| 136 | DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
|
|---|
| 137 |
|
|---|
| 138 | if (createGui) {
|
|---|
| 139 | GuiHelper.runInEDTAndWaitWithException(new Runnable() {
|
|---|
| 140 | @Override
|
|---|
| 141 | public void run() {
|
|---|
| 142 | setupGUI();
|
|---|
| 143 | }
|
|---|
| 144 | });
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | private static boolean isProductionApiUrl(String url) {
|
|---|
| 149 | return url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
|
|---|
| 150 | || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org");
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | @SuppressWarnings("deprecation")
|
|---|
| 154 | private void setupGUI() {
|
|---|
| 155 | JOSMTestRules.cleanLayerEnvironment();
|
|---|
| 156 | assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
|
|---|
| 157 | assertNull(MainApplication.getLayerManager().getEditLayer());
|
|---|
| 158 | assertNull(MainApplication.getLayerManager().getActiveLayer());
|
|---|
| 159 |
|
|---|
| 160 | initContentPane();
|
|---|
| 161 | initMainPanel(false);
|
|---|
| 162 | initToolbar();
|
|---|
| 163 | if (Main.main == null) {
|
|---|
| 164 | new MainApplication().initialize();
|
|---|
| 165 | } else {
|
|---|
| 166 | if (Main.main.panel == null) {
|
|---|
| 167 | initMainPanel(false);
|
|---|
| 168 | Main.main.panel = MainApplication.getMainPanel();
|
|---|
| 169 | }
|
|---|
| 170 | Main.main.panel.reAddListeners();
|
|---|
| 171 | }
|
|---|
| 172 | // Add a test layer to the layer manager to get the MapFrame
|
|---|
| 173 | MainApplication.getLayerManager().addLayer(new TestLayer());
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /**
|
|---|
| 177 | * Make sure {@code MainApplication.contentPanePrivate} is initialized.
|
|---|
| 178 | */
|
|---|
| 179 | public static void initContentPane() {
|
|---|
| 180 | MainApplicationTest.initContentPane();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * Make sure {@code MainApplication.mainPanel} is initialized.
|
|---|
| 185 | */
|
|---|
| 186 | public static void initMainPanel() {
|
|---|
| 187 | initMainPanel(false);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * Make sure {@code MainApplication.mainPanel} is initialized.
|
|---|
| 192 | * @param reAddListeners {@code true} to re-add listeners
|
|---|
| 193 | */
|
|---|
| 194 | public static void initMainPanel(boolean reAddListeners) {
|
|---|
| 195 | MainApplicationTest.initMainPanel(reAddListeners);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /**
|
|---|
| 199 | * Make sure {@code MainApplication.toolbar} is initialized.
|
|---|
| 200 | */
|
|---|
| 201 | public static void initToolbar() {
|
|---|
| 202 | MainApplicationTest.initToolbar();
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * Make sure {@code MainApplication.menu} is initialized.
|
|---|
| 207 | */
|
|---|
| 208 | public static void initMainMenu() {
|
|---|
| 209 | MainApplicationTest.initMainMenu();
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|