-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathUpdaterTest.java
More file actions
157 lines (136 loc) · 5.02 KB
/
UpdaterTest.java
File metadata and controls
157 lines (136 loc) · 5.02 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package nodebox.versioncheck;
import junit.framework.TestCase;
import org.xml.sax.SAXParseException;
import javax.swing.*;
import java.io.FileNotFoundException;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class UpdaterTest extends TestCase {
private MockAppcastServer server;
private Thread serverThread;
@Override
protected void setUp() throws Exception {
server = new MockAppcastServer(MockHost.APPCAST_SERVER_PORT);
serverThread = new Thread(server);
serverThread.start();
}
@Override
protected void tearDown() throws Exception {
server.stop();
serverThread.join();
}
/**
* Test the regular update process.
*/
public void testCheckForUpdates() {
Updater updater = new Updater(new MockHost());
TestUpdateDelegate delegate = checkForUpdates(updater);
assertTrue(delegate.checkPerformed);
assertTrue(delegate.updateAvailable);
assertNull(delegate.throwable);
AppcastItem item = delegate.appcast.getLatest();
assertNotNull(item);
assertEquals(new Version("2.0"), item.getVersion());
assertEquals("Version 2.0", item.getTitle());
assertEquals("2.0 Release Notes.", item.getDescription());
// Months start from zero.
GregorianCalendar c = new GregorianCalendar(2006, 0, 9, 19, 20, 11);
c.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals(c.getTime(), item.getDate());
assertTrue(item.isNewerThan(updater.getHost()));
}
/**
* Test what happens if the appcast file can not be found.
*/
public void testNotFound() {
Updater updater = new Updater(new NotFoundHost());
TestUpdateDelegate delegate = checkForUpdates(updater);
assertFalse(delegate.checkPerformed);
assertEquals(FileNotFoundException.class, delegate.throwable.getClass());
}
/**
* Test what happens if the appcast file can not be parsed.
*/
public void testUnreadableAppcast() {
Updater updater = new Updater(new UnreadableHost());
TestUpdateDelegate delegate = checkForUpdates(updater);
assertFalse(delegate.checkPerformed);
assertEquals(SAXParseException.class, delegate.throwable.getClass());
}
public void testLatestVersion() {
Updater updater = new Updater(new LatestVersionHost());
TestUpdateDelegate delegate = checkForUpdates(updater);
assertTrue(delegate.checkPerformed);
assertFalse(delegate.updateAvailable);
}
private TestUpdateDelegate checkForUpdates(Updater updater) {
TestUpdateDelegate delegate = new TestUpdateDelegate();
updater.setDelegate(delegate);
updater.checkForUpdates(false);
try {
updater.getUpdateChecker().join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// The update checker calls invokeLater on the AWT event dispatch thread.
// We need to make sure this call finishes before we continue our test.
// By using invokeAndWait to place an empty action on the thread, we know
// that the calls will have finished.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
// Do nothing;
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
return delegate;
}
private class TestUpdateDelegate extends UpdateDelegate {
private boolean checkPerformed;
private Appcast appcast;
private Boolean updateAvailable = null;
private Throwable throwable;
@Override
public boolean checkCompleted(UpdateChecker checker, Appcast appcast) {
checkPerformed = true;
return true;
}
@Override
public boolean checkerFoundValidUpdate(UpdateChecker checker, Appcast appcast) {
this.appcast = appcast;
updateAvailable = true;
return true;
}
@Override
public boolean checkerDetectedLatestVersion(UpdateChecker checker, Appcast appcast) {
this.appcast = appcast;
updateAvailable = false;
return true;
}
@Override
public boolean checkerEncounteredError(UpdateChecker checker, Throwable t) {
this.throwable = t;
return true;
}
}
public class NotFoundHost extends MockHost {
@Override
public String getAppcastURL() {
return "http://localhost:" + APPCAST_SERVER_PORT + "/this_file_does_not_exist";
}
}
public class UnreadableHost extends MockHost {
@Override
public String getAppcastURL() {
return "http://localhost:" + APPCAST_SERVER_PORT + "/unreadable_appcast.xml";
}
}
public class LatestVersionHost extends MockHost {
@Override
public Version getVersion() {
return new Version("999");
}
}
}