create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / communication / BugReportTest.java
1 package net.sf.openrocket.communication;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7
8 import java.io.IOException;
9
10 import net.sf.openrocket.util.BuildProperties;
11
12 import org.junit.Test;
13
14
15 public class BugReportTest {
16         
17         private HttpURLConnectionMock setup() {
18                 HttpURLConnectionMock connection = new HttpURLConnectionMock();
19                 Communicator.setConnectionSource(new ConnectionSourceStub(connection));
20                 
21                 connection.setUseCaches(true);
22                 return connection;
23         }
24         
25         private void check(HttpURLConnectionMock connection) {
26                 assertEquals(Communicator.BUG_REPORT_URL, connection.getTrueUrl());
27                 assertTrue(connection.getConnectTimeout() > 0);
28                 assertEquals(BuildProperties.getVersion(), connection.getRequestProperty("X-OpenRocket-Version"));
29                 assertTrue(connection.getInstanceFollowRedirects());
30                 assertEquals("POST", connection.getRequestMethod());
31                 assertFalse(connection.getUseCaches());
32         }
33         
34
35         @Test
36         public void testBugReportSuccess() throws IOException {
37                 HttpURLConnectionMock connection = setup();
38                 connection.setResponseCode(Communicator.BUG_REPORT_RESPONSE_CODE);
39                 
40                 String message = 
41                         "MyMessage\n"+
42                         "is important\n"+
43                         "h\u00e4h?";
44                 
45                 BugReporter.sendBugReport(message);
46
47                 check(connection);
48                 
49                 String msg = connection.getOutputStreamString();
50                 assertTrue(msg.indexOf("version=" + BuildProperties.getVersion()) >= 0);
51                 assertTrue(msg.indexOf(Communicator.encode(message)) >= 0);
52         }
53         
54
55         @Test
56         public void testBugReportFailure() throws IOException {
57                 HttpURLConnectionMock connection = setup();
58                 connection.setResponseCode(200);
59                 
60                 String message = 
61                         "MyMessage\n"+
62                         "is important\n"+
63                         "h\u00e4h?";
64                 
65                 try {
66                         BugReporter.sendBugReport(message);
67                         fail("Exception did not occur");
68                 } catch (IOException e) {
69                         // Success
70                 }
71
72                 check(connection);
73         }
74         
75 }