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