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 ; 7 import java.io.ByteArrayInputStream ; 8 import java.io.LineNumberReader ; 9 import java.io.InputStreamReader ; 10 11 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 23 { 24 final String [] expected = 25 { 26 }; 27 verify(mCheckConfig, getPath("InputWriteTag.java"), expected); 28 } 29 30 public void testTag() throws Exception 31 { 32 mCheckConfig.addAttribute("tag", "@author"); 33 mCheckConfig.addAttribute("tagFormat", "\\S"); 34 final String [] expected = 35 { 36 "10: @author=Daniel Grenner", 37 }; 38 verify(mCheckConfig, getPath("InputWriteTag.java"), expected); 39 } 40 41 public void testMissingFormat() throws Exception 42 { 43 mCheckConfig.addAttribute("tag", "@author"); 44 final String [] expected = 45 { 46 "10: @author=Daniel Grenner", 47 }; 48 verify(mCheckConfig, getPath("InputWriteTag.java"), expected); 49 } 50 51 public void testTagSeverity() throws Exception 52 { 53 mCheckConfig.addAttribute("tag", "@incomplete"); 54 mCheckConfig.addAttribute("tagFormat", "\\S"); 55 mCheckConfig.addAttribute("tagSeverity", "warning"); 56 final String [] 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 64 { 65 mCheckConfig.addAttribute("tag", "@doubletag"); 66 mCheckConfig.addAttribute("tagFormat", "\\S"); 67 final String [] 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 77 { 78 mCheckConfig.addAttribute("tag", "@missingtag"); 79 final String [] 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 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 [] expected = 94 { 95 "22: @todo=Add a comment", 96 }; 97 verify(mCheckConfig, getPath("InputWriteTag.java"), expected); 98 } 99 100 public void testSeverity() throws Exception 101 { 102 mCheckConfig.addAttribute("tag", "@author"); 103 mCheckConfig.addAttribute("tagFormat", "\\S"); 104 mCheckConfig.addAttribute("severity", "ignore"); 105 final String [] expected = 106 { 107 "10: @author=Daniel Grenner", 108 }; 109 verify(mCheckConfig, getPath("InputWriteTag.java"), expected); 110 } 111 112 public void testIgnoreMissing() throws Exception 113 { 114 mCheckConfig.addAttribute("tag", "@todo2"); 115 mCheckConfig.addAttribute("tagFormat", "\\S"); 116 mCheckConfig.addAttribute("severity", "ignore"); 117 final String [] expected = 118 { 119 }; 120 verify(mCheckConfig, getPath("InputWriteTag.java"), expected); 121 } 122 123 public void testRegularEx() 124 throws Exception 125 { 126 mCheckConfig.addAttribute("tag", "@author"); 127 mCheckConfig.addAttribute("tagFormat", "0*"); 128 final String [] expected = { 129 }; 130 verify(mCheckConfig, getPath("InputWriteTag.java"), expected); 131 } 132 133 public void testRegularExError() 134 throws Exception 135 { 136 mCheckConfig.addAttribute("tag", "@author"); 137 mCheckConfig.addAttribute("tagFormat", "ABC"); 138 final String [] 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 [] aProcessedFiles, 146 String aMessageFileName, 147 String [] aExpected) 148 throws Exception 149 { 150 mStream.flush(); 151 final int errs = aC.process(aProcessedFiles); 152 153 final ByteArrayInputStream bais = 155 new ByteArrayInputStream (mBAOS.toByteArray()); 156 final LineNumberReader lnr = 157 new LineNumberReader (new InputStreamReader (bais)); 158 159 for (int i = 0; i < aExpected.length; i++) { 160 final String expected = aMessageFileName + ":" + aExpected[i]; 161 final String 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 |