source: josm/trunk/test/unit/org/openstreetmap/josm/gui/io/SaveLayersDialogTest.java@ 17360

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7
8import java.util.Collections;
9import java.util.List;
10import javax.swing.JComponent;
11import javax.swing.JLabel;
12import javax.swing.JList;
13import javax.swing.JOptionPane;
14
15import org.junit.jupiter.api.extension.RegisterExtension;
16import org.junit.jupiter.api.Test;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.testutils.JOSMTestRules;
20import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Unit tests of {@link SaveLayersDialog} class.
26 */
27class SaveLayersDialogTest {
28
29 /**
30 * Setup tests
31 */
32 @RegisterExtension
33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
34 public JOSMTestRules test = new JOSMTestRules();
35
36 /**
37 * Test of {@link SaveLayersDialog#confirmSaveLayerInfosOK}.
38 */
39 @Test
40 void testConfirmSaveLayerInfosOK() {
41 final List<SaveLayerInfo> list = Collections.singletonList(new SaveLayerInfo(new OsmDataLayer(new DataSet(), null, null)));
42
43 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker() {
44 @Override
45 protected void act(final Object message) {
46 // use this opportunity to assert that our SaveLayerInfo is the single option in the JList
47 @SuppressWarnings("unchecked")
48 final JList<SaveLayerInfo> jList = (JList<SaveLayerInfo>) ((JComponent) message).getComponent(1);
49 assertEquals(1, jList.getModel().getSize());
50 assertEquals(list.get(0), jList.getModel().getElementAt(0));
51 }
52
53 @Override
54 protected String getStringFromMessage(final Object message) {
55 return ((JLabel) ((JComponent) message).getComponent(0)).getText();
56 }
57 };
58
59 jopsMocker.getMockResultMap().put(
60 "<html>1 layer has unresolved conflicts.<br>Either resolve them first or discard the "
61 + "modifications.<br>Layer with conflicts:</html>", JOptionPane.OK_OPTION
62 );
63
64 assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
65 @Override
66 public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() {
67 return list;
68 }
69 }));
70
71 assertEquals(1, jopsMocker.getInvocationLog().size());
72 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
73 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
74 assertEquals("Unsaved data and conflicts", invocationLogEntry[2]);
75
76 jopsMocker.resetInvocationLog();
77 jopsMocker.getMockResultMap().clear();
78 jopsMocker.getMockResultMap().put(
79 "<html>1 layer needs saving but has no associated file.<br>Either select a file for this "
80 + "layer or discard the changes.<br>Layer without a file:</html>", JOptionPane.OK_OPTION
81 );
82
83 assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
84 @Override
85 public List<SaveLayerInfo> getLayersWithoutFilesAndSaveRequest() {
86 return list;
87 }
88 }));
89
90 assertEquals(1, jopsMocker.getInvocationLog().size());
91 invocationLogEntry = jopsMocker.getInvocationLog().get(0);
92 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
93 assertEquals("Unsaved data and missing associated file", invocationLogEntry[2]);
94
95 jopsMocker.resetInvocationLog();
96 jopsMocker.getMockResultMap().clear();
97 jopsMocker.getMockResultMap().put(
98 "<html>1 layer needs saving but has an associated file<br>which cannot be written.<br>Either "
99 + "select another file for this layer or discard the changes.<br>Layer with a non-writable "
100 + "file:</html>", JOptionPane.OK_OPTION
101 );
102
103 assertFalse(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel() {
104 @Override
105 public List<SaveLayerInfo> getLayersWithIllegalFilesAndSaveRequest() {
106 return list;
107 }
108 }));
109
110 assertEquals(1, jopsMocker.getInvocationLog().size());
111 invocationLogEntry = jopsMocker.getInvocationLog().get(0);
112 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]);
113 assertEquals("Unsaved data non-writable files", invocationLogEntry[2]);
114
115 jopsMocker.resetInvocationLog();
116 jopsMocker.getMockResultMap().clear();
117
118 assertTrue(SaveLayersDialog.confirmSaveLayerInfosOK(new SaveLayersModel()));
119 }
120}
Note: See TracBrowser for help on using the repository browser.