01: package com.puppycrawl.tools.checkstyle.checks.imports;
02:
03: import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
04: import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
05: import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
06: import java.io.File;
07:
08: public class ImportControlCheckTest extends BaseCheckTestCase {
09: public void testOne() throws Exception {
10: final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
11: checkConfig.addAttribute("file", System
12: .getProperty("testinputs.dir")
13: + "/import-control_one.xml");
14: final String[] expected = { "5:1: Disallowed import - java.io.File." };
15:
16: verify(checkConfig, getPath("imports" + File.separator
17: + "InputImportControl.java"), expected);
18: }
19:
20: public void testTwo() throws Exception {
21: final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
22: checkConfig.addAttribute("file", System
23: .getProperty("testinputs.dir")
24: + "/import-control_two.xml");
25: final String[] expected = {
26: "3:1: Disallowed import - java.awt.Image.",
27: "4:1: Disallowed import - javax.swing.border.*.",
28: "6:1: Disallowed import - java.awt.Button.ABORT." };
29:
30: verify(checkConfig, getPath("imports" + File.separator
31: + "InputImportControl.java"), expected);
32: }
33:
34: public void testWrong() throws Exception {
35: final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
36: checkConfig.addAttribute("file", System
37: .getProperty("testinputs.dir")
38: + "/import-control_wrong.xml");
39: final String[] expected = { "1:40: Import control file does not handle this package." };
40:
41: verify(checkConfig, getPath("imports" + File.separator
42: + "InputImportControl.java"), expected);
43: }
44:
45: public void testMissing() throws Exception {
46: final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
47: final String[] expected = { "1:40: Missing an import control file." };
48: verify(checkConfig, getPath("imports" + File.separator
49: + "InputImportControl.java"), expected);
50: }
51:
52: public void testEmpty() throws Exception {
53: final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
54: checkConfig.addAttribute("file", " ");
55: final String[] expected = { "1:40: Missing an import control file." };
56: verify(checkConfig, getPath("imports" + File.separator
57: + "InputImportControl.java"), expected);
58: }
59:
60: public void testUnknown() throws Exception {
61: final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
62: checkConfig.addAttribute("file", "unknown-file");
63: final String[] expected = {};
64: try {
65: verify(checkConfig, getPath("imports" + File.separator
66: + "InputImportControl.java"), expected);
67: fail("should fail");
68: } catch (CheckstyleException ex) {
69: ;
70: }
71: }
72:
73: public void testBroken() throws Exception {
74: final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
75: checkConfig.addAttribute("file", System
76: .getProperty("testinputs.dir")
77: + "/import-control_broken.xml");
78: final String[] expected = {};
79: try {
80: verify(checkConfig, getPath("imports" + File.separator
81: + "InputImportControl.java"), expected);
82: fail("should fail");
83: } catch (CheckstyleException ex) {
84: ;
85: }
86: }
87: }
|