1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 |
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.concurrent.ExecutionException;
|
---|
11 | import java.util.concurrent.Future;
|
---|
12 | import java.util.concurrent.TimeUnit;
|
---|
13 | import java.util.concurrent.TimeoutException;
|
---|
14 |
|
---|
15 | import org.junit.Rule;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.Bounds;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 |
|
---|
22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Unit tests for class {@link PostDownloadHandler}.
|
---|
26 | */
|
---|
27 | public class PostDownloadHandlerTest {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Setup test.
|
---|
31 | */
|
---|
32 | @Rule
|
---|
33 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
34 | public JOSMTestRules test = new JOSMTestRules();
|
---|
35 |
|
---|
36 | private static DownloadTask newTask(final List<Object> errorObjects) {
|
---|
37 | return new DownloadTask() {
|
---|
38 | @Override
|
---|
39 | public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public String getTitle() {
|
---|
45 | return null;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public String[] getPatterns() {
|
---|
50 | return new String[0];
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public List<Object> getErrorObjects() {
|
---|
55 | return errorObjects;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public String getConfirmationMessage(URL url) {
|
---|
60 | return null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
---|
65 | return null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void cancel() {
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public boolean acceptsUrl(String url, boolean isRemotecontrol) {
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public String acceptsDocumentationSummary() {
|
---|
79 | return null;
|
---|
80 | }
|
---|
81 | };
|
---|
82 | }
|
---|
83 |
|
---|
84 | private static Future<Object> newFuture(final String exceptionName) {
|
---|
85 | return new Future<Object>() {
|
---|
86 | @Override
|
---|
87 | public boolean cancel(boolean mayInterruptIfRunning) {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public boolean isCancelled() {
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public boolean isDone() {
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public Object get() throws InterruptedException, ExecutionException {
|
---|
103 | if (exceptionName != null) {
|
---|
104 | throw new ExecutionException(exceptionName, null);
|
---|
105 | }
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public Object get(long timeout, TimeUnit unit)
|
---|
111 | throws InterruptedException, ExecutionException, TimeoutException {
|
---|
112 | return null;
|
---|
113 | }
|
---|
114 | };
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Unit test of {@code PostDownloadHandler#run} - error case: future throws exception.
|
---|
119 | */
|
---|
120 | @Test
|
---|
121 | public void testRunExceptionFuture() {
|
---|
122 | Main.clearLastErrorAndWarnings();
|
---|
123 | new PostDownloadHandler(null, newFuture("testRunExceptionFuture")).run();
|
---|
124 | assertTrue(Main.getLastErrorAndWarnings().toString(),
|
---|
125 | Main.getLastErrorAndWarnings().contains("E: java.util.concurrent.ExecutionException: testRunExceptionFuture"));
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Unit test of {@code PostDownloadHandler#run} - nominal case: no errors.
|
---|
130 | */
|
---|
131 | @Test
|
---|
132 | public void testRunNoError() {
|
---|
133 | Main.clearLastErrorAndWarnings();
|
---|
134 | new PostDownloadHandler(newTask(Collections.emptyList()), newFuture(null)).run();
|
---|
135 | assertTrue(Main.getLastErrorAndWarnings().toString(), Main.getLastErrorAndWarnings().isEmpty());
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Unit test of {@code PostDownloadHandler#run} - nominal case: only one error.
|
---|
140 | */
|
---|
141 | @Test
|
---|
142 | public void testRunOneError() {
|
---|
143 | Main.clearLastErrorAndWarnings();
|
---|
144 | new PostDownloadHandler(newTask(Collections.singletonList(new Object())), newFuture(null)).run();
|
---|
145 | assertTrue(Main.getLastErrorAndWarnings().toString(), Main.getLastErrorAndWarnings().isEmpty());
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Unit test of {@code PostDownloadHandler#run} - nominal case: multiple errors.
|
---|
150 | */
|
---|
151 | @Test
|
---|
152 | public void testRunMultipleErrors() {
|
---|
153 | Main.clearLastErrorAndWarnings();
|
---|
154 | new PostDownloadHandler(newTask(Arrays.asList("foo", new Exception("bar"), new Object())), newFuture(null)).run();
|
---|
155 | assertTrue(Main.getLastErrorAndWarnings().toString(),
|
---|
156 | Main.getLastErrorAndWarnings().contains("E: java.lang.Exception: bar"));
|
---|
157 | }
|
---|
158 | }
|
---|