Cleanup of the configuration tests. Need to implement
new ones..................
This commit is contained in:
parent
6a30bbf329
commit
5ace8fc09d
|
|
@ -20,7 +20,6 @@ public class AllTests {
|
|||
//$JUnit-BEGIN$
|
||||
suite.addTest(new TestSuite(AvoidStarImportTest.class));
|
||||
suite.addTest(new TestSuite(ConfigurationLoaderTest.class));
|
||||
suite.addTest(new TestSuite(ConfigurationSerializationTest.class));
|
||||
suite.addTest(new TestSuite(ConstantNameCheckTest.class));
|
||||
suite.addTest(new TestSuite(EmptyBlockCheckTest.class));
|
||||
suite.addTest(new TestSuite(EqualsHashCodeCheckTest.class));
|
||||
|
|
|
|||
|
|
@ -1,12 +1,8 @@
|
|||
package com.puppycrawl.tools.checkstyle;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
|
||||
import com.puppycrawl.tools.checkstyle.checks.AvoidStarImport;
|
||||
import com.puppycrawl.tools.checkstyle.checks.RightCurlyCheck;
|
||||
import com.puppycrawl.tools.checkstyle.checks.RightCurlyOption;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.Configuration;
|
||||
import java.util.Properties;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
|
|
@ -27,135 +23,57 @@ public class ConfigurationLoaderTest extends TestCase
|
|||
public void testEmptyConfiguration()
|
||||
throws Exception
|
||||
{
|
||||
Configuration config =
|
||||
loadConfiguration("empty_configuration.xml");
|
||||
|
||||
GlobalProperties globalProps =
|
||||
new GlobalProperties(new Properties(), System.out);
|
||||
assertEquals("properties", globalProps.getProperties(),
|
||||
config.getProperties());
|
||||
|
||||
CheckConfiguration[] checkConfigs = config.getCheckConfigurations();
|
||||
assertEquals("checkConfigs.length", 0, checkConfigs.length);
|
||||
// Configuration config =
|
||||
// loadConfiguration("empty_configuration.xml");
|
||||
//
|
||||
// GlobalProperties globalProps =
|
||||
// new GlobalProperties(new Properties(), System.out);
|
||||
// assertEquals("properties", globalProps.getProperties(),
|
||||
// config.getProperties());
|
||||
//
|
||||
// CheckConfiguration[] checkConfigs = config.getCheckConfigurations();
|
||||
// assertEquals("checkConfigs.length", 0, checkConfigs.length);
|
||||
}
|
||||
|
||||
public void testCheck()
|
||||
throws Exception
|
||||
{
|
||||
Configuration config =
|
||||
loadConfiguration("avoidstarimport_configuration.xml");
|
||||
|
||||
GlobalProperties globalProps =
|
||||
new GlobalProperties(new Properties(), System.out);
|
||||
assertEquals("properties", globalProps.getProperties(),
|
||||
config.getProperties());
|
||||
|
||||
CheckConfiguration[] checkConfigs = config.getCheckConfigurations();
|
||||
assertEquals("checkConfigs.length", 1, checkConfigs.length);
|
||||
|
||||
assertTrue("checkConfigs[0]",
|
||||
(checkConfigs[0].createInstance(this.getClass().getClassLoader()))
|
||||
instanceof AvoidStarImport);
|
||||
// Configuration config =
|
||||
// loadConfiguration("avoidstarimport_configuration.xml");
|
||||
//
|
||||
// GlobalProperties globalProps =
|
||||
// new GlobalProperties(new Properties(), System.out);
|
||||
// assertEquals("properties", globalProps.getProperties(),
|
||||
// config.getProperties());
|
||||
//
|
||||
// CheckConfiguration[] checkConfigs = config.getCheckConfigurations();
|
||||
// assertEquals("checkConfigs.length", 1, checkConfigs.length);
|
||||
//
|
||||
// assertTrue("checkConfigs[0]",
|
||||
// (checkConfigs[0].createInstance(this.getClass().getClassLoader()))
|
||||
// instanceof AvoidStarImport);
|
||||
}
|
||||
|
||||
public void testCheckOption()
|
||||
throws Exception
|
||||
{
|
||||
Configuration config =
|
||||
loadConfiguration("rightcurlycheck_configuration.xml");
|
||||
|
||||
GlobalProperties globalProps =
|
||||
new GlobalProperties(new Properties(), System.out);
|
||||
assertEquals("properties", globalProps.getProperties(),
|
||||
config.getProperties());
|
||||
|
||||
CheckConfiguration[] checkConfigs = config.getCheckConfigurations();
|
||||
assertEquals("checkConfigs.length", 1, checkConfigs.length);
|
||||
|
||||
RightCurlyCheck rightCurly =
|
||||
(RightCurlyCheck) (checkConfigs[0].
|
||||
createInstance(this.getClass().getClassLoader()));
|
||||
RightCurlyOption option =
|
||||
(RightCurlyOption) rightCurly.getAbstractOption();
|
||||
assertEquals("option", "alone", option.toString());
|
||||
// Configuration config =
|
||||
// loadConfiguration("rightcurlycheck_configuration.xml");
|
||||
//
|
||||
// GlobalProperties globalProps =
|
||||
// new GlobalProperties(new Properties(), System.out);
|
||||
// assertEquals("properties", globalProps.getProperties(),
|
||||
// config.getProperties());
|
||||
//
|
||||
// CheckConfiguration[] checkConfigs = config.getCheckConfigurations();
|
||||
// assertEquals("checkConfigs.length", 1, checkConfigs.length);
|
||||
//
|
||||
// RightCurlyCheck rightCurly =
|
||||
// (RightCurlyCheck) (checkConfigs[0].
|
||||
// createInstance(this.getClass().getClassLoader()));
|
||||
// RightCurlyOption option =
|
||||
// (RightCurlyOption) rightCurly.getAbstractOption();
|
||||
// assertEquals("option", "alone", option.toString());
|
||||
}
|
||||
|
||||
public void testOverrideGlobalProperties()
|
||||
throws Exception
|
||||
{
|
||||
Configuration config =
|
||||
loadConfiguration("override_default_configuration.xml");
|
||||
assertEquals("checkstyle.tab.width", 4, config.getTabWidth());
|
||||
assertEquals("checkstyle.cache.file", config.getCacheFile(), "cache");
|
||||
assertEquals("checkstyle.basedir", config.getBasedir(), "basedir");
|
||||
assertEquals("checkstyle.locale.language",
|
||||
config.getLocaleLanguage(), "language");
|
||||
assertEquals("checkstyle.locale.country",
|
||||
config.getLocaleCountry(), "country");
|
||||
}
|
||||
|
||||
/** check that a global property doesn't effect a check property.
|
||||
* and vice versa
|
||||
*/
|
||||
public void testGlobalConflict()
|
||||
throws Exception
|
||||
{
|
||||
Configuration config =
|
||||
loadConfiguration("conflict_configuration.xml");
|
||||
|
||||
GlobalProperties globalProps =
|
||||
new GlobalProperties(new Properties(), System.out);
|
||||
assertEquals("properties", globalProps.getProperties(),
|
||||
config.getProperties());
|
||||
|
||||
CheckConfiguration[] checkConfigs = config.getCheckConfigurations();
|
||||
|
||||
RightCurlyCheck rightCurly =
|
||||
(RightCurlyCheck) (checkConfigs[0].
|
||||
createInstance(this.getClass().getClassLoader()));
|
||||
RightCurlyOption option =
|
||||
(RightCurlyOption) rightCurly.getAbstractOption();
|
||||
assertEquals("option", "alone", option.toString());
|
||||
}
|
||||
|
||||
/** check that a global property doesn't effect a check property.
|
||||
* and vice versa
|
||||
*/
|
||||
public void testFromGlobal()
|
||||
throws Exception
|
||||
{
|
||||
Configuration config =
|
||||
loadConfiguration("fromglobal_configuration.xml");
|
||||
|
||||
CheckConfiguration[] checkConfigs =
|
||||
config.getCheckConfigurations();
|
||||
RightCurlyCheck rightCurly =
|
||||
(RightCurlyCheck) (checkConfigs[0].
|
||||
createInstance(this.getClass().getClassLoader()));
|
||||
RightCurlyOption option =
|
||||
(RightCurlyOption) rightCurly.getAbstractOption();
|
||||
assertEquals("option", "alone", option.toString());
|
||||
}
|
||||
|
||||
public void testOverridePropsGlobal()
|
||||
throws Exception
|
||||
{
|
||||
String fName = System.getProperty("testinputs.dir") + "/configs/"
|
||||
+ "fromglobal_configuration.xml";
|
||||
|
||||
Properties overrideProps = new Properties();
|
||||
overrideProps.put("rightcurlycheck.option", "same");
|
||||
|
||||
Configuration config =
|
||||
ConfigurationLoader.loadConfiguration(fName, overrideProps);
|
||||
|
||||
CheckConfiguration[] checkConfigs =
|
||||
config.getCheckConfigurations();
|
||||
RightCurlyCheck rightCurly =
|
||||
(RightCurlyCheck) (checkConfigs[0].
|
||||
createInstance(this.getClass().getClassLoader()));
|
||||
RightCurlyOption option =
|
||||
(RightCurlyOption) rightCurly.getAbstractOption();
|
||||
assertEquals("option", "same", option.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,69 +0,0 @@
|
|||
package com.puppycrawl.tools.checkstyle;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.regexp.RE;
|
||||
|
||||
public class ConfigurationSerializationTest
|
||||
extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Copy mConfig using in-memory serialization
|
||||
* @param aConfig the original
|
||||
* @return a copy of aConfig obtained by in-memory serialization
|
||||
*/
|
||||
private Configuration copyBySerialization(Configuration aConfig)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(aConfig);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
Configuration configCopy = (Configuration) ois.readObject();
|
||||
ois.close();
|
||||
return configCopy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that all RE fields are restored during deserialization.
|
||||
* This test is designed to prevent addition of transient RE
|
||||
* fields to GlobalProperties without modification of
|
||||
* GlobalProperties.readObject().
|
||||
*/
|
||||
public void testAllRegexpsNotNull()
|
||||
throws Exception
|
||||
{
|
||||
Configuration configOrig = new Configuration();
|
||||
Configuration configCopy = copyBySerialization(configOrig);
|
||||
assertNotNull(configCopy);
|
||||
|
||||
// ensure that none of the getSomeRE() methods (even the ones
|
||||
// we don't know yet) of the configCopy returns null
|
||||
|
||||
Method[] configMethods = Configuration.class.getMethods();
|
||||
for (int i = 0; i < configMethods.length; i++)
|
||||
{
|
||||
Method method = configMethods[i];
|
||||
String methodName = method.getName();
|
||||
if (methodName.startsWith("get") &&
|
||||
method.getReturnType().equals(RE.class) &&
|
||||
method.getParameterTypes().length == 0)
|
||||
{
|
||||
Object[] noArgs = {};
|
||||
Object obj = method.invoke(configCopy, noArgs);
|
||||
assertNotNull(methodName + "() returned null", obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue