release 0.9.6
[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() + "+" + 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());
47         }
48         
49
50         @Test
51         public void testUpdateAvailable() throws IOException {
52                 HttpURLConnectionMock connection = setup();
53                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
54                 
55                 String content =
56                         "Version: 6.6.6pre A \n" +
57                         "Extra:  information\n" +
58                         "100:hundred\n" +
59                         "50:  m\u00e4 \n\n" +
60                         "1:     one\n" +
61                         "-2: none";
62                 connection.setContent(content);
63                 
64                 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
65                 retriever.start();
66                 
67                 // Info is null while processing
68                 assertNull(retriever.getUpdateInfo());
69                 
70                 waitfor(retriever);
71                 assertFalse(connection.hasFailed());
72
73                 UpdateInfo info = retriever.getUpdateInfo();
74                 assertNotNull(info);
75
76                 check(connection);
77
78                 assertEquals("6.6.6pre A", info.getLatestVersion());
79
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());
89         }
90         
91         
92         
93
94         @Test
95         public void testUpdateNotAvailable() throws IOException {
96                 HttpURLConnectionMock connection = setup();
97                 connection.setResponseCode(Communicator.UPDATE_INFO_NO_UPDATE_CODE);
98                 
99                 String content =
100                         "Version: 6.6.6pre A \n" +
101                         "Extra:  information\n" +
102                         "100:hundred\n" +
103                         "50:  m\u00e4 \n\n" +
104                         "1:     one\n" +
105                         "-2: none";
106                 connection.setContent(content);
107                 
108                 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
109                 retriever.start();
110                 
111                 // Info is null while processing
112                 assertNull(retriever.getUpdateInfo());
113                 
114                 waitfor(retriever);
115                 assertFalse(connection.hasFailed());
116
117                 UpdateInfo info = retriever.getUpdateInfo();
118                 assertNotNull(info);
119
120                 check(connection);
121
122                 assertEquals(Prefs.getVersion(), info.getLatestVersion());
123                 assertEquals(0, info.getUpdates().size());
124         }
125         
126         
127         
128         @Test
129         public void testInvalidResponses() {
130                 HttpURLConnectionMock connection = setup();
131                 connection.setResponseCode(404);
132                 connection.setContent("Version: 1.2.3");
133
134                 UpdateInfoRetriever retriever = new UpdateInfoRetriever();
135                 retriever.start();
136                 assertNull(retriever.getUpdateInfo());
137                 waitfor(retriever);
138                 assertFalse(connection.hasFailed());
139                 assertNull(retriever.getUpdateInfo());
140                 check(connection);
141
142                 
143                 connection = setup();
144                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
145                 connection.setContentType("text/xml");
146
147                 retriever = new UpdateInfoRetriever();
148                 retriever.start();
149                 assertNull(retriever.getUpdateInfo());
150                 waitfor(retriever);
151                 assertFalse(connection.hasFailed());
152                 assertNull(retriever.getUpdateInfo());
153                 check(connection);
154
155
156
157                 connection = setup();
158                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
159                 String content =
160                         "100:hundred\n" +
161                         "50:  m\u00e4 \n\n" +
162                         "1:     one\n";
163                 connection.setContent(content);
164
165                 retriever = new UpdateInfoRetriever();
166                 retriever.start();
167                 assertNull(retriever.getUpdateInfo());
168                 waitfor(retriever);
169                 assertFalse(connection.hasFailed());
170                 assertNull(retriever.getUpdateInfo());
171                 check(connection);
172
173
174                 connection = setup();
175                 connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
176                 connection.setContent(new byte[0]);
177
178                 retriever = new UpdateInfoRetriever();
179                 retriever.start();
180                 assertNull(retriever.getUpdateInfo());
181                 waitfor(retriever);
182                 assertFalse(connection.hasFailed());
183                 assertNull(retriever.getUpdateInfo());
184                 check(connection);
185                 
186         }
187         
188         @Test
189         public void testRandomInputData() {
190                 
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];
195                         rnd.nextBytes(buf);
196
197                         HttpURLConnectionMock connection = setup();
198                         connection.setResponseCode(Communicator.UPDATE_INFO_UPDATE_AVAILABLE);
199                         connection.setContent(buf);
200
201                         UpdateInfoRetriever retriever = new UpdateInfoRetriever();
202                         retriever.start();
203                         assertNull(retriever.getUpdateInfo());
204                         waitfor(retriever);
205                         assertFalse(connection.hasFailed());
206                         assertNull(retriever.getUpdateInfo());
207                         check(connection);
208                 }
209                 
210         }
211         
212
213         
214         private void waitfor(UpdateInfoRetriever retriever) {
215                 long t = System.currentTimeMillis();
216                 
217                 while (retriever.isRunning()) {
218                         if (System.currentTimeMillis() >= t+ALLOWANCE) {
219                                 fail("retriever took too long to respond");
220                         }
221
222                         try {
223                                 Thread.sleep(10);
224                         } catch (InterruptedException e) { }
225                 }
226                 
227                 System.out.println("Waiting took " + (System.currentTimeMillis()-t) + " ms");
228         }
229
230 }