| 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 |         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 = Config.getPref().get("osm-server.url");
 | 
|---|
| 111 |         if (url == null || url.isEmpty() || isProductionApiUrl(url)) {
 | 
|---|
| 112 |             Config.getPref().put("osm-server.url", "http://api06.dev.openstreetmap.org/api");
 | 
|---|
| 113 |         }
 | 
|---|
| 114 |         I18n.set(Config.getPref().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 |         // setup projection grid files
 | 
|---|
| 126 |         MainApplication.setupNadGridSources();
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         // make sure we don't upload to or test against production
 | 
|---|
| 129 |         url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
 | 
|---|
| 130 |         if (isProductionApiUrl(url)) {
 | 
|---|
| 131 |             fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
 | 
|---|
| 132 |         }
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         // Setup callbacks
 | 
|---|
| 135 |         DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
 | 
|---|
| 136 | 
 | 
|---|
| 137 |         if (createGui) {
 | 
|---|
| 138 |             GuiHelper.runInEDTAndWaitWithException(new Runnable() {
 | 
|---|
| 139 |                 @Override
 | 
|---|
| 140 |                 public void run() {
 | 
|---|
| 141 |                     setupGUI();
 | 
|---|
| 142 |                 }
 | 
|---|
| 143 |             });
 | 
|---|
| 144 |         }
 | 
|---|
| 145 |     }
 | 
|---|
| 146 | 
 | 
|---|
| 147 |     private static boolean isProductionApiUrl(String url) {
 | 
|---|
| 148 |         return url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
 | 
|---|
| 149 |             || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org");
 | 
|---|
| 150 |     }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |     @SuppressWarnings("deprecation")
 | 
|---|
| 153 |     private void setupGUI() {
 | 
|---|
| 154 |         JOSMTestRules.cleanLayerEnvironment();
 | 
|---|
| 155 |         assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
 | 
|---|
| 156 |         assertNull(MainApplication.getLayerManager().getEditLayer());
 | 
|---|
| 157 |         assertNull(MainApplication.getLayerManager().getActiveLayer());
 | 
|---|
| 158 | 
 | 
|---|
| 159 |         initContentPane();
 | 
|---|
| 160 |         initMainPanel(false);
 | 
|---|
| 161 |         initToolbar();
 | 
|---|
| 162 |         if (Main.main == null) {
 | 
|---|
| 163 |             new MainApplication().initialize();
 | 
|---|
| 164 |         } else {
 | 
|---|
| 165 |             if (Main.main.panel == null) {
 | 
|---|
| 166 |                 initMainPanel(false);
 | 
|---|
| 167 |                 Main.main.panel = MainApplication.getMainPanel();
 | 
|---|
| 168 |             }
 | 
|---|
| 169 |             Main.main.panel.reAddListeners();
 | 
|---|
| 170 |         }
 | 
|---|
| 171 |         // Add a test layer to the layer manager to get the MapFrame
 | 
|---|
| 172 |         MainApplication.getLayerManager().addLayer(new TestLayer());
 | 
|---|
| 173 |     }
 | 
|---|
| 174 | 
 | 
|---|
| 175 |     /**
 | 
|---|
| 176 |      * Make sure {@code MainApplication.contentPanePrivate} is initialized.
 | 
|---|
| 177 |      */
 | 
|---|
| 178 |     public static void initContentPane() {
 | 
|---|
| 179 |         MainApplicationTest.initContentPane();
 | 
|---|
| 180 |     }
 | 
|---|
| 181 | 
 | 
|---|
| 182 |     /**
 | 
|---|
| 183 |      * Make sure {@code MainApplication.mainPanel} is initialized.
 | 
|---|
| 184 |      */
 | 
|---|
| 185 |     public static void initMainPanel() {
 | 
|---|
| 186 |         initMainPanel(false);
 | 
|---|
| 187 |     }
 | 
|---|
| 188 | 
 | 
|---|
| 189 |     /**
 | 
|---|
| 190 |      * Make sure {@code MainApplication.mainPanel} is initialized.
 | 
|---|
| 191 |      * @param reAddListeners {@code true} to re-add listeners
 | 
|---|
| 192 |      */
 | 
|---|
| 193 |     public static void initMainPanel(boolean reAddListeners) {
 | 
|---|
| 194 |         MainApplicationTest.initMainPanel(reAddListeners);
 | 
|---|
| 195 |     }
 | 
|---|
| 196 | 
 | 
|---|
| 197 |     /**
 | 
|---|
| 198 |      * Make sure {@code MainApplication.toolbar} is initialized.
 | 
|---|
| 199 |      */
 | 
|---|
| 200 |     public static void initToolbar() {
 | 
|---|
| 201 |         MainApplicationTest.initToolbar();
 | 
|---|
| 202 |     }
 | 
|---|
| 203 | 
 | 
|---|
| 204 |     /**
 | 
|---|
| 205 |      * Make sure {@code MainApplication.menu} is initialized.
 | 
|---|
| 206 |      */
 | 
|---|
| 207 |     public static void initMainMenu() {
 | 
|---|
| 208 |         MainApplicationTest.initMainMenu();
 | 
|---|
| 209 |     }
 | 
|---|
| 210 | }
 | 
|---|