updates for 0.9.4
[debian/openrocket] / test / net / sf / openrocket / communication / CommunicationTest.java
1 package net.sf.openrocket.communication;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.io.StringReader;
7 import java.util.Random;
8
9 import org.junit.Test;
10
11 public class CommunicationTest {
12
13         @Test
14         public void testIllegalInputUpdateParsing() throws IOException {
15                 
16                 UpdateInfo info;
17                 
18                 info = Communication.parseUpdateInput(new StringReader(""));
19                 assertNull(info);
20                 
21                 info = Communication.parseUpdateInput(new StringReader("vj\u00e4avdsads"));
22                 assertNull(info);
23                 
24                 info = Communication.parseUpdateInput(new StringReader("\u0000\u0001\u0002"));
25                 assertNull(info);
26                 
27                 info = Communication.parseUpdateInput(new StringReader("Version: 1.2"));
28                 assertNull(info);
29                 
30                 info = Communication.parseUpdateInput(new StringReader("Version: 1.2pre"));
31                 assertNull(info);
32                 
33                 info = Communication.parseUpdateInput(new StringReader("Version: 1.2.x"));
34                 assertNull(info);
35                 
36                 info = Communication.parseUpdateInput(new StringReader("\u0000\u0001\u0002"));
37                 assertNull(info);
38                 
39                 // Feed random bad input
40                 Random rnd = new Random();
41                 StringBuilder sb = new StringBuilder(10000);
42                 for (int i=0; i<100; i++) {
43                         int length = rnd.nextInt(10000);
44                         sb.delete(0, sb.length());
45                         for (int j=0; j<length; j++) {
46                                 sb.append((char)rnd.nextInt());
47                         }
48                         info = Communication.parseUpdateInput(new StringReader(sb.toString()));
49                         assertNull(info);
50                 }
51                 
52         }
53         
54         
55
56         @Test
57         public void testValidInputUpdateParsing() throws IOException {
58
59                 UpdateInfo info;
60                 
61                 info = Communication.parseUpdateInput(new StringReader("Version: 1.2.3"));
62                 assertNotNull(info);
63                 assertEquals("1.2.3", info.getLatestVersion());
64                 assertEquals(0, info.getUpdates().size());
65                 
66                 info = Communication.parseUpdateInput(new StringReader("Version: 1.2.3pre"));
67                 assertNotNull(info);
68                 assertEquals("1.2.3pre", info.getLatestVersion());
69                 assertEquals(0, info.getUpdates().size());
70                 
71                 info = Communication.parseUpdateInput(new StringReader("Version: 1.2.3-build-3"));
72                 assertNotNull(info);
73                 assertEquals("1.2.3-build-3", info.getLatestVersion());
74                 assertEquals(0, info.getUpdates().size());
75                 
76                 info = Communication.parseUpdateInput(new StringReader("Version: 1.2.3x\n\n"));
77                 assertNotNull(info);
78                 assertEquals("1.2.3x", info.getLatestVersion());
79                 assertEquals(0, info.getUpdates().size());
80                 
81                 info = Communication.parseUpdateInput(new StringReader("Version:1.2.3\nfdsacd\u00e4fdsa"));
82                 assertNotNull(info);
83                 assertEquals("1.2.3", info.getLatestVersion());
84                 assertEquals(0, info.getUpdates().size());
85                 
86                 info = Communication.parseUpdateInput(new StringReader(
87                                 "Version:   1.2.3  \n" +
88                                 "15: Fifteen\n" +
89                                 "3: Three1  \r\n" +
90                                 "3: Three2\r" +
91                                 "1:One"));
92                 assertNotNull(info);
93                 assertEquals("1.2.3", info.getLatestVersion());
94                 assertEquals(4, info.getUpdates().size());
95                 assertEquals(15, info.getUpdates().get(0).getU());
96                 assertEquals(3, info.getUpdates().get(1).getU());
97                 assertEquals(3, info.getUpdates().get(2).getU());
98                 assertEquals(1, info.getUpdates().get(3).getU());
99                 assertEquals("Fifteen", info.getUpdates().get(0).getV());
100                 assertEquals("Three1", info.getUpdates().get(1).getV());
101                 assertEquals("Three2", info.getUpdates().get(2).getV());
102                 assertEquals("One", info.getUpdates().get(3).getV());
103                 
104
105                 info = Communication.parseUpdateInput(new StringReader(
106                                 "Version: 1.2.3\n" +
107                                 "15:   (C) 1234 A&B %23 \\o/  \r\r\n" +
108                                 "5: m\u00e4c\n" +
109                                 "3: Invalid\u0000value\n" +
110                                 "1: One\u0019two"));
111                 assertNotNull(info);
112                 assertEquals("1.2.3", info.getLatestVersion());
113                 assertEquals(1, info.getUpdates().size());
114                 assertEquals(15, info.getUpdates().get(0).getU());
115                 assertEquals("(C) 1234 A&B %23 \\o/", info.getUpdates().get(0).getV());
116                 
117                 
118                 
119         }
120 }