Changeset 14332 in josm for trunk/test/unit/org/openstreetmap
- Timestamp:
- 2018-10-14T17:30:28+02:00 (6 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/actions/MergeLayerActionTest.java
r14138 r14332 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.actions; 3 4 import javax.swing.JLabel; 5 import javax.swing.JPanel; 3 6 4 7 import static org.junit.Assert.assertEquals; … … 9 12 import org.junit.Test; 10 13 import org.openstreetmap.josm.data.osm.DataSet; 14 import org.openstreetmap.josm.gui.ExtendedDialog; 11 15 import org.openstreetmap.josm.gui.MainApplication; 12 16 import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer; 13 17 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 18 import org.openstreetmap.josm.gui.widgets.JosmComboBox; 19 import org.openstreetmap.josm.TestUtils; 14 20 import org.openstreetmap.josm.testutils.JOSMTestRules; 21 import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker; 22 import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; 23 24 import com.google.common.collect.ImmutableMap; 15 25 16 26 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 27 37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 28 38 public JOSMTestRules test = new JOSMTestRules().main(); 39 40 /** 41 * MergeLayerExtendedDialog mocker. 42 */ 43 public static class MergeLayerExtendedDialogMocker extends ExtendedDialogMocker { 44 @Override 45 protected void act(final ExtendedDialog instance) { 46 ((JosmComboBox<?>) ((JPanel) this.getContent(instance)).getComponent(1)).setSelectedIndex(0); 47 } 48 49 @Override 50 protected String getString(final ExtendedDialog instance) { 51 return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(0)).getText(); 52 } 53 } 29 54 30 55 private MergeLayerAction action; … … 58 83 @Test 59 84 public void testMergeNoTargetLayer() { 60 OsmDataLayer layer = new OsmDataLayer(new DataSet(), "", null); 85 TestUtils.assumeWorkingJMockit(); 86 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( 87 ImmutableMap.<String, Object>of("<html>There are no layers the source layer<br>'onion'<br>could be merged to.</html>", 0) 88 ); 89 90 OsmDataLayer layer = new OsmDataLayer(new DataSet(), "onion", null); 61 91 MainApplication.getLayerManager().addLayer(layer); 62 92 assertEquals(1, MainApplication.getLayerManager().getLayers().size()); 63 93 assertNull(action.merge(layer)); 64 94 assertEquals(1, MainApplication.getLayerManager().getLayers().size()); 95 96 assertEquals(1, jopsMocker.getInvocationLog().size()); 97 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0); 98 assertEquals(0, (int) invocationLogEntry[0]); 99 assertEquals("No target layers", invocationLogEntry[2]); 65 100 } 66 101 … … 71 106 @Test 72 107 public void testMergeTwoEmptyLayers() throws Exception { 108 TestUtils.assumeWorkingJMockit(); 109 final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker(); 110 edMocker.getMockResultMap().put("Please select the target layer.", "Merge"); 111 73 112 OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "1", null); 74 113 OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "2", null); … … 78 117 action.merge(layer2).get(); 79 118 assertEquals(1, MainApplication.getLayerManager().getLayers().size()); 119 120 assertEquals(1, edMocker.getInvocationLog().size()); 121 Object[] invocationLogEntry = edMocker.getInvocationLog().get(0); 122 assertEquals(1, (int) invocationLogEntry[0]); 123 assertEquals("Select target layer", invocationLogEntry[2]); 80 124 } 81 125 } -
trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java
r14138 r14332 3 3 4 4 import static java.util.concurrent.TimeUnit.MILLISECONDS; 5 import static org.junit.Assert.assertEquals; 5 6 import static org.junit.Assert.assertNotNull; 6 7 import static org.junit.Assert.assertNull; … … 10 11 import org.junit.Rule; 11 12 import org.junit.Test; 13 import org.openstreetmap.josm.actions.MergeLayerActionTest.MergeLayerExtendedDialogMocker; 12 14 import org.openstreetmap.josm.data.gpx.GpxData; 13 15 import org.openstreetmap.josm.gui.MainApplication; … … 15 17 import org.openstreetmap.josm.gui.layer.TMSLayer; 16 18 import org.openstreetmap.josm.gui.layer.gpx.DownloadWmsAlongTrackAction.PrecacheWmsTask; 19 import org.openstreetmap.josm.TestUtils; 17 20 import org.openstreetmap.josm.testutils.JOSMTestRules; 18 21 import org.openstreetmap.josm.testutils.TileSourceRule; 22 import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; 23 24 import com.google.common.collect.ImmutableMap; 19 25 20 26 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 37 43 @Test 38 44 public void testNoLayer() { 45 TestUtils.assumeWorkingJMockit(); 46 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( 47 ImmutableMap.<String, Object>of("There are no imagery layers.", 0) 48 ); 49 39 50 assertNull(new DownloadWmsAlongTrackAction(new GpxData()).createTask()); 51 52 assertEquals(1, jopsMocker.getInvocationLog().size()); 53 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0); 54 assertEquals(0, (int) invocationLogEntry[0]); 55 assertEquals("No imagery layers", invocationLogEntry[2]); 40 56 } 41 57 … … 46 62 @Test 47 63 public void testTMSLayer() throws Exception { 64 TestUtils.assumeWorkingJMockit(); 65 final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker(); 66 edMocker.getMockResultMap().put("Please select the imagery layer.", "Download"); 67 48 68 final TileSourceRule tileSourceRule = this.test.getTileSourceRule(); 49 69 … … 65 85 MainApplication.getLayerManager().removeLayer(layer); 66 86 } 87 88 assertEquals(1, edMocker.getInvocationLog().size()); 89 Object[] invocationLogEntry = edMocker.getInvocationLog().get(0); 90 assertEquals(1, (int) invocationLogEntry[0]); 91 assertEquals("Select imagery layer", invocationLogEntry[2]); 67 92 } 68 93 } -
trunk/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java
r14052 r14332 11 11 import java.util.WeakHashMap; 12 12 13 import org.openstreetmap.josm.TestUtils; 13 14 import org.openstreetmap.josm.gui.ExtendedDialog; 14 15 import org.openstreetmap.josm.tools.Logging; … … 108 109 } 109 110 111 /** 112 * Target for overriding, similar to {@link #getMockResult} except with the implication it will only 113 * be invoked once per dialog display, therefore ideal opportunity to perform any mutating actions, 114 * e.g. making a selection on a widget. 115 * @param instance dialog instance 116 */ 117 protected void act(final ExtendedDialog instance) { 118 // Override in sub-classes 119 } 120 110 121 protected Object[] getInvocationLogEntry(final ExtendedDialog instance, final int mockResult) { 111 122 return new Object[] { … … 114 125 instance.getTitle() 115 126 }; 127 } 128 129 /** 130 * A convenience method to access {@link ExtendedDialog#content} without exception-catching boilerplate 131 * @param instance dialog instance 132 * @return dialog content component 133 */ 134 protected Component getContent(final ExtendedDialog instance) { 135 try { 136 return (Component) TestUtils.getPrivateField(instance, "content"); 137 } catch (ReflectiveOperationException e) { 138 throw new RuntimeException(e); 139 } 116 140 } 117 141 … … 130 154 try { 131 155 final ExtendedDialog instance = invocation.getInvokedInstance(); 156 this.act(instance); 132 157 final int mockResult = this.getMockResult(instance); 133 158 // TODO check validity of mockResult? -
trunk/test/unit/org/openstreetmap/josm/testutils/mockers/HelpAwareOptionPaneMocker.java
r14201 r14332 76 76 } 77 77 return this.getMockResultMap().get(messageString); 78 } 79 80 /** 81 * Target for overriding, similar to {@link #getMockResultForMessage} except with the implication it 82 * will only be invoked once per dialog display, therefore ideal opportunity to perform any mutating 83 * actions, e.g. making a selection on a widget. 84 * @param message message 85 */ 86 protected void act(final Object message) { 87 // Override in sub-classes 78 88 } 79 89 … … 134 144 ) { 135 145 try { 146 this.act(msg); 136 147 final Object result = this.getMockResultForMessage(msg); 137 148 -
trunk/test/unit/org/openstreetmap/josm/testutils/mockers/JOptionPaneSimpleMocker.java
r14052 r14332 134 134 } 135 135 136 /** 137 * Target for overriding, similar to {@link #getMockResultForMessage} except with the implication it 138 * will only be invoked once per dialog display, therefore ideal opportunity to perform any mutating 139 * actions, e.g. making a selection on a widget. 140 * @param message message 141 */ 142 protected void act(final Object message) { 143 // Override in sub-classes 144 } 145 136 146 protected Object[] getInvocationLogEntry( 137 147 final Object message, … … 162 172 ) { 163 173 try { 174 this.act(message); 164 175 final Object result = this.getMockResultForMessage(message); 165 176 if (selectionValues == null) { … … 216 227 ) { 217 228 try { 229 this.act(message); 218 230 // why look up a "result" for a message dialog which can only have one possible result? it's 219 231 // a good opportunity to assert its contents … … 263 275 ) { 264 276 try { 277 this.act(message); 265 278 final Object result = this.getMockResultForMessage(message); 266 279 if (!(result instanceof Integer && Ints.contains(optionTypePermittedResults.get(optionType), (int) result))) {
Note:
See TracChangeset
for help on using the changeset viewer.