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() + "+" + Prefs.getBuildSource(),
39 connection.getRequestProperty("X-OpenRocket-Version"));
40 assertNotNull(connection.getRequestProperty("X-OpenRocket-Country"));
41 assertNotNull(connection.getRequestProperty("X-OpenRocket-ID"));
42 assertNotNull(connection.getRequestProperty("X-OpenRocket-OS"));
43 assertNotNull(connection.getRequestProperty("X-OpenRocket-Java"));
44 assertTrue(connection.getInstanceFollowRedirects());
45 assertEquals("GET", connection.getRequestMethod());
46 assertFalse(connection.getUseCaches());
51 public void testUpdateAvailable() throws IOException {
52 HttpURLConnectionMock connection = setup();
53 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
56 "Version: 6.6.6pre A \n" +
57 "Extra: information\n" +
62 connection.setContent(content);
64 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
67 // Info is null while processing
68 assertNull(retriever.getUpdateInfo());
71 assertFalse(connection.hasFailed());
73 UpdateInfo info = retriever.getUpdateInfo();
78 assertEquals("6.6.6pre A", info.getLatestVersion());
80 List<ComparablePair<Integer, String>> updates = info.getUpdates();
81 assertEquals(3, updates.size());
82 Collections.sort(updates);
83 assertEquals(1, (int)updates.get(0).getU());
84 assertEquals("one", updates.get(0).getV());
85 assertEquals(50, (int)updates.get(1).getU());
86 assertEquals("m\u00e4", updates.get(1).getV());
87 assertEquals(100, (int)updates.get(2).getU());
88 assertEquals("hundred", updates.get(2).getV());
95 public void testUpdateNotAvailable() throws IOException {
96 HttpURLConnectionMock connection = setup();
97 connection.setResponseCode(Communicator.UPDATE_INFO_NO_UPDATE_CODE);
100 "Version: 6.6.6pre A \n" +
101 "Extra: information\n" +
106 connection.setContent(content);
108 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
111 // Info is null while processing
112 assertNull(retriever.getUpdateInfo());
115 assertFalse(connection.hasFailed());
117 UpdateInfo info = retriever.getUpdateInfo();
122 assertEquals(Prefs.getVersion(), info.getLatestVersion());
123 assertEquals(0, info.getUpdates().size());
129 public void testInvalidResponses() {
130 HttpURLConnectionMock connection = setup();
131 connection.setResponseCode(404);
132 connection.setContent("Version: 1.2.3");
134 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
136 assertNull(retriever.getUpdateInfo());
138 assertFalse(connection.hasFailed());
139 assertNull(retriever.getUpdateInfo());
143 connection = setup();
144 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
145 connection.setContentType("text/xml");
147 retriever = new UpdateInfoRetriever();
149 assertNull(retriever.getUpdateInfo());
151 assertFalse(connection.hasFailed());
152 assertNull(retriever.getUpdateInfo());
157 connection = setup();
158 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
163 connection.setContent(content);
165 retriever = new UpdateInfoRetriever();
167 assertNull(retriever.getUpdateInfo());
169 assertFalse(connection.hasFailed());
170 assertNull(retriever.getUpdateInfo());
174 connection = setup();
175 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
176 connection.setContent(new byte[0]);
178 retriever = new UpdateInfoRetriever();
180 assertNull(retriever.getUpdateInfo());
182 assertFalse(connection.hasFailed());
183 assertNull(retriever.getUpdateInfo());
189 public void testRandomInputData() {
191 Random rnd = new Random();
192 for (int i=0; i<10; i++) {
193 int size = (int) ((1 + 0.3 * rnd.nextGaussian()) * Math.pow(i, 6));
194 byte[] buf = new byte[size];
197 HttpURLConnectionMock connection = setup();
198 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
199 connection.setContent(buf);
201 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
203 assertNull(retriever.getUpdateInfo());
205 assertFalse(connection.hasFailed());
206 assertNull(retriever.getUpdateInfo());
214 private void waitfor(UpdateInfoRetriever retriever) {
215 long t = System.currentTimeMillis();
217 while (retriever.isRunning()) {
218 if (System.currentTimeMillis() >= t+ALLOWANCE) {
219 fail("retriever took too long to respond");
224 } catch (InterruptedException e) { }
227 System.out.println("Waiting took " + (System.currentTimeMillis()-t) + " ms");