Skip to content

Commit

Permalink
fix #21953: Remember custom overpass servers
Browse files Browse the repository at this point in the history
This was caused by not adding the new server to the HistoryComboBox list
prior to saving the list.


git-svn-id: https://josm.openstreetmap.de/svn/trunk@18403 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
taylor.smock committed Mar 21, 2022
1 parent 2b1e212 commit 623c629
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
import org.openstreetmap.josm.io.OverpassDownloadReader;
import org.openstreetmap.josm.tools.GBC;
import org.openstreetmap.josm.tools.Utils;

/**
* Preferences related to Overpass API servers.
Expand Down Expand Up @@ -50,7 +51,16 @@ public final void initFromPreferences() {
* Saves the current values to the preferences
*/
public final void saveToPreferences() {
OverpassDownloadReader.OVERPASS_SERVER.put(overpassServer.getText());
// Get the new server -- add the trailing `/` if it does not exist to avoid duplicate entries.
// Other code locations assume that the URL has a trailing `/` as well.
String newServer = Utils.strip(overpassServer.getText());
if (!newServer.endsWith("/")) {
newServer += "/";
}
OverpassDownloadReader.OVERPASS_SERVER.put(newServer);
// Ensure that the new overpass server is added to history
overpassServer.setText(newServer);
overpassServer.addCurrentItemToHistory();
overpassServer.getModel().prefs().save(OverpassDownloadReader.OVERPASS_SERVER_HISTORY);
OverpassDownloadReader.FOR_MULTI_FETCH.put(forMultiFetch.isSelected());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.gui.preferences.server;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
import org.openstreetmap.josm.io.OverpassDownloadReader;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;

/**
* Test class for {@link OverpassServerPanel}
* @author Taylor Smock
* @since 18403
*/
@BasicPreferences
class OverpassServerPanelTest {
/**
* Non-regression test for <a href="https://josm.openstreetmap.de/ticket/21953">#21953</a>
*/
@ParameterizedTest
@ValueSource(strings = {"https://something.example.com/api", "https://something.example.com/api/"})
void testOverpassApiServerSaved(final String someRandomUrl) {
// We expect the API to have a trailing /
final String expected = someRandomUrl.endsWith("/") ? someRandomUrl : someRandomUrl + '/';
final OverpassServerPanel panel = new OverpassServerPanel();
panel.initFromPreferences();
final HistoryComboBox historyComboBox = Stream.of(panel.getComponents()).filter(HistoryComboBox.class::isInstance)
.map(HistoryComboBox.class::cast).findFirst().orElseGet(() -> fail("No HistoryComboBox found"));
assertEquals(OverpassDownloadReader.OVERPASS_SERVER.get(), historyComboBox.getText());
assertAll(OverpassDownloadReader.OVERPASS_SERVER_HISTORY.get().stream()
.map(server -> () -> assertNotNull(historyComboBox.getModel().find(server), "Server " + server + " not found")));
historyComboBox.setText(someRandomUrl);
panel.saveToPreferences();
panel.initFromPreferences();
assertEquals(OverpassDownloadReader.OVERPASS_SERVER.get(), historyComboBox.getText());
assertAll(OverpassDownloadReader.OVERPASS_SERVER_HISTORY.get().stream()
.map(server -> () -> assertNotNull(historyComboBox.getModel().find(server), "Server " + server + " not found")));
assertEquals(expected, OverpassDownloadReader.OVERPASS_SERVER.get());
assertTrue(OverpassDownloadReader.OVERPASS_SERVER_HISTORY.get().contains(expected),
String.join(";", OverpassDownloadReader.OVERPASS_SERVER_HISTORY.get()));
}
}

0 comments on commit 623c629

Please sign in to comment.