-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObserverTest.java
More file actions
50 lines (38 loc) · 1.34 KB
/
ObserverTest.java
File metadata and controls
50 lines (38 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package behavioral.observer;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
public class ObserverTest {
private Log log;
private ExecutorService executorService;
private List<Observer> observers;
@Before
public void setUp() {
log = new Log();
executorService = Executors.newSingleThreadExecutor();
observers = new ArrayList<>();
}
@Test
public void download_Should_Notify_UserInterface_When_DownloadAsset_IsCompleted() {
Download download = new Download(observers, executorService);
UserInterface userInterface = new UserInterface(log);
download.subscribe(userInterface);
String assetUri = "https://example.org/resource.txt";
download.downloadAsset(assetUri);
executorServiceJoinAll();
assertEquals("Download finished!", log.getMessage());
}
private void executorServiceJoinAll() {
executorService.shutdown();
try {
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}