updates for 0.9.4
[debian/openrocket] / test / net / sf / openrocket / communication / UpdateInfoTest.java
1 package net.sf.openrocket.communication;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.util.Collections;
7 import java.util.List;
8 import java.util.Random;
9
10 import net.sf.openrocket.util.ComparablePair;
11 import net.sf.openrocket.util.Prefs;
12
13 import org.junit.Test;
14
15 public class UpdateInfoTest {
16         
17         /** The connection delay */
18         private static final int DELAY = 100;
19         
20         /** How much long does the test allow it to take */
21         private static final int ALLOWANCE = 2000;
22
23         
24         private HttpURLConnectionMock setup() {
25                 HttpURLConnectionMock connection = new HttpURLConnectionMock();
26                 Communicator.setConnectionSource(new ConnectionSourceStub(connection));
27                 
28                 connection.setConnectionDelay(DELAY);
29                 connection.setUseCaches(true);
30                 connection.setContentType("text/plain");
31                 return connection;
32         }
33         
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());
46         }
47         
48
49         @Test
50         public void testUpdateAvailable() throws IOException {
51                 HttpURLConnectionMock connection = setup();
52                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
53                 
54                 String content =
55                         "Version: 6.6.6pre A \n" +
56                         "Extra:  information\n" +
57                         "100:hundred\n" +
58                         "50:  m\u00e4 \n\n" +
59                         "1:     one\n" +
60                         "-2: none";
61                 connection.setContent(content);
62                 
63                 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
64                 retriever.start();
65                 
66                 // Info is null while processing
67                 assertNull(retriever.getUpdateInfo());
68                 
69                 waitfor(retriever);
70                 assertFalse(connection.hasFailed());
71
72                 UpdateInfo info = retriever.getUpdateInfo();
73                 assertNotNull(info);
74
75                 check(connection);
76
77                 assertEquals("6.6.6pre A", info.getLatestVersion());
78
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());
88         }
89         
90         
91         
92
93         @Test
94         public void testUpdateNotAvailable() throws IOException {
95                 HttpURLConnectionMock connection = setup();
96                 connection.setResponseCode(Communicator.UPDATE_INFO_NO_UPDATE_CODE);
97                 
98                 String content =
99                         "Version: 6.6.6pre A \n" +
100                         "Extra:  information\n" +
101                         "100:hundred\n" +
102                         "50:  m\u00e4 \n\n" +
103                         "1:     one\n" +
104                         "-2: none";
105                 connection.setContent(content);
106                 
107                 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
108                 retriever.start();
109                 
110                 // Info is null while processing
111                 assertNull(retriever.getUpdateInfo());
112                 
113                 waitfor(retriever);
114                 assertFalse(connection.hasFailed());
115
116                 UpdateInfo info = retriever.getUpdateInfo();
117                 assertNotNull(info);
118
119                 check(connection);
120
121                 assertEquals(Prefs.getVersion(), info.getLatestVersion());
122                 assertEquals(0, info.getUpdates().size());
123         }
124         
125         
126         
127         @Test
128         public void testInvalidResponses() {
129                 HttpURLConnectionMock connection = setup();
130                 connection.setResponseCode(404);
131                 connection.setContent("Version: 1.2.3");
132
133                 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
134                 retriever.start();
135                 assertNull(retriever.getUpdateInfo());
136                 waitfor(retriever);
137                 assertFalse(connection.hasFailed());
138                 assertNull(retriever.getUpdateInfo());
139                 check(connection);
140
141                 
142                 connection = setup();
143                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
144                 connection.setContentType("text/xml");
145
146                 retriever = new UpdateInfoRetriever();
147                 retriever.start();
148                 assertNull(retriever.getUpdateInfo());
149                 waitfor(retriever);
150                 assertFalse(connection.hasFailed());
151                 assertNull(retriever.getUpdateInfo());
152                 check(connection);
153
154
155
156                 connection = setup();
157                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
158                 String content =
159                         "100:hundred\n" +
160                         "50:  m\u00e4 \n\n" +
161                         "1:     one\n";
162                 connection.setContent(content);
163
164                 retriever = new UpdateInfoRetriever();
165                 retriever.start();
166                 assertNull(retriever.getUpdateInfo());
167                 waitfor(retriever);
168                 assertFalse(connection.hasFailed());
169                 assertNull(retriever.getUpdateInfo());
170                 check(connection);
171
172
173                 connection = setup();
174                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
175                 connection.setContent(new byte[0]);
176
177                 retriever = new UpdateInfoRetriever();
178                 retriever.start();
179                 assertNull(retriever.getUpdateInfo());
180                 waitfor(retriever);
181                 assertFalse(connection.hasFailed());
182                 assertNull(retriever.getUpdateInfo());
183                 check(connection);
184                 
185         }
186         
187         @Test
188         public void testRandomInputData() {
189                 
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];
194                         rnd.nextBytes(buf);
195
196                         HttpURLConnectionMock connection = setup();
197                         connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
198                         connection.setContent(buf);
199
200                         UpdateInfoRetriever retriever = new UpdateInfoRetriever();
201                         retriever.start();
202                         assertNull(retriever.getUpdateInfo());
203                         waitfor(retriever);
204                         assertFalse(connection.hasFailed());
205                         assertNull(retriever.getUpdateInfo());
206                         check(connection);
207                 }
208                 
209         }
210         
211
212         
213         private void waitfor(UpdateInfoRetriever retriever) {
214                 long t = System.currentTimeMillis();
215                 
216                 while (retriever.isRunning()) {
217                         if (System.currentTimeMillis() >= t+ALLOWANCE) {
218                                 fail("retriever took too long to respond");
219                         }
220
221                         try {
222                                 Thread.sleep(10);
223                         } catch (InterruptedException e) { }
224                 }
225                 
226                 System.out.println("Waiting took " + (System.currentTimeMillis()-t) + " ms");
227         }
228
229 }