| 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.awt.BorderLayout;
|
|---|
| 9 | import java.io.File;
|
|---|
| 10 | import java.io.IOException;
|
|---|
| 11 | import java.nio.file.Paths;
|
|---|
| 12 | import java.security.GeneralSecurityException;
|
|---|
| 13 | import java.text.MessageFormat;
|
|---|
| 14 | import java.util.Locale;
|
|---|
| 15 | import java.util.TimeZone;
|
|---|
| 16 |
|
|---|
| 17 | import javax.swing.JPanel;
|
|---|
| 18 |
|
|---|
| 19 | import org.openstreetmap.josm.data.projection.Projections;
|
|---|
| 20 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 21 | import org.openstreetmap.josm.gui.MainPanel;
|
|---|
| 22 | import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
|
|---|
| 23 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
|---|
| 24 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 25 | import org.openstreetmap.josm.io.CertificateAmendment;
|
|---|
| 26 | import org.openstreetmap.josm.io.OsmApi;
|
|---|
| 27 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
|---|
| 28 | import org.openstreetmap.josm.tools.I18n;
|
|---|
| 29 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
|---|
| 30 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 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 | */
|
|---|
| 41 | public static JOSMFixture createUnitTestFixture() {
|
|---|
| 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 | */
|
|---|
| 49 | public static JOSMFixture createFunctionalTestFixture() {
|
|---|
| 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 | */
|
|---|
| 57 | public static JOSMFixture createPerformanceTestFixture() {
|
|---|
| 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);
|
|---|
| 90 | if (!f.exists() || !f.canRead()) {
|
|---|
| 91 | fail(MessageFormat.format(
|
|---|
| 92 | // CHECKSTYLE.OFF: LineLength
|
|---|
| 93 | "property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
|
|---|
| 94 | // CHECKSTYLE.ON: LineLength
|
|---|
| 95 | "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | System.setProperty("josm.home", josmHome);
|
|---|
| 99 | TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
|---|
| 100 | Main.pref.resetToInitialState();
|
|---|
| 101 | Main.pref.enableSaveOnPut(false);
|
|---|
| 102 | I18n.init();
|
|---|
| 103 | // initialize the plaform hook, and
|
|---|
| 104 | Main.determinePlatformHook();
|
|---|
| 105 | // call the really early hook before we anything else
|
|---|
| 106 | Main.platform.preStartupHook();
|
|---|
| 107 |
|
|---|
| 108 | Logging.setLogLevel(Logging.LEVEL_INFO);
|
|---|
| 109 | Main.pref.init(false);
|
|---|
| 110 | String url = Main.pref.get("osm-server.url");
|
|---|
| 111 | if (url == null || url.isEmpty() || isProductionApiUrl(url)) {
|
|---|
| 112 | Main.pref.put("osm-server.url", "http://api06.dev.openstreetmap.org/api");
|
|---|
| 113 | }
|
|---|
| 114 | I18n.set(Main.pref.get("language", "en"));
|
|---|
| 115 |
|
|---|
| 116 | try {
|
|---|
| 117 | CertificateAmendment.addMissingCertificates();
|
|---|
| 118 | } catch (IOException | GeneralSecurityException ex) {
|
|---|
| 119 | throw new JosmRuntimeException(ex);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | // init projection
|
|---|
| 123 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
|---|
| 124 |
|
|---|
| 125 | // make sure we don't upload to or test against production
|
|---|
| 126 | url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
|
|---|
| 127 | if (isProductionApiUrl(url)) {
|
|---|
| 128 | fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | if (createGui) {
|
|---|
| 132 | GuiHelper.runInEDTAndWaitWithException(new Runnable() {
|
|---|
| 133 | @Override
|
|---|
| 134 | public void run() {
|
|---|
| 135 | setupGUI();
|
|---|
| 136 | }
|
|---|
| 137 | });
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | private static boolean isProductionApiUrl(String url) {
|
|---|
| 142 | return url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
|
|---|
| 143 | || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org");
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | private void setupGUI() {
|
|---|
| 147 | JOSMTestRules.cleanLayerEnvironment();
|
|---|
| 148 | assertTrue(Main.getLayerManager().getLayers().isEmpty());
|
|---|
| 149 | assertNull(Main.getLayerManager().getEditLayer());
|
|---|
| 150 | assertNull(Main.getLayerManager().getActiveLayer());
|
|---|
| 151 |
|
|---|
| 152 | initContentPane();
|
|---|
| 153 | initMainPanel();
|
|---|
| 154 | initToolbar();
|
|---|
| 155 | if (Main.main == null) {
|
|---|
| 156 | new MainApplication().initialize();
|
|---|
| 157 | } else {
|
|---|
| 158 | if (Main.main.panel == null) {
|
|---|
| 159 | initMainPanel();
|
|---|
| 160 | Main.main.panel = Main.mainPanel;
|
|---|
| 161 | }
|
|---|
| 162 | Main.main.panel.reAddListeners();
|
|---|
| 163 | }
|
|---|
| 164 | // Add a test layer to the layer manager to get the MapFrame
|
|---|
| 165 | Main.getLayerManager().addLayer(new TestLayer());
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Make sure {@code Main.contentPanePrivate} is initialized.
|
|---|
| 170 | */
|
|---|
| 171 | public static void initContentPane() {
|
|---|
| 172 | if (Main.contentPanePrivate == null) {
|
|---|
| 173 | Main.contentPanePrivate = new JPanel(new BorderLayout());
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Make sure {@code Main.mainPanel} is initialized.
|
|---|
| 179 | */
|
|---|
| 180 | public static void initMainPanel() {
|
|---|
| 181 | if (Main.mainPanel == null) {
|
|---|
| 182 | Main.mainPanel = new MainPanel(Main.getLayerManager());
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * Make sure {@code Main.toolbar} is initialized.
|
|---|
| 188 | */
|
|---|
| 189 | public static void initToolbar() {
|
|---|
| 190 | if (Main.toolbar == null) {
|
|---|
| 191 | Main.toolbar = new ToolbarPreferences();
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|