source: josm/trunk/test/unit/org/openstreetmap/josm/actions/downloadtasks/PostDownloadHandlerTest.java@ 10945

Last change on this file since 10945 was 10945, checked in by Don-vip, 8 years ago

convert more unit tests to JOSMTestRules

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.junit.Assert.assertTrue;
5
6import java.net.URL;
7import java.util.Arrays;
8import java.util.Collections;
9import java.util.List;
10import java.util.concurrent.ExecutionException;
11import java.util.concurrent.Future;
12import java.util.concurrent.TimeUnit;
13import java.util.concurrent.TimeoutException;
14
15import org.junit.Rule;
16import org.junit.Test;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Unit tests for class {@link PostDownloadHandler}.
26 */
27public 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}
Note: See TracBrowser for help on using the repository browser.