Changeset 10444 in josm for trunk/test/unit/org/openstreetmap
- Timestamp:
- 2016-06-21T00:05:47+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/data/AutosaveTaskTest.java
r10301 r10444 9 9 10 10 import java.io.File; 11 import java.io.FileWriter; 12 import java.io.FilenameFilter; 11 13 import java.io.IOException; 12 14 import java.nio.file.Files; 13 15 import java.nio.file.Paths; 14 import java.util.ArrayList;15 16 import java.util.Calendar; 16 17 import java.util.Date; 17 18 import java.util.List; 18 19 import org.junit.BeforeClass; 19 import java.util.concurrent.ExecutionException; 20 21 import org.junit.Before; 22 import org.junit.Rule; 20 23 import org.junit.Test; 21 import org.openstreetmap.josm. JOSMFixture;24 import org.openstreetmap.josm.Main; 22 25 import org.openstreetmap.josm.data.AutosaveTask.AutosaveLayerInfo; 26 import org.openstreetmap.josm.data.coor.LatLon; 23 27 import org.openstreetmap.josm.data.osm.DataSet; 28 import org.openstreetmap.josm.data.osm.Node; 24 29 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 30 import org.openstreetmap.josm.testutils.JOSMTestRules; 31 32 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 25 33 26 34 /** … … 28 36 */ 29 37 public class AutosaveTaskTest { 30 31 private static AutosaveTask task; 38 /** 39 * We need preferences and a home directory for this. 40 */ 41 @Rule 42 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 43 public JOSMTestRules test = new JOSMTestRules().preferences().platform().projection(); 44 45 private AutosaveTask task; 32 46 33 47 /** … … 35 49 * @throws IOException if autosave directory cannot be created 36 50 */ 37 @BeforeClass 38 public static void setUpBeforeClass() throws IOException { 39 JOSMFixture.createUnitTestFixture().init(); 51 @Before 52 public void setUp() throws IOException { 40 53 task = new AutosaveTask(); 41 Files.createDirectories(task.getAutosaveDir());42 54 } 43 55 … … 56 68 @Test 57 69 public void testGetUnsavedLayersFilesNotEmpty() throws IOException { 70 Files.createDirectories(task.getAutosaveDir()); 58 71 String autodir = task.getAutosaveDir().toString(); 59 72 File layer1 = Files.createFile(Paths.get(autodir, "layer1.osm")).toFile(); 60 73 File layer2 = Files.createFile(Paths.get(autodir, "layer2.osm")).toFile(); 61 74 File dir = Files.createDirectory(Paths.get(autodir, "dir.osm")).toFile(); 62 try { 63 List<File> files = task.getUnsavedLayersFiles(); 64 assertEquals(2, files.size()); 65 assertTrue(files.contains(layer1)); 66 assertTrue(files.contains(layer2)); 67 assertFalse(files.contains(dir)); 68 } finally { 69 Files.delete(dir.toPath()); 70 Files.delete(layer2.toPath()); 71 Files.delete(layer1.toPath()); 72 } 75 List<File> files = task.getUnsavedLayersFiles(); 76 assertEquals(2, files.size()); 77 assertTrue(files.contains(layer1)); 78 assertTrue(files.contains(layer2)); 79 assertFalse(files.contains(dir)); 73 80 } 74 81 … … 79 86 @Test 80 87 public void testGetNewLayerFile() throws IOException { 88 Files.createDirectories(task.getAutosaveDir()); 81 89 AutosaveLayerInfo info = new AutosaveLayerInfo(new OsmDataLayer(new DataSet(), "layer", null)); 82 90 Calendar cal = Calendar.getInstance(); … … 85 93 Date fixed = cal.getTime(); 86 94 87 List<File> files = new ArrayList<>(); 88 89 try { 90 for (int i = 0; i <= AutosaveTask.PROP_INDEX_LIMIT.get()+1; i++) { 91 // Only retry 2 indexes to avoid 1000*1000 disk operations 92 File f = task.getNewLayerFile(info, fixed, Math.max(0, i-2)); 93 files.add(f); 94 if (i > AutosaveTask.PROP_INDEX_LIMIT.get()) { 95 assertNull(f); 95 for (int i = 0; i <= AutosaveTask.PROP_INDEX_LIMIT.get() + 1; i++) { 96 // Only retry 2 indexes to avoid 1000*1000 disk operations 97 File f = task.getNewLayerFile(info, fixed, Math.max(0, i - 2)); 98 if (i > AutosaveTask.PROP_INDEX_LIMIT.get()) { 99 assertNull(f); 100 } else { 101 assertNotNull(f); 102 File pid = task.getPidFile(f); 103 assertTrue(pid.exists()); 104 assertTrue(f.exists()); 105 if (i == 0) { 106 assertEquals("null_20160101_010203456.osm", f.getName()); 107 assertEquals("null_20160101_010203456.pid", pid.getName()); 96 108 } else { 97 assertNotNull(f); 98 File pid = task.getPidFile(f); 99 assertTrue(pid.exists()); 100 assertTrue(f.exists()); 101 if (i == 0) { 102 assertEquals("null_20160101_010203456.osm", f.getName()); 103 assertEquals("null_20160101_010203456.pid", pid.getName()); 104 } else { 105 assertEquals("null_20160101_010203456_"+i+".osm", f.getName()); 106 assertEquals("null_20160101_010203456_"+i+".pid", pid.getName()); 107 } 109 assertEquals("null_20160101_010203456_" + i + ".osm", f.getName()); 110 assertEquals("null_20160101_010203456_" + i + ".pid", pid.getName()); 108 111 } 109 112 } 110 } finally { 111 for (File f : files) { 112 if (f != null) { 113 Files.delete(task.getPidFile(f).toPath()); 114 Files.delete(f.toPath()); 115 } 113 } 114 } 115 116 /** 117 * Tests if {@link AutosaveTask#schedule()} creates the directories. 118 */ 119 @Test 120 public void testScheduleCreatesDirectories() { 121 try { 122 task.schedule(); 123 assertTrue(task.getAutosaveDir().toFile().isDirectory()); 124 } finally { 125 task.cancel(); 126 } 127 } 128 129 /** 130 * Tests that {@link AutosaveTask#run()} saves every layer 131 */ 132 @Test 133 public void testAutosaveIgnoresUnmodifiedLayer() { 134 OsmDataLayer layer = new OsmDataLayer(new DataSet(), "OsmData", null); 135 Main.getLayerManager().addLayer(layer); 136 try { 137 task.schedule(); 138 assertEquals(0, countFiles()); 139 task.run(); 140 assertEquals(0, countFiles()); 141 } finally { 142 task.cancel(); 143 } 144 } 145 146 private int countFiles() { 147 return task.getAutosaveDir().toFile().list(new FilenameFilter() { 148 @Override 149 public boolean accept(File dir, String name) { 150 return name.endsWith(".osm"); 116 151 } 117 } 152 }).length; 153 } 154 155 /** 156 * Tests that {@link AutosaveTask#run()} saves every layer. 157 */ 158 @Test 159 public void testAutosaveSavesLayer() { 160 runAutosaveTaskSeveralTimes(1); 161 } 162 163 /** 164 * Tests that {@link AutosaveTask#run()} saves every layer. 165 */ 166 @Test 167 public void testAutosaveSavesLayerMultipleTimes() { 168 AutosaveTask.PROP_FILES_PER_LAYER.put(3); 169 runAutosaveTaskSeveralTimes(5); 170 } 171 172 private void runAutosaveTaskSeveralTimes(int times) { 173 DataSet data = new DataSet(); 174 OsmDataLayer layer = new OsmDataLayer(data, "OsmData", null); 175 Main.getLayerManager().addLayer(layer); 176 try { 177 task.schedule(); 178 assertEquals(0, countFiles()); 179 180 for (int i = 0; i < times; i++) { 181 data.addPrimitive(new Node(new LatLon(10, 10))); 182 task.run(); 183 assertEquals(Math.min(i + 1, 3), countFiles()); 184 } 185 186 } finally { 187 task.cancel(); 188 } 189 } 190 191 /** 192 * Tests that {@link AutosaveTask#discardUnsavedLayers()} ignores layers from the current instance 193 * @throws IOException in case of I/O error 194 */ 195 @Test 196 public void testDiscardUnsavedLayersIgnoresCurrentInstance() throws IOException { 197 runAutosaveTaskSeveralTimes(1); 198 try (FileWriter file = new FileWriter(new File(task.getAutosaveDir().toFile(), "any_other_file.osm"))) { 199 file.append(""); 200 } 201 assertEquals(2, countFiles()); 202 203 task.discardUnsavedLayers(); 204 assertEquals(1, countFiles()); 205 } 206 207 /** 208 * Tests that {@link AutosaveTask#run()} handles dupplicate layers 209 * @throws InterruptedException 210 */ 211 @Test 212 public void testAutosaveHandlesDupplicateNames() { 213 DataSet data1 = new DataSet(); 214 OsmDataLayer layer1 = new OsmDataLayer(data1, "OsmData", null); 215 Main.getLayerManager().addLayer(layer1); 216 217 DataSet data2 = new DataSet(); 218 OsmDataLayer layer2 = new OsmDataLayer(data2, "OsmData", null); 219 220 try { 221 task.schedule(); 222 assertEquals(0, countFiles()); 223 // also test adding layer later 224 Main.getLayerManager().addLayer(layer2); 225 226 data1.addPrimitive(new Node(new LatLon(10, 10))); 227 data2.addPrimitive(new Node(new LatLon(10, 10))); 228 task.run(); 229 assertEquals(2, countFiles()); 230 } finally { 231 task.cancel(); 232 } 233 } 234 235 /** 236 * Test that 237 * @throws IOException 238 * @throws ExecutionException 239 * @throws InterruptedException 240 */ 241 @Test 242 public void testRecoverLayers() throws IOException, InterruptedException, ExecutionException { 243 runAutosaveTaskSeveralTimes(1); 244 try (FileWriter file = new FileWriter(new File(task.getAutosaveDir().toFile(), "any_other_file.osm"))) { 245 file.append("<?xml version=\"1.0\"?><osm version=\"0.6\"><node id=\"1\" lat=\"1\" lon=\"2\" version=\"1\"/></osm>"); 246 } 247 248 assertEquals(2, countFiles()); 249 task.recoverUnsavedLayers().get(); 250 251 assertEquals(1, countFiles()); 118 252 } 119 253 }
Note:
See TracChangeset
for help on using the changeset viewer.