View Javadoc

1   package org.apache.torque.generator.outlet.java;
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  import static org.junit.Assert.assertTrue;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.torque.generator.configuration.mergepoint.MergepointMapping;
29  import org.apache.torque.generator.control.ControllerState;
30  import org.apache.torque.generator.control.action.MergepointAction;
31  import org.apache.torque.generator.control.action.OutputAction;
32  import org.apache.torque.generator.outlet.OutletResult;
33  import org.apache.torque.generator.qname.QualifiedName;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  public class JavadocOutletTest
38  {
39      private JavadocOutlet javadocOutlet;
40  
41      @Before
42      public void setUp()
43      {
44          javadocOutlet = new JavadocOutlet(new QualifiedName("jacadocOutlet"));
45          javadocOutlet.setMaxLineLength(20);
46      }
47  
48      /**
49       * Tests that the javadoc contains both the body and the attributes
50       * and both are properly line wrapped.
51       */
52      @Test
53      public void testCompleteJavadoc() throws Exception
54      {
55          List<MergepointAction> mergepointActions
56                  = new ArrayList<MergepointAction>();
57          mergepointActions.add(new OutputAction("Test-body for.a,javadoc"));
58          javadocOutlet.setMergepointMapping(
59                  new MergepointMapping(
60                          "body",
61                          mergepointActions));
62          mergepointActions = new ArrayList<MergepointAction>();
63          mergepointActions.add(
64                  new OutputAction("@param param1 description,of param1"));
65          javadocOutlet.setMergepointMapping(
66                  new MergepointMapping(
67                          "attributes",
68                          mergepointActions));
69          OutletResult result = javadocOutlet.execute(new ControllerState());
70          assertTrue(result.isStringResult());
71          assertEquals(
72                      "    /**\n"
73                    + "     * Test-body\n"
74                    + "     * for.a,javadoc\n"
75                    + "     *\n"
76                    + "     * @param param1\n"
77                    + "     *        description,\n"
78                    + "     *        of\n"
79                    + "     *        param1\n"
80                    + "     */\n",
81                result.getStringResult());
82      }
83  
84      /**
85       * Checks that a break-after character can cause a line break , and that
86       * a single space after such a break is removed. The boundary cases
87       * maxLineLength + 1 and maxLineLength are checked.
88       */
89      @Test
90      public void testWrapAfterNotRemovedCharacters()
91      {
92          String result = javadocOutlet.wrapLinesAndIndent(
93                  "Test-body111-breaking.at1.  111111 not;  removed1111,characters");
94          assertEquals(
95                      "     * Test-body111-\n"
96                    + "     * breaking.at1.\n"
97                    + "     *  111111 not; \n"
98                    + "     * removed1111,\n"
99                    + "     * characters\n",
100                 result);
101     }
102 
103     /**
104      * Checks that a space can cause a line break and the space is removed
105      * in the output. The boundary cases maxLineLength + 1 and maxLineLength
106      * are checked.
107      */
108     @Test
109     public void testSpaceAtEndRemoved()
110     {
111         String result = javadocOutlet.wrapLinesAndIndent(
112                 "Test body1111 breaking at1 space");
113         assertEquals(
114                     "     * Test body1111\n"
115                   + "     * breaking at1\n"
116                   + "     * space\n",
117                 result);
118     }
119 
120     /**
121      * Checks that a new javadoc attribute causes a double line wrap
122      */
123     @Test
124     public void testDoubleWrapAtNewAttribute()
125     {
126         String result = javadocOutlet.wrapLinesAndIndent(
127                 "body @since 1 2 3 4 5");
128         assertEquals(
129                     "     * body\n"
130                   + "     *\n"
131                   + "     * @since 1 2 3\n"
132                   + "     *        4 5\n",
133                 result);
134     }
135 
136     /**
137      * Checks that between different javadoc attributes,
138      * a double line wrap is inserted.
139      */
140     @Test
141     public void testDoubleWrapAtDifferentAttributes()
142     {
143         String result = javadocOutlet.wrapLinesAndIndent(
144                 "@since 1 @see y");
145         assertEquals(
146                     "     * @since 1\n"
147                   + "     *\n"
148                   + "     * @see y\n",
149                 result);
150     }
151 
152     /**
153      * Checks that between different javadoc attributes
154      * where the first ends with a line break,
155      * a double line wrap is inserted.
156      */
157     @Test
158     public void testDoubleWrapAtDifferentAttributesWithLineBreak()
159     {
160         String result = javadocOutlet.wrapLinesAndIndent(
161                 "@since 1\n@see y");
162         assertEquals(
163                     "     * @since 1\n"
164                   + "     *\n"
165                   + "     * @see y\n",
166                 result);
167     }
168 
169     /**
170      * Checks that a preceding \n or two same javadoc attribute
171      * cause a single line wrap
172      */
173     @Test
174     public void testSingleWrapAtAttribute()
175     {
176         String result = javadocOutlet.wrapLinesAndIndent(
177                 "body\n@since 11 2 3 4 5 @since x");
178         assertEquals(
179                     "     * body\n"
180                   + "     *\n"
181                   + "     * @since 11 2 3\n"
182                   + "     *        4 5\n"
183                   + "     * @since x\n",
184                 result);
185     }
186 
187     /**
188      * Checks that a preceding \n or two same javadoc attribute
189      * cause a single line wrap
190      */
191     @Test
192     public void testSingleWrapAtAttributeWithLineBreak()
193     {
194         String result = javadocOutlet.wrapLinesAndIndent(
195                 "@since x\n@since y");
196         assertEquals(
197                     "     * @since x\n"
198                   + "     * @since y\n",
199                 result);
200     }
201 
202     /**
203      * Checks that setWrapAfterCharacters works as expected.
204      */
205     @Test
206     public void testSetWrapAfterCharacters()
207     {
208         javadocOutlet.setWrapAfterCharacters(".");
209         String result = javadocOutlet.wrapLinesAndIndent(
210                 "123-456,789;012.345");
211         assertEquals(
212                     "     * 123-456,789;012.\n"
213                   + "     * 345\n",
214                 result);
215     }
216 
217     /**
218      * Checks that setIndent works as expected.
219      */
220     @Test
221     public void testSetIndent()
222     {
223         javadocOutlet.setIndent("        ");
224         String result = javadocOutlet.wrapLinesAndIndent(
225                 "123-456,789;012");
226         assertEquals(
227                     "         * 123-456,\n"
228                   + "         * 789;012\n",
229                 result);
230     }
231 
232     /**
233      * Checks that setLineBreak works as expected.
234      */
235     @Test
236     public void testSetLineBreak()
237     {
238         javadocOutlet.setLineBreak("\r\n");
239         String result = javadocOutlet.wrapLinesAndIndent(
240                 "xxx123-456,789\n012.345");
241         assertEquals(
242                     "     * xxx123-456,\r\n"
243                   + "     * 789\r\n"
244                   + "     * 012.345\r\n",
245                 result);
246     }
247 
248     /**
249      * Checks that setLineBreak works as expected.
250      */
251     @Test(expected = IllegalArgumentException.class)
252     public void testSetLineBreakIllegalInput()
253     {
254         javadocOutlet.setLineBreak("\n\r");
255     }
256 
257     /**
258      * Tests that removeEnd works for an empty String.
259      */
260    @Test
261     public void testRemoveEndEmptyString()
262     {
263         StringBuilder builder = new StringBuilder();
264         JavadocOutlet.removeEnd(builder, " \r\n");
265         assertEquals("", builder.toString());
266     }
267 
268    /**
269     * Tests that removeEnd works if everything is removed.
270     */
271     @Test
272     public void testRemoveEndEverythingRemoved()
273     {
274         StringBuilder builder = new StringBuilder().append(" \r\r\n   \n");
275         JavadocOutlet.removeEnd(builder, " \r\n");
276         assertEquals("", builder.toString());
277     }
278 
279     /**
280      * Tests that removeEnd works if nothing is removed.
281      */
282     @Test
283     public void testRemoveEndNothingRemoved()
284     {
285         StringBuilder builder = new StringBuilder().append(" \r\r\n   \na");
286         JavadocOutlet.removeEnd(builder, " \r\n");
287         assertEquals(" \r\r\n   \na", builder.toString());
288     }
289 
290     /**
291      * Tests that removeEnd works if a part is removed.
292      */
293     @Test
294     public void testRemoveEndPartRemoved()
295     {
296         StringBuilder builder = new StringBuilder().append(" \r\r\n x \r \n");
297         JavadocOutlet.removeEnd(builder, " \r\n");
298         assertEquals(" \r\r\n x", builder.toString());
299     }
300 
301     /**
302      * Tests that removeEnd works if everything but the first character
303      * is removed.
304      */
305     @Test
306     public void testRemoveEndAllButFirstCharacterRemoved()
307     {
308         StringBuilder builder = new StringBuilder().append("x\r\r\n  \r \n");
309         JavadocOutlet.removeEnd(builder, " \r\n");
310         assertEquals("x", builder.toString());
311     }
312 
313     /**
314      * Tests that removeEnd works if only the last character is removed.
315      */
316     @Test
317     public void testRemoveEndOnlyLastCharacterRemoved()
318     {
319         StringBuilder builder = new StringBuilder().append("\r\r\n  \rx ");
320         JavadocOutlet.removeEnd(builder, " \r\n");
321         assertEquals("\r\r\n  \rx", builder.toString());
322     }
323 }