1 package net.sf.openrocket.communication;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.util.Collections;
8 import java.util.Random;
10 import net.sf.openrocket.util.ComparablePair;
11 import net.sf.openrocket.util.Prefs;
13 import org.junit.Test;
15 public class UpdateInfoTest {
17 /** The connection delay */
18 private static final int DELAY = 100;
20 /** How much long does the test allow it to take */
21 private static final int ALLOWANCE = 2000;
24 private HttpURLConnectionMock setup() {
25 HttpURLConnectionMock connection = new HttpURLConnectionMock();
26 Communicator.setConnectionSource(new ConnectionSourceStub(connection));
28 connection.setConnectionDelay(DELAY);
29 connection.setUseCaches(true);
30 connection.setContentType("text/plain");
34 private void check(HttpURLConnectionMock connection) {
35 assertEquals(Communicator.UPDATE_INFO_URL + "?version=" + Prefs.getVersion(),
36 connection.getTrueUrl());
37 assertTrue(connection.getConnectTimeout() > 0);
38 assertEquals(Prefs.getVersion(), connection.getRequestProperty("X-OpenRocket-Version"));
39 assertNotNull(connection.getRequestProperty("X-OpenRocket-Country"));
40 assertNotNull(connection.getRequestProperty("X-OpenRocket-ID"));
41 assertNotNull(connection.getRequestProperty("X-OpenRocket-OS"));
42 assertNotNull(connection.getRequestProperty("X-OpenRocket-Java"));
43 assertTrue(connection.getInstanceFollowRedirects());
44 assertEquals("GET", connection.getRequestMethod());
45 assertFalse(connection.getUseCaches());
50 public void testUpdateAvailable() throws IOException {
51 HttpURLConnectionMock connection = setup();
52 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
55 "Version: 6.6.6pre A \n" +
56 "Extra: information\n" +
61 connection.setContent(content);
63 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
66 // Info is null while processing
67 assertNull(retriever.getUpdateInfo());
70 assertFalse(connection.hasFailed());
72 UpdateInfo info = retriever.getUpdateInfo();
77 assertEquals("6.6.6pre A", info.getLatestVersion());
79 List<ComparablePair<Integer, String>> updates = info.getUpdates();
80 assertEquals(3, updates.size());
81 Collections.sort(updates);
82 assertEquals(1, (int)updates.get(0).getU());
83 assertEquals("one", updates.get(0).getV());
84 assertEquals(50, (int)updates.get(1).getU());
85 assertEquals("m\u00e4", updates.get(1).getV());
86 assertEquals(100, (int)updates.get(2).getU());
87 assertEquals("hundred", updates.get(2).getV());
94 public void testUpdateNotAvailable() throws IOException {
95 HttpURLConnectionMock connection = setup();
96 connection.setResponseCode(Communicator.UPDATE_INFO_NO_UPDATE_CODE);
99 "Version: 6.6.6pre A \n" +
100 "Extra: information\n" +
105 connection.setContent(content);
107 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
110 // Info is null while processing
111 assertNull(retriever.getUpdateInfo());
114 assertFalse(connection.hasFailed());
116 UpdateInfo info = retriever.getUpdateInfo();
121 assertEquals(Prefs.getVersion(), info.getLatestVersion());
122 assertEquals(0, info.getUpdates().size());
128 public void testInvalidResponses() {
129 HttpURLConnectionMock connection = setup();
130 connection.setResponseCode(404);
131 connection.setContent("Version: 1.2.3");
133 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
135 assertNull(retriever.getUpdateInfo());
137 assertFalse(connection.hasFailed());
138 assertNull(retriever.getUpdateInfo());
142 connection = setup();
143 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
144 connection.setContentType("text/xml");
146 retriever = new UpdateInfoRetriever();
148 assertNull(retriever.getUpdateInfo());
150 assertFalse(connection.hasFailed());
151 assertNull(retriever.getUpdateInfo());
156 connection = setup();
157 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
162 connection.setContent(content);
164 retriever = new UpdateInfoRetriever();
166 assertNull(retriever.getUpdateInfo());
168 assertFalse(connection.hasFailed());
169 assertNull(retriever.getUpdateInfo());
173 connection = setup();
174 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
175 connection.setContent(new byte[0]);
177 retriever = new UpdateInfoRetriever();
179 assertNull(retriever.getUpdateInfo());
181 assertFalse(connection.hasFailed());
182 assertNull(retriever.getUpdateInfo());
188 public void testRandomInputData() {
190 Random rnd = new Random();
191 for (int i=0; i<10; i++) {
192 int size = (int) ((1 + 0.3 * rnd.nextGaussian()) * Math.pow(i, 6));
193 byte[] buf = new byte[size];
196 HttpURLConnectionMock connection = setup();
197 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
198 connection.setContent(buf);
200 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
202 assertNull(retriever.getUpdateInfo());
204 assertFalse(connection.hasFailed());
205 assertNull(retriever.getUpdateInfo());
213 private void waitfor(UpdateInfoRetriever retriever) {
214 long t = System.currentTimeMillis();
216 while (retriever.isRunning()) {
217 if (System.currentTimeMillis() >= t+ALLOWANCE) {
218 fail("retriever took too long to respond");
223 } catch (InterruptedException e) { }
226 System.out.println("Waiting took " + (System.currentTimeMillis()-t) + " ms");