diff --git a/scripts/TagInfoExtract.groovy b/scripts/TagInfoExtract.groovy
index 828e325..0bb00a7 100644
a
|
b
|
class TagInfoExtract {
|
370 | 370 | * Initialize the script. |
371 | 371 | */ |
372 | 372 | def init() { |
373 | | Main.initApplicationPreferences() |
374 | 373 | Main.determinePlatformHook() |
375 | 374 | Main.pref.enableSaveOnPut(false) |
376 | 375 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")) |
diff --git a/src/org/openstreetmap/josm/Main.java b/src/org/openstreetmap/josm/Main.java
index 85245aa..01d0378 100644
a
|
b
|
public abstract class Main {
|
167 | 167 | /** |
168 | 168 | * Global application preferences |
169 | 169 | */ |
170 | | public static Preferences pref; |
| 170 | public static final Preferences pref = new Preferences(); |
171 | 171 | |
172 | 172 | /** |
173 | 173 | * The MapFrame. Use {@link Main#setMapFrame} to set or clear it. |
… |
… |
public abstract class Main {
|
514 | 514 | public static volatile PlatformHook platform; |
515 | 515 | |
516 | 516 | /** |
517 | | * Initializes {@code Main.pref} in normal application context. |
518 | | * @since 6471 |
519 | | */ |
520 | | public static void initApplicationPreferences() { |
521 | | Main.pref = new Preferences(); |
522 | | } |
523 | | |
524 | | /** |
525 | 517 | * Set or clear (if passed <code>null</code>) the map. |
526 | 518 | * <p> |
527 | 519 | * To be removed any time |
diff --git a/src/org/openstreetmap/josm/data/Preferences.java b/src/org/openstreetmap/josm/data/Preferences.java
index 6c738e9..e145cac 100644
a
|
b
|
public class Preferences {
|
778 | 778 | } |
779 | 779 | } |
780 | 780 | |
| 781 | /** |
| 782 | * Resets the preferences to their initial state. This resets all values and file associations. |
| 783 | * The default values and listeners are not removed. |
| 784 | * <p> |
| 785 | * It is meant to be called before {@link #init(boolean)} |
| 786 | */ |
| 787 | public void resetToInitialState() { |
| 788 | resetToDefault(); |
| 789 | preferencesDir = null; |
| 790 | cacheDir = null; |
| 791 | userdataDir = null; |
| 792 | saveOnPut = true; |
| 793 | initSuccessful = false; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Reset all values stored in this map to the default values. This clears the preferences. |
| 798 | */ |
781 | 799 | public final void resetToDefault() { |
782 | 800 | settingsMap.clear(); |
783 | 801 | } |
diff --git a/src/org/openstreetmap/josm/gui/MainApplication.java b/src/org/openstreetmap/josm/gui/MainApplication.java
index ea120a1..e42444a 100644
a
|
b
|
public class MainApplication extends Main {
|
325 | 325 | Main.info(tr("Printing debugging messages to console")); |
326 | 326 | } |
327 | 327 | |
328 | | initApplicationPreferences(); |
329 | | |
330 | 328 | Policy.setPolicy(new Policy() { |
331 | 329 | // Permissions for plug-ins loaded when josm is started via webstart |
332 | 330 | private PermissionCollection pc; |
diff --git a/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java b/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java
index fab7dde..0789584 100644
a
|
b
|
import javax.json.spi.JsonProvider;
|
19 | 19 | |
20 | 20 | import org.junit.Assert; |
21 | 21 | import org.junit.Before; |
22 | | import org.junit.BeforeClass; |
| 22 | import org.junit.Rule; |
23 | 23 | import org.junit.Test; |
24 | | import org.openstreetmap.josm.JOSMFixture; |
25 | | import org.openstreetmap.josm.Main; |
26 | 24 | import org.openstreetmap.josm.TestUtils; |
27 | 25 | import org.openstreetmap.josm.data.Version; |
28 | 26 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| 27 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| 28 | |
| 29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
29 | 30 | |
30 | 31 | /** |
31 | 32 | * Tests the {@link HttpClient} using the webservice <a href="https://httpbin.org/">https://httpbin.org/</a>. |
32 | 33 | */ |
33 | 34 | public class HttpClientTest { |
34 | 35 | |
35 | | private ProgressMonitor progress; |
| 36 | /** |
| 37 | * |
| 38 | */ |
| 39 | @Rule |
| 40 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 41 | public JOSMTestRules test = new JOSMTestRules().preferences(); |
36 | 42 | |
37 | | @BeforeClass |
38 | | public static void setUpBeforeClass() { |
39 | | JOSMFixture.createFunctionalTestFixture().init(); |
40 | | } |
| 43 | private ProgressMonitor progress; |
41 | 44 | |
42 | 45 | @Before |
43 | 46 | public void setUp() { |
… |
… |
public class HttpClientTest {
|
172 | 175 | */ |
173 | 176 | @Test |
174 | 177 | public void testOpenUrlGzip() throws IOException { |
175 | | Main.initApplicationPreferences(); |
176 | 178 | final URL url = new URL("https://www.openstreetmap.org/trace/1613906/data"); |
177 | 179 | try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) { |
178 | 180 | Assert.assertTrue(x.readLine().startsWith("<?xml version=")); |
… |
… |
public class HttpClientTest {
|
185 | 187 | */ |
186 | 188 | @Test |
187 | 189 | public void testOpenUrlBzip() throws IOException { |
188 | | Main.initApplicationPreferences(); |
189 | 190 | final URL url = new URL("https://www.openstreetmap.org/trace/785544/data"); |
190 | 191 | try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) { |
191 | 192 | Assert.assertTrue(x.readLine().startsWith("<?xml version=")); |
… |
… |
public class HttpClientTest {
|
198 | 199 | */ |
199 | 200 | @Test |
200 | 201 | public void testTicket9660() throws IOException { |
201 | | Main.initApplicationPreferences(); |
202 | 202 | final URL url = new URL("http://www.openstreetmap.org/trace/1350010/data"); |
203 | 203 | try (BufferedReader x = HttpClient.create(url).connect() |
204 | 204 | .uncompress(true).uncompressAccordingToContentDisposition(true).getContentReader()) { |
diff --git a/test/unit/org/openstreetmap/josm/JOSMFixture.java b/test/unit/org/openstreetmap/josm/JOSMFixture.java
index 81d579b..f9bcc23 100644
a
|
b
|
public class JOSMFixture {
|
90 | 90 | } |
91 | 91 | System.setProperty("josm.home", josmHome); |
92 | 92 | TimeZone.setDefault(TimeZone.getTimeZone("UTC")); |
93 | | Main.initApplicationPreferences(); |
| 93 | Main.pref.resetToInitialState(); |
94 | 94 | Main.pref.enableSaveOnPut(false); |
95 | 95 | I18n.init(); |
96 | 96 | // initialize the plaform hook, and |
diff --git a/test/unit/org/openstreetmap/josm/actions/mapmode/SelectActionTest.java b/test/unit/org/openstreetmap/josm/actions/mapmode/SelectActionTest.java
index 0c0ae11..3e52858 100644
a
|
b
|
import org.junit.BeforeClass;
|
16 | 16 | import org.junit.Test; |
17 | 17 | import org.openstreetmap.josm.JOSMFixture; |
18 | 18 | import org.openstreetmap.josm.Main; |
19 | | import org.openstreetmap.josm.data.Preferences; |
20 | 19 | import org.openstreetmap.josm.data.coor.EastNorth; |
21 | 20 | import org.openstreetmap.josm.data.osm.DataSet; |
22 | 21 | import org.openstreetmap.josm.data.osm.Node; |
… |
… |
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
32 | 31 | */ |
33 | 32 | public class SelectActionTest { |
34 | 33 | |
35 | | /** |
36 | | * Override some configuration variables without change in preferences.xml |
37 | | */ |
38 | | static class PreferencesMock extends Preferences { |
39 | | @Override |
40 | | public synchronized int getInteger(String key, int def) { |
41 | | if ("edit.initial-move-delay".equals(key)) { |
42 | | return 0; |
43 | | } else { |
44 | | return super.getInteger(key, def); |
45 | | } |
46 | | } |
47 | | } |
48 | | |
49 | 34 | boolean nodesMerged; |
50 | 35 | |
51 | 36 | class SelectActionMock extends SelectAction { |
… |
… |
public class SelectActionTest {
|
103 | 88 | dataSet.addSelected(n2); |
104 | 89 | dataSet.addSelected(w); |
105 | 90 | |
106 | | Main.pref = new PreferencesMock(); |
| 91 | Main.pref.put("edit.initial-move-delay", "0"); |
107 | 92 | Main.getLayerManager().addLayer(layer); |
108 | 93 | try { |
109 | 94 | SelectAction action = new SelectActionMock(Main.map, dataSet, layer); |
diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/relation/sort/RelationSorterTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/relation/sort/RelationSorterTest.java
index d58ebf7..159a7fe 100644
a
|
b
|
import java.io.InputStream;
|
7 | 7 | import java.util.List; |
8 | 8 | |
9 | 9 | import org.junit.Assert; |
10 | | import org.junit.BeforeClass; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Rule; |
11 | 12 | import org.junit.Test; |
12 | | import org.openstreetmap.josm.Main; |
13 | 13 | import org.openstreetmap.josm.data.osm.DataSet; |
14 | 14 | import org.openstreetmap.josm.data.osm.Relation; |
15 | 15 | import org.openstreetmap.josm.data.osm.RelationMember; |
16 | | import org.openstreetmap.josm.data.projection.Projections; |
17 | 16 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor; |
18 | 17 | import org.openstreetmap.josm.io.IllegalDataException; |
19 | 18 | import org.openstreetmap.josm.io.OsmReader; |
| 19 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| 20 | |
| 21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
20 | 22 | |
21 | 23 | /** |
22 | 24 | * Unit tests of {@link RelationSorter} class. |
… |
… |
import org.openstreetmap.josm.io.OsmReader;
|
24 | 26 | public class RelationSorterTest { |
25 | 27 | |
26 | 28 | private final RelationSorter sorter = new RelationSorter(); |
27 | | private static DataSet testDataset; |
| 29 | private DataSet testDataset; |
| 30 | |
| 31 | /** |
| 32 | * Use Mercator projection |
| 33 | */ |
| 34 | @Rule |
| 35 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 36 | public JOSMTestRules test = new JOSMTestRules().preferences().projection(); |
28 | 37 | |
29 | | @BeforeClass |
30 | | public static void loadData() throws IllegalDataException, IOException { |
31 | | Main.initApplicationPreferences(); |
32 | | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator |
33 | | try (InputStream fis = new FileInputStream("data_nodist/relation_sort.osm")) { |
34 | | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE); |
| 38 | /** |
| 39 | * Load the test data set |
| 40 | * @throws IllegalDataException |
| 41 | * @throws IOException |
| 42 | */ |
| 43 | @Before |
| 44 | public void loadData() throws IllegalDataException, IOException { |
| 45 | if (testDataset == null) { |
| 46 | try (InputStream fis = new FileInputStream("data_nodist/relation_sort.osm")) { |
| 47 | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE); |
| 48 | } |
35 | 49 | } |
36 | 50 | } |
37 | 51 | |
diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/relation/sort/WayConnectionTypeCalculatorTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/relation/sort/WayConnectionTypeCalculatorTest.java
index c9ee5b1..536867d 100644
a
|
b
|
import java.util.Arrays;
|
8 | 8 | import java.util.List; |
9 | 9 | |
10 | 10 | import org.junit.Assert; |
11 | | import org.junit.BeforeClass; |
| 11 | import org.junit.Before; |
| 12 | import org.junit.Rule; |
12 | 13 | import org.junit.Test; |
13 | | import org.openstreetmap.josm.Main; |
14 | 14 | import org.openstreetmap.josm.data.osm.DataSet; |
15 | 15 | import org.openstreetmap.josm.data.osm.Relation; |
16 | | import org.openstreetmap.josm.data.projection.Projections; |
17 | 16 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor; |
18 | 17 | import org.openstreetmap.josm.io.IllegalDataException; |
19 | 18 | import org.openstreetmap.josm.io.OsmReader; |
| 19 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| 20 | |
| 21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
20 | 22 | |
21 | 23 | /** |
22 | 24 | * Unit tests of {@link WayConnectionTypeCalculator} class. |
… |
… |
public class WayConnectionTypeCalculatorTest {
|
25 | 27 | |
26 | 28 | private RelationSorter sorter = new RelationSorter(); |
27 | 29 | private WayConnectionTypeCalculator wayConnectionTypeCalculator = new WayConnectionTypeCalculator(); |
28 | | private static DataSet testDataset; |
29 | | |
30 | | @BeforeClass |
31 | | public static void loadData() throws IllegalDataException, IOException { |
32 | | Main.initApplicationPreferences(); |
33 | | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator |
34 | | try (InputStream fis = new FileInputStream("data_nodist/relation_sort.osm")) { |
35 | | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE); |
| 30 | private DataSet testDataset; |
| 31 | |
| 32 | /** |
| 33 | * Use Mercator projection |
| 34 | */ |
| 35 | @Rule |
| 36 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 37 | public JOSMTestRules test = new JOSMTestRules().preferences().projection(); |
| 38 | |
| 39 | /** |
| 40 | * Load the test data set |
| 41 | * @throws IllegalDataException |
| 42 | * @throws IOException |
| 43 | */ |
| 44 | @Before |
| 45 | public void loadData() throws IllegalDataException, IOException { |
| 46 | if (testDataset == null) { |
| 47 | try (InputStream fis = new FileInputStream("data_nodist/relation_sort.osm")) { |
| 48 | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE); |
| 49 | } |
36 | 50 | } |
37 | 51 | } |
38 | 52 | |
diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
index 634b625..49b6536 100644
a
|
b
|
public class JOSMTestRules implements TestRule {
|
200 | 200 | |
201 | 201 | // Add preferences |
202 | 202 | if (usePreferences) { |
203 | | Main.initApplicationPreferences(); |
| 203 | Main.pref.resetToInitialState(); |
204 | 204 | Main.pref.enableSaveOnPut(false); |
205 | 205 | // No pref init -> that would only create the preferences file. |
206 | 206 | // We force the use of a wrong API server, just in case anyone attempts an upload |
… |
… |
public class JOSMTestRules implements TestRule {
|
246 | 246 | private void cleanUpFromJosmFixture() { |
247 | 247 | MemoryManagerTest.resetState(true); |
248 | 248 | Main.getLayerManager().resetState(); |
249 | | Main.pref = null; |
| 249 | Main.pref.resetToInitialState(); |
250 | 250 | Main.platform = null; |
251 | 251 | System.gc(); |
252 | 252 | } |
… |
… |
public class JOSMTestRules implements TestRule {
|
267 | 267 | MemoryManagerTest.resetState(allowMemoryManagerLeaks); |
268 | 268 | |
269 | 269 | // TODO: Remove global listeners and other global state. |
270 | | Main.pref = null; |
| 270 | Main.pref.resetToInitialState();; |
271 | 271 | Main.platform = null; |
272 | 272 | // Parts of JOSM uses weak references - destroy them. |
273 | 273 | System.gc(); |