eec671f55829ac0156ee573152ab12b67f3a8606
[frank.git] / test / net / jaekl / frank / ViewScheduleTest.java
1 package net.jaekl.frank;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.PrintWriter;
5 import java.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.Locale;
8
9 import net.jaekl.qd.QDException;
10 import net.jaekl.qd.http.HttpServletRequestMock;
11
12 import org.junit.Assert;
13 import org.junit.Test;
14
15 public class ViewScheduleTest {
16         
17         private HashMap<String, String[]> makeParamMap() {
18                 String[] routeNos = { "127" };
19                 String[] alphas = { "abcdefg" };
20                 String[] kanas = { "あいうえお" };
21
22                 HashMap<String, String[]> paramMap = new HashMap<String, String[]>();
23                 paramMap.put("routeNo", routeNos);
24                 paramMap.put("alpha", alphas);
25                 paramMap.put("kana", kanas);
26                 
27                 return paramMap;
28         }
29         
30         @Test
31         public void testGetParamInt() {
32                 HashMap<String, String[]> paramMap = makeParamMap();
33                 HttpServletRequestMock reqMock = new HttpServletRequestMock(paramMap);
34                 ViewSchedule vs = new ViewSchedule();
35
36                 // Try to get a parameter that is an integer, and is there
37                 int value = vs.getParamInt(reqMock, "routeNo");
38                 Assert.assertEquals(127, value);
39                 
40                 // Try to get a parameter that is not an integer
41                 value = vs.getParamInt(reqMock, "alpha");
42                 Assert.assertEquals(0, value);
43                 
44                 // Try to get a parameter that is not alphanumeric
45                 value = vs.getParamInt(reqMock, "kana");
46                 Assert.assertEquals(0, value);
47                 
48                 // Try to get a parameter that is not present 
49                 value = vs.getParamInt(reqMock, "notPresent");
50                 Assert.assertEquals(0, value);
51         }
52
53         @Test
54         public void testGetParamString() {
55                 HashMap<String, String[]> paramMap = makeParamMap();
56                 HttpServletRequestMock reqMock = new HttpServletRequestMock(paramMap);
57                 ViewSchedule vs = new ViewSchedule();
58
59                 // Try to get a parameter that is an integer, and is there
60                 String value = vs.getParamString(reqMock, "routeNo");
61                 Assert.assertEquals("127", value);
62                 
63                 // Try to get a parameter that is not an integer
64                 value = vs.getParamString(reqMock, "alpha");
65                 Assert.assertEquals("abcdefg", value);
66                 
67                 // Try to get a parameter that is not alphanumeric
68                 value = vs.getParamString(reqMock, "kana");
69                 Assert.assertEquals("あいうえお", value);
70                 
71                 // Try to get a parameter that is not present 
72                 value = vs.getParamString(reqMock, "notPresent");
73                 Assert.assertEquals(null, value);
74         }
75
76         @Test
77         public void testWriteErrorPage() {
78                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
79                 PrintWriter pw = new PrintWriter(baos);
80                 
81                 ViewSchedule vs = new ViewSchedule();
82                 
83                 Throwable[] throwables = { 
84                                 new NullPointerException(),
85                                 new QDException(),
86                                 new SQLException()
87                 };
88                 
89                 for (Throwable t : throwables) {
90                         baos.reset();
91                         vs.writeErrorPage(pw, t, Locale.CANADA);        // TODO:  test translations
92                         pw.flush();
93                         
94                         String actual = baos.toString();
95                         Assert.assertTrue(actual.contains("<TITLE>Frank: Error Page</TITLE>"));
96                         Assert.assertTrue(actual.contains(t.toString()));
97                 }
98         }
99
100 }