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

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

findbugs - fix/suppress most of warnings reported in unit tests + enable low confidence warnings for core

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