View Javadoc

1   package org.apache.torque.generator.template.velocity;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      * Filters the input <code>input</code> and checks whether the output
106      * of the filter is equal to <code>expected</code>.
107      *
108      * @param expected The expected outcome.
109      * @param input the input for the filter.
110      *
111      * @throws IOException If an IOException occurs during Streaming.
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      * Reads the content of a Stream into a String.
125      *
126      * @param stream the stream to read, not null.
127      * @param encoding The encoding to use.
128      *
129      * @return the Stream as String.
130      *
131      * @throws IOException If an IO Error occurs during reading.
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 }