Added unit tests for ImportControlCheck.
This commit is contained in:
parent
a0a11388c6
commit
dc3fa902da
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE import-control PUBLIC
|
||||
"-//Puppy Crawl//DTD Import Control 1.0//EN"
|
||||
"http://www.puppycrawl.com/dtds/import_control_1_0.dtd">
|
||||
|
||||
<import-control>
|
||||
</import-control>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE import-control PUBLIC
|
||||
"-//Puppy Crawl//DTD Import Control 1.0//EN"
|
||||
"http://www.puppycrawl.com/dtds/import_control_1_0.dtd">
|
||||
|
||||
<import-control pkg="com.puppycrawl.tools.checkstyle">
|
||||
<allow class="java.awt.Image"/>
|
||||
<allow class="java.io.File" local-only="selected"/>
|
||||
<subpackage name="imports">
|
||||
<allow pkg="javax.swing"/>
|
||||
</subpackage>
|
||||
</import-control>
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE import-control PUBLIC
|
||||
"-//Puppy Crawl//DTD Import Control 1.0//EN"
|
||||
"http://www.puppycrawl.com/dtds/import_control_1_0.dtd">
|
||||
|
||||
<import-control pkg="com.puppycrawl.tools.checkstyle">
|
||||
<allow class="java.awt.Image"/>
|
||||
<allow class="java.io.File" local-only="selected"/>
|
||||
<subpackage name="imports">
|
||||
<disallow class="java.awt.Image" local-only="selected"/>
|
||||
<allow pkg="javax.swing" exact-match="selected"/>
|
||||
<allow pkg="java.io" exact-match="selected" local-only="selected"/>
|
||||
</subpackage>
|
||||
</import-control>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.puppycrawl.tools.checkstyle.imports;
|
||||
|
||||
import java.awt.Image;
|
||||
import javax.swing.border.*;
|
||||
import java.io.File;
|
||||
|
||||
public class InputImportControl
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
package com.puppycrawl.tools.checkstyle.checks.imports;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
|
||||
import java.io.File;
|
||||
|
||||
public class ImportControlCheckTest extends BaseCheckTestCase
|
||||
{
|
||||
public void testOne() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
|
||||
checkConfig.addAttribute("file", System.getProperty("testinputs.dir")
|
||||
+ "/import-control_one.xml");
|
||||
final String[] expected = {"5:1: Disallowed import - java.io.File."};
|
||||
|
||||
verify(checkConfig, getPath("imports" + File.separator
|
||||
+ "InputImportControl.java"), expected);
|
||||
}
|
||||
|
||||
public void testTwo() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
|
||||
checkConfig.addAttribute("file", System.getProperty("testinputs.dir")
|
||||
+ "/import-control_two.xml");
|
||||
final String[] expected = {"3:1: Disallowed import - java.awt.Image.",
|
||||
"4:1: Disallowed import - javax.swing.border.*."};
|
||||
|
||||
verify(checkConfig, getPath("imports" + File.separator
|
||||
+ "InputImportControl.java"), expected);
|
||||
}
|
||||
|
||||
public void testMissing() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
|
||||
final String[] expected = {"1:40: Missing an import control file."};
|
||||
verify(checkConfig, getPath("imports" + File.separator
|
||||
+ "InputImportControl.java"), expected);
|
||||
}
|
||||
|
||||
public void testEmpty() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
|
||||
checkConfig.addAttribute("file", " ");
|
||||
final String[] expected = {"1:40: Missing an import control file."};
|
||||
verify(checkConfig, getPath("imports" + File.separator
|
||||
+ "InputImportControl.java"), expected);
|
||||
}
|
||||
|
||||
public void testWrong() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
|
||||
checkConfig.addAttribute("file", "unknown-file");
|
||||
final String[] expected = {};
|
||||
try {
|
||||
verify(checkConfig, getPath("imports" + File.separator
|
||||
+ "InputImportControl.java"), expected);
|
||||
fail("should fail");
|
||||
}
|
||||
catch (CheckstyleException ex) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public void testBroken() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class);
|
||||
checkConfig.addAttribute("file", System.getProperty("testinputs.dir")
|
||||
+ "/import-control_broken.xml");
|
||||
final String[] expected = {};
|
||||
try {
|
||||
verify(checkConfig, getPath("imports" + File.separator
|
||||
+ "InputImportControl.java"), expected);
|
||||
fail("should fail");
|
||||
}
|
||||
catch (CheckstyleException ex) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue