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