source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/AutosaveTaskTest.java@ 12790

Last change on this file since 12790 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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