1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
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.assertNull;
|
---|
8 | import static org.junit.Assert.assertTrue;
|
---|
9 |
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.FileWriter;
|
---|
12 | import java.io.FilenameFilter;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.nio.file.Files;
|
---|
15 | import java.nio.file.Paths;
|
---|
16 | import java.util.Calendar;
|
---|
17 | import java.util.Date;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.concurrent.ExecutionException;
|
---|
20 |
|
---|
21 | import org.junit.Before;
|
---|
22 | import org.junit.Rule;
|
---|
23 | import org.junit.Test;
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.AutosaveTask.AutosaveLayerInfo;
|
---|
26 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
27 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
29 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
30 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
31 |
|
---|
32 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unit tests for class {@link AutosaveTask}.
|
---|
36 | */
|
---|
37 | public class AutosaveTaskTest {
|
---|
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;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Setup test.
|
---|
49 | * @throws IOException if autosave directory cannot be created
|
---|
50 | */
|
---|
51 | @Before
|
---|
52 | public void setUp() throws IOException {
|
---|
53 | task = new AutosaveTask();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Unit test to {@link AutosaveTask#getUnsavedLayersFiles} - empty case
|
---|
58 | */
|
---|
59 | @Test
|
---|
60 | public void testGetUnsavedLayersFilesEmpty() {
|
---|
61 | assertTrue(task.getUnsavedLayersFiles().isEmpty());
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Unit test to {@link AutosaveTask#getUnsavedLayersFiles} - non empty case
|
---|
66 | * @throws IOException in case of I/O error
|
---|
67 | */
|
---|
68 | @Test
|
---|
69 | public void testGetUnsavedLayersFilesNotEmpty() throws IOException {
|
---|
70 | Files.createDirectories(task.getAutosaveDir());
|
---|
71 | String autodir = task.getAutosaveDir().toString();
|
---|
72 | File layer1 = Files.createFile(Paths.get(autodir, "layer1.osm")).toFile();
|
---|
73 | File layer2 = Files.createFile(Paths.get(autodir, "layer2.osm")).toFile();
|
---|
74 | File dir = Files.createDirectory(Paths.get(autodir, "dir.osm")).toFile();
|
---|
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));
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Unit test to {@link AutosaveTask#getNewLayerFile}
|
---|
84 | * @throws IOException in case of I/O error
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | public void testGetNewLayerFile() throws IOException {
|
---|
88 | Files.createDirectories(task.getAutosaveDir());
|
---|
89 | AutosaveLayerInfo info = new AutosaveLayerInfo(new OsmDataLayer(new DataSet(), "layer", null));
|
---|
90 | Calendar cal = Calendar.getInstance();
|
---|
91 | cal.set(2016, 0, 1, 1, 2, 3);
|
---|
92 | cal.set(Calendar.MILLISECOND, 456);
|
---|
93 | Date fixed = cal.getTime();
|
---|
94 |
|
---|
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());
|
---|
108 | } else {
|
---|
109 | assertEquals("null_20160101_010203456_" + i + ".osm", f.getName());
|
---|
110 | assertEquals("null_20160101_010203456_" + i + ".pid", pid.getName());
|
---|
111 | }
|
---|
112 | }
|
---|
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");
|
---|
151 | }
|
---|
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());
|
---|
252 | }
|
---|
253 | }
|
---|