KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > javadoc > WriteTagCheckTest


1 package com.puppycrawl.tools.checkstyle.checks.javadoc;
2
3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
4 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
5 import com.puppycrawl.tools.checkstyle.Checker;
6 import java.io.File JavaDoc;
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.LineNumberReader JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10
11 /**
12  * @author Daniel Grenner
13  */

14 public class WriteTagCheckTest extends BaseCheckTestCase
15 {
16     private DefaultConfiguration mCheckConfig;
17     
18     public void setUp() {
19         mCheckConfig = createCheckConfig(WriteTagCheck.class);
20     }
21
22     public void testDefaultSettings() throws Exception JavaDoc
23     {
24         final String JavaDoc[] expected =
25         {
26         };
27         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
28     }
29
30     public void testTag() throws Exception JavaDoc
31     {
32         mCheckConfig.addAttribute("tag", "@author");
33         mCheckConfig.addAttribute("tagFormat", "\\S");
34         final String JavaDoc[] expected =
35         {
36             "10: @author=Daniel Grenner",
37         };
38         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
39     }
40
41     public void testMissingFormat() throws Exception JavaDoc
42     {
43         mCheckConfig.addAttribute("tag", "@author");
44         final String JavaDoc[] expected =
45         {
46             "10: @author=Daniel Grenner",
47         };
48         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
49     }
50
51     public void testTagSeverity() throws Exception JavaDoc
52     {
53         mCheckConfig.addAttribute("tag", "@incomplete");
54         mCheckConfig.addAttribute("tagFormat", "\\S");
55         mCheckConfig.addAttribute("tagSeverity", "warning");
56         final String JavaDoc[] expected =
57         {
58             "11: warning: @incomplete=This class needs more code...",
59         };
60         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
61     }
62
63     public void testDoubleTag() throws Exception JavaDoc
64     {
65       mCheckConfig.addAttribute("tag", "@doubletag");
66       mCheckConfig.addAttribute("tagFormat", "\\S");
67       final String JavaDoc[] expected =
68       {
69           "12: @doubletag=first text",
70           "13: @doubletag=second text",
71       };
72       verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
73     }
74
75
76     public void testMissingTag() throws Exception JavaDoc
77     {
78         mCheckConfig.addAttribute("tag", "@missingtag");
79         final String JavaDoc[] expected =
80         {
81             "15: Type Javadoc comment is missing an @missingtag tag.",
82         };
83         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
84     }
85
86     public void testMethod() throws Exception JavaDoc
87     {
88         mCheckConfig.addAttribute("tag", "@todo");
89         mCheckConfig.addAttribute("tagFormat", "\\S");
90         mCheckConfig.addAttribute("tokens",
91             "INTERFACE_DEF, CLASS_DEF, METHOD_DEF");
92         mCheckConfig.addAttribute("severity", "ignore");
93         final String JavaDoc[] expected =
94         {
95             "22: @todo=Add a comment",
96         };
97         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
98     }
99
100     public void testSeverity() throws Exception JavaDoc
101     {
102         mCheckConfig.addAttribute("tag", "@author");
103         mCheckConfig.addAttribute("tagFormat", "\\S");
104         mCheckConfig.addAttribute("severity", "ignore");
105         final String JavaDoc[] expected =
106         {
107             "10: @author=Daniel Grenner",
108         };
109         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
110     }
111
112     public void testIgnoreMissing() throws Exception JavaDoc
113     {
114         mCheckConfig.addAttribute("tag", "@todo2");
115         mCheckConfig.addAttribute("tagFormat", "\\S");
116         mCheckConfig.addAttribute("severity", "ignore");
117         final String JavaDoc[] expected =
118         {
119         };
120         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
121     }
122
123     public void testRegularEx()
124         throws Exception JavaDoc
125     {
126         mCheckConfig.addAttribute("tag", "@author");
127         mCheckConfig.addAttribute("tagFormat", "0*");
128         final String JavaDoc[] expected = {
129         };
130         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
131     }
132
133     public void testRegularExError()
134         throws Exception JavaDoc
135     {
136         mCheckConfig.addAttribute("tag", "@author");
137         mCheckConfig.addAttribute("tagFormat", "ABC");
138         final String JavaDoc[] expected = {
139             "10: Type Javadoc tag @author must match pattern 'ABC'.",
140         };
141         verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
142     }
143
144     protected void verify(Checker aC,
145                           File JavaDoc[] aProcessedFiles,
146                           String JavaDoc aMessageFileName,
147                           String JavaDoc[] aExpected)
148         throws Exception JavaDoc
149     {
150         mStream.flush();
151         final int errs = aC.process(aProcessedFiles);
152
153         // process each of the lines
154
final ByteArrayInputStream JavaDoc bais =
155             new ByteArrayInputStream JavaDoc(mBAOS.toByteArray());
156         final LineNumberReader JavaDoc lnr =
157             new LineNumberReader JavaDoc(new InputStreamReader JavaDoc(bais));
158
159         for (int i = 0; i < aExpected.length; i++) {
160             final String JavaDoc expected = aMessageFileName + ":" + aExpected[i];
161             final String JavaDoc actual = lnr.readLine();
162             assertEquals("error message " + i, expected, actual);
163         }
164
165         assertTrue("unexpected output: " + lnr.readLine(),
166                    aExpected.length >= errs);
167
168         aC.destroy();
169     }
170 }
171
Popular Tags