Changeset 10604 in josm for trunk/test/unit
- Timestamp:
- 2016-07-23T14:54:19+02:00 (8 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 3 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/actions/CopyActionTest.java
r8876 r10604 3 3 4 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertFalse; 6 import static org.junit.Assert.assertNotNull; 7 import static org.junit.Assert.assertTrue; 8 import static org.junit.Assert.fail; 5 9 10 import java.awt.datatransfer.Clipboard; 11 import java.awt.datatransfer.DataFlavor; 12 import java.awt.datatransfer.StringSelection; 13 import java.awt.datatransfer.UnsupportedFlavorException; 14 import java.io.IOException; 6 15 import java.util.Arrays; 7 import java.util.Collections;8 16 9 import org.junit. BeforeClass;17 import org.junit.Rule; 10 18 import org.junit.Test; 11 import org.openstreetmap.josm.JOSMFixture; 12 import org.openstreetmap.josm.data.osm.Relation; 19 import org.openstreetmap.josm.Main; 20 import org.openstreetmap.josm.data.coor.LatLon; 21 import org.openstreetmap.josm.data.osm.DataSet; 22 import org.openstreetmap.josm.data.osm.Node; 13 23 import org.openstreetmap.josm.data.osm.Way; 24 import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils; 25 import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData; 26 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 27 import org.openstreetmap.josm.testutils.JOSMTestRules; 28 29 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 14 30 15 31 /** … … 17 33 */ 18 34 public class CopyActionTest { 35 private static final class CapturingCopyAction extends CopyAction { 36 private boolean warningShown; 19 37 20 /** 21 * Setup test. 22 */ 23 @BeforeClass 24 public static void setUpBeforeClass() { 25 JOSMFixture.createUnitTestFixture().init(); 38 @Override 39 protected void showEmptySelectionWarning() { 40 warningShown = true; 41 } 26 42 } 27 43 28 44 /** 29 * Test of {@link CopyAction#getCopyString} method for a single way. 45 * We need prefs for this. 46 */ 47 @Rule 48 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 49 public JOSMTestRules test = new JOSMTestRules().preferences().platform().fakeAPI(); 50 51 /** 52 * Test that copy action copies the selected primitive 53 * @throws IOException if an I/O error occurs 54 * @throws UnsupportedFlavorException if the requested data flavor is not supported 30 55 */ 31 56 @Test 32 public void testCopyStringWay() { 33 final Way way = new Way(123L); 34 assertEquals("way 123", CopyAction.getCopyString(Collections.singleton(way))); 57 public void testWarnOnEmpty() throws UnsupportedFlavorException, IOException { 58 Clipboard clipboard = ClipboardUtils.getClipboard(); 59 clipboard.setContents(new StringSelection("test"), null); 60 61 CapturingCopyAction action = new CapturingCopyAction(); 62 63 action.updateEnabledState(); 64 assertFalse(action.isEnabled()); 65 action.actionPerformed(null); 66 assertTrue(action.warningShown); 67 68 Main.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "test", null)); 69 action.warningShown = false; 70 71 action.updateEnabledState(); 72 assertFalse(action.isEnabled()); 73 action.actionPerformed(null); 74 assertTrue(action.warningShown); 75 76 assertEquals("test", clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor)); 35 77 } 36 78 37 79 /** 38 * Test of {@link CopyAction#getCopyString} method for a way and a relation. 80 * Test that copy action copies the selected primitive 81 * @throws Exception if an error occurs 39 82 */ 40 83 @Test 41 public void testCopyStringWayRelation() { 42 final Way way = new Way(123L); 43 final Relation relation = new Relation(456); 44 assertEquals("way 123,relation 456", CopyAction.getCopyString(Arrays.asList(way, relation))); 45 assertEquals("relation 456,way 123", CopyAction.getCopyString(Arrays.asList(relation, way))); 84 public void testCopySinglePrimitive() throws Exception { 85 DataSet data = new DataSet(); 86 87 Node node1 = new Node(); 88 node1.setCoor(LatLon.ZERO); 89 data.addPrimitive(node1); 90 91 Node node2 = new Node(); 92 node2.setCoor(LatLon.ZERO); 93 data.addPrimitive(node2); 94 Way way = new Way(); 95 way.setNodes(Arrays.asList(node1, node2)); 96 data.addPrimitive(way); 97 data.setSelected(way); 98 99 Main.getLayerManager().addLayer(new OsmDataLayer(data, "test", null)); 100 101 CopyAction action = new CopyAction() { 102 @Override 103 protected void showEmptySelectionWarning() { 104 fail("Selection is not empty."); 105 } 106 }; 107 action.updateEnabledState(); 108 assertTrue(action.isEnabled()); 109 action.actionPerformed(null); 110 111 Object copied = ClipboardUtils.getClipboard().getContents(null).getTransferData(PrimitiveTransferData.DATA_FLAVOR); 112 assertNotNull(copied); 113 assertTrue(copied instanceof PrimitiveTransferData); 114 PrimitiveTransferData ptd = (PrimitiveTransferData) copied; 115 Object[] direct = ptd.getDirectlyAdded().toArray(); 116 assertEquals(1, direct.length); 117 Object[] referenced = ptd.getReferenced().toArray(); 118 assertEquals(2, referenced.length); 46 119 } 47 120 } -
trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/PrimitiveTransferableTest.java
r9711 r10604 5 5 import static org.junit.Assert.assertFalse; 6 6 import static org.junit.Assert.assertTrue; 7 import static org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable.PRIMITIVE_DATA;8 7 9 8 import java.awt.datatransfer.DataFlavor; 10 9 import java.awt.datatransfer.UnsupportedFlavorException; 10 import java.util.Arrays; 11 11 import java.util.Collection; 12 12 import java.util.Collections; 13 import java.util.List; 13 14 14 import org.junit. BeforeClass;15 import org.junit.Rule; 15 16 import org.junit.Test; 16 import org.openstreetmap.josm.JOSMFixture;17 17 import org.openstreetmap.josm.data.osm.Node; 18 import org.openstreetmap.josm.data.osm.NodeData; 18 19 import org.openstreetmap.josm.data.osm.PrimitiveData; 20 import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData; 21 import org.openstreetmap.josm.gui.datatransfer.data.TagTransferData; 22 import org.openstreetmap.josm.testutils.JOSMTestRules; 23 24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 19 25 20 26 /** … … 22 28 */ 23 29 public class PrimitiveTransferableTest { 30 /** 31 * Prefs to use OSM primitives 32 */ 33 @Rule 34 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 35 public JOSMTestRules test = new JOSMTestRules().preferences(); 24 36 25 37 /** 26 * Setup tests 27 */ 28 @BeforeClass 29 public static void setUpBeforeClass() { 30 JOSMFixture.createUnitTestFixture().init(); 31 } 32 33 /** 34 * Test of {@link PrimitiveTransferable#getTransferDataFlavors()} method. 38 * Test of {@link PrimitiveTransferable#getTransferDataFlavors()} method response order 35 39 */ 36 40 @Test 37 41 public void testGetTransferDataFlavors() { 38 DataFlavor[] flavors = new PrimitiveTransferable(null).getTransferDataFlavors(); 39 assertEquals(2, flavors.length); 40 assertEquals(PRIMITIVE_DATA, flavors[0]); 41 assertEquals(DataFlavor.stringFlavor, flavors[1]); 42 List<DataFlavor> flavors = Arrays.asList(new PrimitiveTransferable(null).getTransferDataFlavors()); 43 int ptd = flavors.indexOf(PrimitiveTransferData.DATA_FLAVOR); 44 int tags = flavors.indexOf(TagTransferData.FLAVOR); 45 int string = flavors.indexOf(DataFlavor.stringFlavor); 46 47 assertTrue(ptd >= 0); 48 assertTrue(tags >= 0); 49 assertTrue(string >= 0); 50 51 assertTrue(ptd < tags); 52 assertTrue(tags < string); 42 53 } 43 54 … … 47 58 @Test 48 59 public void testIsDataFlavorSupported() { 49 assertTrue(new PrimitiveTransferable(null).isDataFlavorSupported(P RIMITIVE_DATA));50 assertFalse(new PrimitiveTransferable(null).isDataFlavorSupported( null));60 assertTrue(new PrimitiveTransferable(null).isDataFlavorSupported(PrimitiveTransferData.DATA_FLAVOR)); 61 assertFalse(new PrimitiveTransferable(null).isDataFlavorSupported(DataFlavor.imageFlavor)); 51 62 } 52 63 … … 57 68 @Test 58 69 public void testGetTransferDataNominal() throws UnsupportedFlavorException { 59 PrimitiveTransferable pt = new PrimitiveTransferable(Collections.singleton(new Node(1))); 60 assertEquals("node 1 # incomplete\n", pt.getTransferData(DataFlavor.stringFlavor)); 61 Collection<PrimitiveData> td = ((PrimitiveTransferable.Data) pt.getTransferData(PRIMITIVE_DATA)).getPrimitiveData(); 70 PrimitiveTransferData data = PrimitiveTransferData.getData(Collections.singleton(new Node(1))); 71 PrimitiveTransferable pt = new PrimitiveTransferable(data); 72 assertEquals("node 1", pt.getTransferData(DataFlavor.stringFlavor)); 73 Collection<PrimitiveData> td = ((PrimitiveTransferData) pt.getTransferData(PrimitiveTransferData.DATA_FLAVOR)).getAll(); 62 74 assertEquals(1, td.size()); 63 assertTrue(td.iterator().next() instanceof PrimitiveData); 75 assertTrue(td.iterator().next() instanceof NodeData); 76 77 78 data = PrimitiveTransferData.getData(Arrays.asList(new Node(1), new Node(2))); 79 pt = new PrimitiveTransferable(data); 80 assertEquals("node 1\nnode 2", pt.getTransferData(DataFlavor.stringFlavor)); 64 81 } 65 82 … … 70 87 @Test(expected = UnsupportedFlavorException.class) 71 88 public void testGetTransferDataError() throws UnsupportedFlavorException { 72 new PrimitiveTransferable(Collections.singleton(new Node(1))).getTransferData(null); 89 PrimitiveTransferData data = PrimitiveTransferData.getData(Collections.singleton(new Node(1))); 90 new PrimitiveTransferable(data).getTransferData(DataFlavor.imageFlavor); 73 91 } 74 92 } -
trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/RelationMemberTransferableTest.java
r9717 r10604 38 38 @Test 39 39 public void testGetTransferDataFlavors() { 40 DataFlavor[] flavors = new RelationMemberTransferable( null).getTransferDataFlavors();40 DataFlavor[] flavors = new RelationMemberTransferable(Collections.<RelationMember>emptyList()).getTransferDataFlavors(); 41 41 assertEquals(2, flavors.length); 42 42 assertEquals(RELATION_MEMBER_DATA, flavors[0]); … … 49 49 @Test 50 50 public void testIsDataFlavorSupported() { 51 assertTrue(new RelationMemberTransferable(null).isDataFlavorSupported(RELATION_MEMBER_DATA)); 52 assertFalse(new RelationMemberTransferable(null).isDataFlavorSupported(null)); 51 RelationMemberTransferable transferable = new RelationMemberTransferable(Collections.<RelationMember>emptyList()); 52 assertTrue(transferable.isDataFlavorSupported(RELATION_MEMBER_DATA)); 53 assertFalse(transferable.isDataFlavorSupported(null)); 53 54 } 54 55 -
trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/actions/RelationEditorActionsTest.java
r10113 r10604 2 2 package org.openstreetmap.josm.gui.dialogs.relation.actions; 3 3 4 import org.junit. BeforeClass;4 import org.junit.Rule; 5 5 import org.junit.Test; 6 import org.openstreetmap.josm.JOSMFixture;7 6 import org.openstreetmap.josm.data.osm.DataSet; 8 7 import org.openstreetmap.josm.data.osm.Relation; … … 15 14 import org.openstreetmap.josm.gui.tagging.TagEditorModel; 16 15 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 16 import org.openstreetmap.josm.testutils.JOSMTestRules; 17 18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 17 19 18 20 /** … … 20 22 */ 21 23 public class RelationEditorActionsTest { 24 /** 25 * Plattform for tooltips. 26 */ 27 @Rule 28 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 29 public JOSMTestRules test = new JOSMTestRules().preferences().platform().commands(); 22 30 23 31 /** 24 * Setup test. 25 */ 26 @BeforeClass 27 public static void setUpBeforeClass() { 28 JOSMFixture.createUnitTestFixture().init(true); 29 } 30 31 /** 32 * Test all actions with minimal data. 32 * Check that all actions do not crash. 33 33 */ 34 34 @Test … … 58 58 59 59 new CopyMembersAction(memberTableModel, layer, editor).actionPerformed(null); 60 new PasteMembersAction(memberTable Model, layer, editor).actionPerformed(null);60 new PasteMembersAction(memberTable, layer, editor).actionPerformed(null); 61 61 62 62 new DeleteCurrentRelationAction(layer, editor).actionPerformed(null); -
trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
r10588 r10604 12 12 import org.junit.runners.model.InitializationError; 13 13 import org.junit.runners.model.Statement; 14 import org.openstreetmap.josm.JOSMFixture; 14 15 import org.openstreetmap.josm.Main; 15 16 import org.openstreetmap.josm.data.projection.Projections; … … 40 41 private boolean platform; 41 42 private boolean useProjection; 43 private boolean commands; 42 44 private boolean allowMemoryManagerLeaks; 43 45 … … 132 134 public JOSMTestRules projection() { 133 135 useProjection = true; 136 return this; 137 } 138 139 /** 140 * Allow the execution of commands using {@link Main#undoRedo} 141 * @return this instance, for easy chaining 142 */ 143 public JOSMTestRules commands() { 144 commands = true; 134 145 return this; 135 146 } … … 221 232 if (platform) { 222 233 Main.determinePlatformHook(); 234 } 235 236 if (commands) { 237 // TODO: Implement a more selective version of this once Main is restructured. 238 JOSMFixture.createUnitTestFixture().init(true); 223 239 } 224 240 }
Note:
See TracChangeset
for help on using the changeset viewer.