1 package org.apache.torque.generator.template.velocity;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertEquals;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.Reader;
29
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class VelocityTemplateFilterTest
34 {
35 private VelocityTemplateFilter velocityTemplateFilter;
36
37 @Before
38 public void setUp()
39 {
40 velocityTemplateFilter = new VelocityTemplateFilter();
41 }
42
43 @Test
44 public void testEmptyInput() throws IOException
45 {
46 assertFilteredEquals("", "");
47 }
48
49 @Test
50 public void testEmptyLines() throws IOException
51 {
52 assertFilteredEquals("\n", "\n");
53 assertFilteredEquals("\r\n", "\r\n");
54 }
55
56 @Test
57 public void testCommentWithoutSpaces() throws IOException
58 {
59 assertFilteredEquals("#Comment", "#Comment");
60 assertFilteredEquals("#Comment\n", "#Comment\n");
61 assertFilteredEquals("#Comment\r\n", "#Comment\r\n");
62 }
63
64 @Test
65 public void testCommentWithSpaces() throws IOException
66 {
67 assertFilteredEquals("# Com ment", "# Com ment");
68 assertFilteredEquals("# Com ment\n", "# Com ment\n");
69 assertFilteredEquals("# Com ment\r\n", "# Com ment\r\n");
70 }
71
72 @Test
73 public void testWhitespaceLine() throws IOException
74 {
75 assertFilteredEquals(" ", " \t");
76 assertFilteredEquals(" \n", " \t\n");
77 assertFilteredEquals(" \r\n", " \t\r\n");
78 }
79
80 @Test
81 public void testLineStartingWithWhitespaceNoComment() throws IOException
82 {
83 assertFilteredEquals(" ab cd", "\t ab\tcd");
84 assertFilteredEquals(" ab cd\n", "\t ab\tcd\n");
85 assertFilteredEquals(" ab cd\r\n", "\t ab\tcd\r\n");
86 }
87
88 @Test
89 public void testLineStartingWithWhitespaceComment() throws IOException
90 {
91 assertFilteredEquals("# ab cd", "\t # ab\tcd");
92 assertFilteredEquals("# ab cd\n", "\t # ab\tcd\n");
93 assertFilteredEquals("# ab cd\r\n", "\t # ab\tcd\r\n");
94 }
95
96 @Test
97 public void testCommentAfterWhitespaceLineSpaces() throws IOException
98 {
99 assertFilteredEquals(" \n#Comment", " \n #Comment");
100 assertFilteredEquals(" \n#Comment\n", " \n #Comment\n");
101 assertFilteredEquals(" \n#Comment\r\n", " \n #Comment\r\n");
102 }
103
104
105
106
107
108
109
110
111
112
113 private void assertFilteredEquals(String expected, String input)
114 throws IOException
115 {
116 InputStream inputStream = velocityTemplateFilter.filter(
117 new ByteArrayInputStream(expected.getBytes("ISO-8859-1")),
118 "ISO-8859-1");
119 String result = readAndCloseStream(inputStream, "ISO-8859-1");
120 assertEquals(expected, result);
121 }
122
123
124
125
126
127
128
129
130
131
132
133 private String readAndCloseStream(InputStream stream, String encoding)
134 throws IOException
135 {
136 Reader reader;
137 if (encoding == null)
138 {
139 reader = new InputStreamReader(stream);
140 }
141 else
142 {
143 reader = new InputStreamReader(stream, encoding);
144 }
145
146 StringBuffer contentBuffer = new StringBuffer();
147 while (true)
148 {
149 char[] charBuffer = new char[8192];
150 int filledChars = reader.read(charBuffer);
151 if (filledChars == -1)
152 {
153 break;
154 }
155 contentBuffer.append(charBuffer, 0, filledChars);
156 }
157 stream.close();
158 return contentBuffer.toString();
159 }
160 }