Begin refactoring the unit tests for the plugin

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4421 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2006-06-18 22:30:33 +00:00
parent cc57b9d4c0
commit d3f923f863
20 changed files with 2171 additions and 499 deletions

View File

@ -1,4 +1,4 @@
source.pmd_eclipse_test.jar = test/
output.pmd_eclipse_test.jar = bin/
source.pmd-eclipse-test.jar = test/
output.pmd-eclipse-test.jar = bin/
bin.includes = plugin.xml,\
pmd_eclipse_test.jar
pmd-eclipse-test.jar

View File

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
id="test.net.sourceforge.pmd.eclipse"
id="net.sourceforge.pmd.eclipse.test"
name="PMD Eclipse Test Plug-in"
version="3.1.3"
version="3.1.6"
provider-name="PMD Development Team">
<runtime>
<library name="pmd_eclipse_test.jar">
<library name="pmd-eclipse-test.jar">
<export name="*"/>
</library>
</runtime>
@ -20,6 +20,8 @@
<import plugin="org.eclipse.core.resources"/>
<import plugin="org.eclipse.jdt"/>
<import plugin="org.eclipse.jdt.core"/>
<import plugin="net.sourceforge.pmd.runtime"/>
<import plugin="net.sourceforge.pmd.ui"/>
</requires>
</plugin>

View File

@ -6,7 +6,7 @@
<property name="eclipse-home" value="c:/program files/eclipse.3"/>
<!-- sets the properties eclipse-home, and library-file -->
<property name="plugin-name" value="test.net.sourceforge.pmd.eclipse"/>
<property name="plugin-name" value="net.sourceforge.pmd.eclipse"/>
<property name="library-file" value="${eclipse-home}/plugins/org.eclipse.test/library.xml"/>
<!-- This target holds all initialization code that needs to be done for -->
@ -21,7 +21,7 @@
<ant target="core-test" antfile="${library-file}" dir="${eclipse-home}">
<property name="data-dir" value="${jdt-folder}"/>
<property name="plugin-name" value="${plugin-name}"/>
<property name="classname" value="test.net.sourceforge.pmd.eclipse.AllTests"/>
<property name="classname" value="net.sourceforge.pmd.eclipse.AllTests"/>
</ant>
</target>

View File

@ -33,17 +33,17 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.core;
package net.sourceforge.pmd.core;
import java.util.Iterator;
import java.util.Set;
import junit.framework.TestCase;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.core.PMDCorePlugin;
import net.sourceforge.pmd.core.PluginConstants;
import junit.framework.TestCase;
/**
* Test the PMD Core plugin
@ -52,18 +52,21 @@ import junit.framework.TestCase;
* @version $Revision$
*
* $Log$
* Revision 1.2 2005/07/02 14:32:01 phherlin
* Revision 1.1 2006/06/18 22:29:52 phherlin
* Begin refactoring the unit tests for the plugin
* Revision 1.2 2005/07/02 14:32:01 phherlin
* Implement the RuleSets extension points new tests
*
* Revision 1.1 2005/06/15 21:14:56 phherlin
* Create the project for the Eclipse plugin unit tests
*
*
*
* Revision 1.1 2005/06/15 21:14:56 phherlin Create the project for the Eclipse
* plugin unit tests
*
*
*/
public class PMDCorePluginTest extends TestCase {
/**
* Constructor for PMDCorePluginTest.
*
* @param name
*/
public PMDCorePluginTest(String name) {
@ -72,67 +75,70 @@ public class PMDCorePluginTest extends TestCase {
/**
* Test that the core plugin has been instantiated
*
*
*/
public void testPMDCorePluginNotNull() {
assertNotNull("The Core Plugin has not been instantiated", PMDCorePlugin.getDefault());
}
/**
* Test that we can get a ruleset manager
*
*
*/
public void testRuleSetManagerNotNull() {
assertNotNull("Cannot get a ruleset manager", PMDCorePlugin.getDefault().getRuleSetManager());
assertNotNull("Cannot get a ruleset manager", PMDCorePlugin.getDefault().getRuleSetManager());
}
/**
* Test all the known PMD rulesets has been registered
* For this test to work, no fragement or only the test plugin fragment should be installed.
*
* Test all the known PMD rulesets has been registered For this test to
* work, no fragement or only the test plugin fragment should be installed.
*
*/
public void testStandardPMDRuleSetsRegistered() throws RuleSetNotFoundException {
Set registeredRuleSets = PMDCorePlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
assertFalse("No registered rulesets!", registeredRuleSets.isEmpty());
RuleSetFactory factory = new RuleSetFactory();
for (int i = 0; i < PluginConstants.PMD_RULESETS.length; i++) {
RuleSet ruleSet = factory.createRuleSet(PluginConstants.PMD_RULESETS[i]);
assertTrue("RuleSet \"" + PluginConstants.PMD_RULESETS[i] + "\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets));
RuleSet ruleSet = factory.createSingleRuleSet(PluginConstants.PMD_RULESETS[i]);
assertTrue("RuleSet \"" + PluginConstants.PMD_RULESETS[i] + "\" has not been registered", ruleSetRegistered(ruleSet,
registeredRuleSets));
}
}
/**
* Test the default rulesets has been registered
* For this test to work, no fragement or only the test plugin fragment should be installed.
*
* Test the default rulesets has been registered For this test to work, no
* fragement or only the test plugin fragment should be installed.
*
*/
public void testDefaultPMDRuleSetsRegistered() throws RuleSetNotFoundException {
Set defaultRuleSets = PMDCorePlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
assertFalse("No registered default rulesets!", defaultRuleSets.isEmpty());
RuleSetFactory factory = new RuleSetFactory();
for (int i = 0; i < PluginConstants.PMD_RULESETS.length; i++) {
RuleSet ruleSet = factory.createRuleSet(PluginConstants.PMD_RULESETS[i]);
assertTrue("RuleSet \"" + PluginConstants.PMD_RULESETS[i] + "\" has not been registered", ruleSetRegistered(ruleSet, defaultRuleSets));
RuleSet ruleSet = factory.createSingleRuleSet(PluginConstants.PMD_RULESETS[i]);
assertTrue("RuleSet \"" + PluginConstants.PMD_RULESETS[i] + "\" has not been registered", ruleSetRegistered(ruleSet,
defaultRuleSets));
}
}
/**
* test if a ruleset is registered
*
* @param ruleSet
* @param set
* @return true if ok
*/
private boolean ruleSetRegistered(RuleSet ruleSet, Set set) {
boolean registered = false;
Iterator i = set.iterator();
while (i.hasNext() && !registered) {
RuleSet registeredRuleSet = (RuleSet) i.next();
registered = registeredRuleSet.getName().equals(ruleSet.getName());
}
return registered;
}
}

View File

@ -33,7 +33,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.core;
package net.sourceforge.pmd.core;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.core.IRuleSetManager;
@ -47,6 +47,9 @@ import junit.framework.TestCase;
* @version $Revision$
*
* $Log$
* Revision 1.1 2006/06/18 22:29:51 phherlin
* Begin refactoring the unit tests for the plugin
*
* Revision 1.2 2005/07/02 14:32:01 phherlin
* Implement the RuleSets extension points new tests
*

View File

@ -33,7 +33,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.core.ext;
package net.sourceforge.pmd.core.ext;
import java.util.Iterator;
import java.util.Set;
@ -51,62 +51,65 @@ import net.sourceforge.pmd.core.PMDCorePlugin;
* @version $Revision$
*
* $Log$
* Revision 1.1 2005/07/02 14:32:02 phherlin
* Implement the RuleSets extension points new tests
*
*
* Revision 1.1 2006/06/18 22:29:52 phherlin
* Begin refactoring the unit tests for the plugin
* Revision 1.1 2005/07/02 14:32:02 phherlin Implement the RuleSets
* extension points new tests
*
*
*/
public class RuleSetsExtensionProcessorTest extends TestCase {
/**
* Tests the additional rulesets has been registered.
* For this test to work, the test plugin fragment must be installed.
*
* Tests the additional rulesets has been registered. For this test to work,
* the test plugin fragment must be installed.
*
*/
public void testAdditionalRuleSetsRegistered() throws RuleSetNotFoundException {
Set registeredRuleSets = PMDCorePlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
assertFalse("No registered rulesets!", registeredRuleSets.isEmpty());
RuleSetFactory factory = new RuleSetFactory();
RuleSet ruleSet = factory.createRuleSet("rulesets/extra1.xml");
RuleSet ruleSet = factory.createSingleRuleSet("rulesets/extra1.xml");
assertTrue("RuleSet \"rulesets/extra1.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets));
ruleSet = factory.createRuleSet("rulesets/extra2.xml");
ruleSet = factory.createSingleRuleSet("rulesets/extra2.xml");
assertTrue("RuleSet \"rulesets/extra2.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets));
}
/**
* Tests the additional default rulesets has been registered.
* For this test to work, the test plugin fragment must be installed.
*
* Tests the additional default rulesets has been registered. For this test
* to work, the test plugin fragment must be installed.
*
*/
public void testAdditionalDefaultRuleSetsRegistered() throws RuleSetNotFoundException {
Set registeredRuleSets = PMDCorePlugin.getDefault().getRuleSetManager().getDefaultRuleSets();
assertFalse("No registered default rulesets!", registeredRuleSets.isEmpty());
RuleSetFactory factory = new RuleSetFactory();
RuleSet ruleSet = factory.createRuleSet("rulesets/extra1.xml");
RuleSet ruleSet = factory.createSingleRuleSet("rulesets/extra1.xml");
assertTrue("RuleSet \"rulesets/extra1.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets));
ruleSet = factory.createRuleSet("rulesets/extra2.xml");
ruleSet = factory.createSingleRuleSet("rulesets/extra2.xml");
assertTrue("RuleSet \"rulesets/extra2.xml\" has not been registered", ruleSetRegistered(ruleSet, registeredRuleSets));
}
/**
* test if a ruleset is registered
*
* @param ruleSet
* @param set
* @return true if ok
*/
private boolean ruleSetRegistered(RuleSet ruleSet, Set set) {
boolean registered = false;
Iterator i = set.iterator();
while (i.hasNext() && !registered) {
RuleSet registeredRuleSet = (RuleSet) i.next();
registered = registeredRuleSet.getName().equals(ruleSet.getName());
}
return registered;
}
}

View File

@ -33,7 +33,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.eclipse;
package net.sourceforge.pmd.eclipse;
import java.io.Reader;
import java.io.StringReader;
@ -48,10 +48,8 @@ import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.TargetJDK1_3;
import net.sourceforge.pmd.TargetJDK1_4;
import net.sourceforge.pmd.TargetJDK1_5;
import net.sourceforge.pmd.eclipse.PMDPluginConstants;
import net.sourceforge.pmd.SourceType;
import net.sourceforge.pmd.core.PluginConstants;
/**
* Test if PMD can be run correctly
@ -60,21 +58,22 @@ import net.sourceforge.pmd.eclipse.PMDPluginConstants;
* @version $Revision$
*
* $Log$
* Revision 1.2 2005/07/01 00:06:38 phherlin
* Revision 1.1 2006/06/18 22:29:51 phherlin
* Begin refactoring the unit tests for the plugin
* Revision 1.2 2005/07/01 00:06:38 phherlin
* Refactoring and writing more tests
*
* Revision 1.1 2005/06/15 21:14:57 phherlin
* Create the project for the Eclipse plugin unit tests
*
*
*
* Revision 1.1 2005/06/15 21:14:57 phherlin Create the project for the Eclipse
* plugin unit tests
*
*
*/
public class BasicPMDTest extends TestCase {
/**
* Test case constructor
*
* @param name
* of the test case
* @param name of the test case
*/
public BasicPMDTest(String name) {
super(name);
@ -82,12 +81,13 @@ public class BasicPMDTest extends TestCase {
/**
* One first thing the plugin must be able to do is to run PMD
*
*
*/
public void testRunPmdJdk13() {
try {
PMD pmd = new PMD(new TargetJDK1_3());
PMD pmd = new PMD();
pmd.setJavaVersion(SourceType.JAVA_13);
String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
Reader input = new StringReader(sourceCode);
@ -96,7 +96,7 @@ public class BasicPMDTest extends TestCase {
context.setSourceCodeFilename("foo.java");
context.setReport(new Report());
RuleSet basicRuleSet = new RuleSetFactory().createRuleSet("rulesets/basic.xml");
RuleSet basicRuleSet = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml");
pmd.processFile(input, basicRuleSet, context);
Iterator iter = context.getReport().iterator();
@ -104,7 +104,7 @@ public class BasicPMDTest extends TestCase {
RuleViolation violation = (RuleViolation) iter.next();
assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
assertEquals(3, violation.getLine());
assertEquals(3, violation.getBeginLine());
} catch (RuleSetNotFoundException e) {
e.printStackTrace();
@ -117,12 +117,13 @@ public class BasicPMDTest extends TestCase {
/**
* Let see with Java 1.4
*
*
*/
public void testRunPmdJdk14() {
try {
PMD pmd = new PMD(new TargetJDK1_4());
PMD pmd = new PMD();
pmd.setJavaVersion(SourceType.JAVA_14);
String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
Reader input = new StringReader(sourceCode);
@ -131,7 +132,7 @@ public class BasicPMDTest extends TestCase {
context.setSourceCodeFilename("foo.java");
context.setReport(new Report());
RuleSet basicRuleSet = new RuleSetFactory().createRuleSet("rulesets/basic.xml");
RuleSet basicRuleSet = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml");
pmd.processFile(input, basicRuleSet, context);
Iterator iter = context.getReport().iterator();
@ -139,7 +140,7 @@ public class BasicPMDTest extends TestCase {
RuleViolation violation = (RuleViolation) iter.next();
assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
assertEquals(3, violation.getLine());
assertEquals(3, violation.getBeginLine());
} catch (RuleSetNotFoundException e) {
e.printStackTrace();
@ -152,12 +153,13 @@ public class BasicPMDTest extends TestCase {
/**
* Let see with Java 1.5
*
*
*/
public void testRunPmdJdk15() {
try {
PMD pmd = new PMD(new TargetJDK1_5());
PMD pmd = new PMD();
pmd.setJavaVersion(SourceType.JAVA_15);
String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
Reader input = new StringReader(sourceCode);
@ -166,7 +168,7 @@ public class BasicPMDTest extends TestCase {
context.setSourceCodeFilename("foo.java");
context.setReport(new Report());
RuleSet basicRuleSet = new RuleSetFactory().createRuleSet("rulesets/basic.xml");
RuleSet basicRuleSet = new RuleSetFactory().createSingleRuleSet("rulesets/basic.xml");
pmd.processFile(input, basicRuleSet, context);
Iterator iter = context.getReport().iterator();
@ -174,7 +176,7 @@ public class BasicPMDTest extends TestCase {
RuleViolation violation = (RuleViolation) iter.next();
assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
assertEquals(3, violation.getLine());
assertEquals(3, violation.getBeginLine());
} catch (RuleSetNotFoundException e) {
e.printStackTrace();
@ -187,14 +189,14 @@ public class BasicPMDTest extends TestCase {
/**
* Try to load all the plugin known rulesets
*
*
*/
public void testDefaulltRuleSets() {
RuleSetFactory factory = new RuleSetFactory();
String allRuleSets[] = PMDPluginConstants.RULESET_ALLPMD;
String allRuleSets[] = PluginConstants.PMD_RULESETS;
for (int i = 0; i < allRuleSets.length; i++) {
try {
RuleSet ruleSet = factory.createRuleSet(allRuleSets[i]);
RuleSet ruleSet = factory.createSingleRuleSet(allRuleSets[i]);
} catch (RuleSetNotFoundException e) {
e.printStackTrace();
fail("unable to load ruleset " + allRuleSets[i]);

View File

@ -33,17 +33,18 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.eclipse;
package net.sourceforge.pmd.eclipse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.eclipse.PMDPluginConstants;
import net.sourceforge.pmd.eclipse.builder.PMDNature;
import net.sourceforge.pmd.runtime.PMDRuntimeConstants;
import net.sourceforge.pmd.runtime.builder.PMDNature;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
@ -62,13 +63,15 @@ import org.eclipse.jdt.core.JavaModelException;
* @version $Revision$
*
* $Log$
* Revision 1.2 2005/12/30 16:29:15 phherlin
* Revision 1.1 2006/06/18 22:29:51 phherlin
* Begin refactoring the unit tests for the plugin
* Revision 1.2 2005/12/30 16:29:15 phherlin
* Implement a new preferences model and review some tests
*
* Revision 1.1 2005/06/15 21:14:57 phherlin
* Create the project for the Eclipse plugin unit tests
*
*
*
* Revision 1.1 2005/06/15 21:14:57 phherlin Create the project for the Eclipse
* plugin unit tests
*
*
*/
public class EclipseUtils {
@ -78,39 +81,40 @@ public class EclipseUtils {
private EclipseUtils() {
super();
}
/**
* Create a new java project
*
* @param projectName a project name
* @return newProject a new project resource handle
*/
public static IProject createJavaProject(String projectName) throws CoreException {
// 1. Get the project from the workspace
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject newProject = root.getProject(projectName);
IProjectDescription description = newProject.getWorkspace().newProjectDescription(projectName);
// 2. Create a project if it does not already exist
if (!newProject.exists()) {
description.setLocation(null);
newProject.create(description, null);
newProject.open(null);
}
// 4. Make it a Java Project
addJavaNature(newProject);
return newProject;
}
/**
* Create a test source file
* @param project a project where to create that file; this project is expected
* to be empty
*
* @param project a project where to create that file; this project is
* expected to be empty
*/
public static IFile createTestSourceFile(IProject project) throws JavaModelException, CoreException, IOException {
public static IFile createTestSourceFile(IProject project) throws JavaModelException, CoreException, IOException {
// 1. Locate the test java source template
InputStream is = EclipseUtils.class.getResourceAsStream("/test.template");
@ -125,17 +129,15 @@ public class EclipseUtils {
is.close();
sourceFile.setLocal(true, IResource.DEPTH_INFINITE, null);
project.refreshLocal(IResource.DEPTH_INFINITE, null);
return sourceFile;
}
/**
* Get the content of a project resource.
*
* @param project
* a project reference
* @param resourceName
* the name of the resource (@see IProject)
* @param project a project reference
* @param resourceName the name of the resource (@see IProject)
* @return the resource content as an InputStream or null
* @throws CoreException
*/
@ -143,49 +145,51 @@ public class EclipseUtils {
IFile file = project.getFile(resourceName);
return (file != null) && file.exists() && file.isAccessible() ? file.getContents(true) : null;
}
/**
* Remove the PMD Nature from a project
*
* @param project a project to remove the PMD Nature
* @param monitor a progress monitor
* @return success true if the nature has been removed; false means the
* project already had not the PMD Nature.
* project already had not the PMD Nature.
* @throws CoreException if any error occurs.
*/
public static boolean removePMDNature(final IProject project) throws CoreException {
boolean success = false;
if (project.hasNature(PMDNature.PMD_NATURE)) {
final IProjectDescription description = project.getDescription();
final String[] natureIds = description.getNatureIds();
String[] newNatureIds = new String[natureIds.length - 1];
for (int i = 0, j = 0; i < natureIds.length; i++) {
if (!natureIds[i].equals(PMDNature.PMD_NATURE)) {
newNatureIds[j++] = natureIds[i];
}
}
description.setNatureIds(newNatureIds);
project.setDescription(description, null);
project.deleteMarkers(PMDPluginConstants.PMD_MARKER, true, IResource.DEPTH_INFINITE);
IFile file = project.getFile(".pmd");
if (file.exists() && file.isAccessible()) {
file.delete(true, false, null);
}
}
return success;
boolean success = false;
if (project.hasNature(PMDNature.PMD_NATURE)) {
final IProjectDescription description = project.getDescription();
final String[] natureIds = description.getNatureIds();
String[] newNatureIds = new String[natureIds.length - 1];
for (int i = 0, j = 0; i < natureIds.length; i++) {
if (!natureIds[i].equals(PMDNature.PMD_NATURE)) {
newNatureIds[j++] = natureIds[i];
}
}
description.setNatureIds(newNatureIds);
project.setDescription(description, null);
project.deleteMarkers(PMDRuntimeConstants.PMD_MARKER, true, IResource.DEPTH_INFINITE);
IFile file = project.getFile(".pmd");
if (file.exists() && file.isAccessible()) {
file.delete(true, false, null);
}
}
return success;
}
/**
* Test if 2 sets of rules are equals
*
* @param ruleSet1
* @param ruleSet2
* @return
*/
public static boolean assertRuleSetEquals(Set ruleSet1, Set ruleSet2) {
public static boolean assertRuleSetEquals(Collection ruleSet1, Collection ruleSet2) {
boolean equals = true;
for (Iterator i = ruleSet1.iterator(); i.hasNext() && equals;) {
Rule rule = (Rule) i.next();
if (!searchRule(rule, ruleSet2)) {
@ -193,7 +197,7 @@ public class EclipseUtils {
System.out.println("Rule " + rule.getName() + " is not in the second ruleset");
}
}
for (Iterator i = ruleSet2.iterator(); i.hasNext() && equals;) {
Rule rule = (Rule) i.next();
if (!searchRule(rule, ruleSet1)) {
@ -201,40 +205,43 @@ public class EclipseUtils {
System.out.println("Rule " + rule.getName() + " is not in the first ruleset");
}
}
return equals;
}
/**
* Add a Java Nature to a project when creating
*
* @param project
* @throws CoreException
*/
private static void addJavaNature(IProject project) throws CoreException {
if (!project.hasNature(JavaCore.NATURE_ID)) {
IProjectDescription description = project.getDescription();
String[] prevNatures= description.getNatureIds();
String[] newNatures= new String[prevNatures.length + 1];
String[] prevNatures = description.getNatureIds();
String[] newNatures = new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length]= JavaCore.NATURE_ID;
newNatures[prevNatures.length] = JavaCore.NATURE_ID;
description.setNatureIds(newNatures);
project.setDescription(description, null);
}
}
/**
* Search a rule in a set of rules
*
* @param rule
* @param set
* @return
*/
private static boolean searchRule(Rule rule, Set set) {
private static boolean searchRule(Rule rule, Collection set) {
boolean found = false;
for (Iterator i = set.iterator(); i.hasNext() && !found;) {
Rule r = (Rule) i.next();
if (r.getClass().getName().equals(rule.getClass().getName())) {
found = r.getName().equals(rule.getName()) && r.getProperties().equals(rule.getProperties()) && (r.getPriority() == rule.getPriority());
found = r.getName().equals(rule.getName()) && r.getProperties().equals(rule.getProperties())
&& (r.getPriority() == rule.getPriority());
if ((!found) && (r.getName().equals(rule.getName()))) {
System.out.println("Rules " + r.getName() + " are different because:");
System.out.println("Priorities are different: " + (r.getPriority() != rule.getPriority()));
@ -249,12 +256,13 @@ public class EclipseUtils {
}
}
}
return found;
}
/**
* Print rule details
*
* @param rule
*/
private static void dumpRule(Rule rule) {
@ -267,5 +275,5 @@ public class EclipseUtils {
System.out.println(" " + key + " = " + p.getProperty(key));
}
}
}

View File

@ -33,7 +33,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.eclipse.cmd;
package net.sourceforge.pmd.runtime.cmd;
import java.io.InputStream;
@ -41,15 +41,15 @@ import junit.framework.TestCase;
import name.herlin.command.CommandException;
import name.herlin.command.UnsetInputPropertiesException;
import net.sourceforge.pmd.cpd.SimpleRenderer;
import net.sourceforge.pmd.eclipse.PMDPluginConstants;
import net.sourceforge.pmd.eclipse.cpd.DetectCutAndPasteCmd;
import net.sourceforge.pmd.eclipse.EclipseUtils;
import net.sourceforge.pmd.runtime.PMDRuntimeConstants;
import net.sourceforge.pmd.runtime.cmd.DetectCutAndPasteCmd;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import test.net.sourceforge.pmd.eclipse.EclipseUtils;
/**
* Test the CPD command
@ -58,44 +58,47 @@ import test.net.sourceforge.pmd.eclipse.EclipseUtils;
* @version $Revision$
*
* $Log$
* Revision 1.1 2005/06/15 21:14:57 phherlin
* Create the project for the Eclipse plugin unit tests
*
*
* Revision 1.1 2006/06/18 22:29:51 phherlin
* Begin refactoring the unit tests for the plugin
* Revision 1.1 2005/06/15 21:14:57
* phherlin Create the project for the Eclipse plugin unit tests
*
*
*/
public class DetectCutAndPasteCmdTest extends TestCase {
private IProject testProject;
/**
* Default constructor
*
* @param name
*/
public DetectCutAndPasteCmdTest(String name) {
super(name);
}
/**
* Test the basic usage of the report rendering command
*
*
*/
public void testDetectCutAndPasteCmdBasic() throws CommandException, CoreException {
DetectCutAndPasteCmd cmd = new DetectCutAndPasteCmd();
cmd.setProject(this.testProject);
cmd.setRenderer(new SimpleRenderer());
cmd.setReportName(PMDPluginConstants.SIMPLE_CPDREPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.SIMPLE_CPDREPORT_NAME);
cmd.performExecute();
cmd.join();
IFolder reportFolder = this.testProject.getFolder(PMDPluginConstants.REPORT_FOLDER);
IFolder reportFolder = this.testProject.getFolder(PMDRuntimeConstants.REPORT_FOLDER);
assertTrue(reportFolder.exists());
IFile reportFile = reportFolder.getFile(PMDPluginConstants.SIMPLE_CPDREPORT_NAME);
IFile reportFile = reportFolder.getFile(PMDRuntimeConstants.SIMPLE_CPDREPORT_NAME);
assertTrue(reportFile.exists());
if (reportFile.exists()) {
reportFile.delete(true, false, null);
}
if (reportFolder.exists()) {
reportFolder.delete(true, false, null);
}
@ -103,6 +106,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
/**
* Test robustness #1
*
* @throws CommandException
*/
public void testDetectCutAndPasteCmdNullArg1() throws CommandException {
@ -110,7 +114,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
DetectCutAndPasteCmd cmd = new DetectCutAndPasteCmd();
cmd.setProject(null);
cmd.setRenderer(new SimpleRenderer());
cmd.setReportName(PMDPluginConstants.SIMPLE_CPDREPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.SIMPLE_CPDREPORT_NAME);
cmd.performExecute();
fail();
} catch (UnsetInputPropertiesException e) {
@ -120,6 +124,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
/**
* Test robustness #2
*
* @throws CommandException
*/
public void testDetectCutAndPasteCmdNullArg2() throws CommandException {
@ -127,7 +132,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
DetectCutAndPasteCmd cmd = new DetectCutAndPasteCmd();
cmd.setProject(this.testProject);
cmd.setRenderer(null);
cmd.setReportName(PMDPluginConstants.SIMPLE_CPDREPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.SIMPLE_CPDREPORT_NAME);
cmd.performExecute();
fail();
} catch (UnsetInputPropertiesException e) {
@ -137,6 +142,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
/**
* Test robustness #3
*
* @throws CommandException
*/
public void testDetectCutAndPasteCmdNullArg3() throws CommandException {
@ -154,6 +160,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
/**
* Test robustness #4
*
* @throws CommandException
*/
public void testDetectCutAndPasteCmdNullArg4() throws CommandException {
@ -161,7 +168,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
DetectCutAndPasteCmd cmd = new DetectCutAndPasteCmd();
cmd.setProject(null);
cmd.setRenderer(null);
cmd.setReportName(PMDPluginConstants.SIMPLE_CPDREPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.SIMPLE_CPDREPORT_NAME);
cmd.performExecute();
fail();
} catch (UnsetInputPropertiesException e) {
@ -171,6 +178,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
/**
* Test robustness #5
*
* @throws CommandException
*/
public void testDetectCutAndPasteCmdNullArg5() throws CommandException {
@ -188,6 +196,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
/**
* Test robustness #6
*
* @throws CommandException
*/
public void testDetectCutAndPasteCmdNullArg6() throws CommandException {
@ -205,6 +214,7 @@ public class DetectCutAndPasteCmdTest extends TestCase {
/**
* Test robustness #7
*
* @throws CommandException
*/
public void testDetectCutAndPasteCmdNullArg7() throws CommandException {
@ -246,10 +256,11 @@ public class DetectCutAndPasteCmdTest extends TestCase {
if (this.testProject != null) {
if (this.testProject.exists() && this.testProject.isAccessible()) {
EclipseUtils.removePMDNature(this.testProject);
// this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
// Thread.sleep(500);
// this.testProject.delete(true, true, null);
// this.testProject = null;
// this.testProject.refreshLocal(IResource.DEPTH_INFINITE,
// null);
// Thread.sleep(500);
// this.testProject.delete(true, true, null);
// this.testProject = null;
}
}

View File

@ -33,17 +33,18 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.eclipse.cmd;
package net.sourceforge.pmd.runtime.cmd;
import java.io.InputStream;
import junit.framework.TestCase;
import name.herlin.command.CommandException;
import name.herlin.command.UnsetInputPropertiesException;
import net.sourceforge.pmd.eclipse.PMDPluginConstants;
import net.sourceforge.pmd.eclipse.cmd.RenderReportCmd;
import net.sourceforge.pmd.eclipse.cmd.ReviewCodeCmd;
import net.sourceforge.pmd.eclipse.EclipseUtils;
import net.sourceforge.pmd.renderers.HTMLRenderer;
import net.sourceforge.pmd.runtime.PMDRuntimeConstants;
import net.sourceforge.pmd.runtime.cmd.RenderReportCmd;
import net.sourceforge.pmd.runtime.cmd.ReviewCodeCmd;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
@ -51,7 +52,6 @@ import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import test.net.sourceforge.pmd.eclipse.EclipseUtils;
/**
* Test the report rendering
@ -60,53 +60,56 @@ import test.net.sourceforge.pmd.eclipse.EclipseUtils;
* @version $Revision$
*
* $Log$
* Revision 1.2 2005/12/30 16:29:15 phherlin
* Revision 1.1 2006/06/18 22:29:51 phherlin
* Begin refactoring the unit tests for the plugin
* Revision 1.2 2005/12/30 16:29:15 phherlin
* Implement a new preferences model and review some tests
*
* Revision 1.1 2005/06/15 21:14:57 phherlin
* Create the project for the Eclipse plugin unit tests
*
*
*
* Revision 1.1 2005/06/15 21:14:57 phherlin Create the project for the Eclipse
* plugin unit tests
*
*
*/
public class RenderReportCmdTest extends TestCase {
private IProject testProject;
/**
* Default constructor
*
* @param name
*/
public RenderReportCmdTest(String name) {
super(name);
}
/**
* Test the basic usage of the report rendering command
*
*
*/
public void testRenderReportCmdBasic() throws CommandException, CoreException {
ReviewCodeCmd reviewCmd = new ReviewCodeCmd();
reviewCmd.addResource(this.testProject);
reviewCmd.performExecute();
RenderReportCmd cmd = new RenderReportCmd();
cmd.setProject(this.testProject);
cmd.setRenderer(new HTMLRenderer());
cmd.setReportName(PMDPluginConstants.HTML_REPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.HTML_REPORT_NAME);
cmd.performExecute();
cmd.join();
IFolder reportFolder = this.testProject.getFolder(PMDPluginConstants.REPORT_FOLDER);
IFolder reportFolder = this.testProject.getFolder(PMDRuntimeConstants.REPORT_FOLDER);
assertTrue(reportFolder.exists());
IFile reportFile = reportFolder.getFile(PMDPluginConstants.HTML_REPORT_NAME);
IFile reportFile = reportFolder.getFile(PMDRuntimeConstants.HTML_REPORT_NAME);
assertTrue(reportFile.exists());
this.testProject.deleteMarkers(PMDPluginConstants.PMD_MARKER, true, IResource.DEPTH_INFINITE);
this.testProject.deleteMarkers(PMDRuntimeConstants.PMD_MARKER, true, IResource.DEPTH_INFINITE);
if (reportFile.exists()) {
reportFile.delete(true, false, null);
}
if (reportFolder.exists()) {
reportFolder.delete(true, false, null);
}
@ -114,6 +117,7 @@ public class RenderReportCmdTest extends TestCase {
/**
* Test robustness #1
*
* @throws CommandException
*/
public void testRenderReportCmdNullArg1() throws CommandException {
@ -121,7 +125,7 @@ public class RenderReportCmdTest extends TestCase {
RenderReportCmd cmd = new RenderReportCmd();
cmd.setProject(null);
cmd.setRenderer(new HTMLRenderer());
cmd.setReportName(PMDPluginConstants.HTML_REPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.HTML_REPORT_NAME);
cmd.performExecute();
fail();
} catch (UnsetInputPropertiesException e) {
@ -131,6 +135,7 @@ public class RenderReportCmdTest extends TestCase {
/**
* Test robustness #2
*
* @throws CommandException
*/
public void testRenderReportCmdNullArg2() throws CommandException {
@ -138,7 +143,7 @@ public class RenderReportCmdTest extends TestCase {
RenderReportCmd cmd = new RenderReportCmd();
cmd.setProject(this.testProject);
cmd.setRenderer(null);
cmd.setReportName(PMDPluginConstants.HTML_REPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.HTML_REPORT_NAME);
cmd.performExecute();
fail();
} catch (UnsetInputPropertiesException e) {
@ -148,6 +153,7 @@ public class RenderReportCmdTest extends TestCase {
/**
* Test robustness #3
*
* @throws CommandException
*/
public void testRenderReportCmdNullArg3() throws CommandException {
@ -165,6 +171,7 @@ public class RenderReportCmdTest extends TestCase {
/**
* Test robustness #4
*
* @throws CommandException
*/
public void testRenderReportCmdNullArg4() throws CommandException {
@ -172,7 +179,7 @@ public class RenderReportCmdTest extends TestCase {
RenderReportCmd cmd = new RenderReportCmd();
cmd.setProject(null);
cmd.setRenderer(null);
cmd.setReportName(PMDPluginConstants.HTML_REPORT_NAME);
cmd.setReportName(PMDRuntimeConstants.HTML_REPORT_NAME);
cmd.performExecute();
fail();
} catch (UnsetInputPropertiesException e) {
@ -182,6 +189,7 @@ public class RenderReportCmdTest extends TestCase {
/**
* Test robustness #5
*
* @throws CommandException
*/
public void testRenderReportCmdNullArg5() throws CommandException {
@ -199,6 +207,7 @@ public class RenderReportCmdTest extends TestCase {
/**
* Test robustness #6
*
* @throws CommandException
*/
public void testRenderReportCmdNullArg6() throws CommandException {
@ -216,6 +225,7 @@ public class RenderReportCmdTest extends TestCase {
/**
* Test robustness #7
*
* @throws CommandException
*/
public void testRenderReportCmdNullArg7() throws CommandException {
@ -257,10 +267,11 @@ public class RenderReportCmdTest extends TestCase {
if (this.testProject != null) {
if (this.testProject.exists() && this.testProject.isAccessible()) {
EclipseUtils.removePMDNature(this.testProject);
// this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
// Thread.sleep(500);
// this.testProject.delete(true, true, null);
// this.testProject = null;
// this.testProject.refreshLocal(IResource.DEPTH_INFINITE,
// null);
// Thread.sleep(500);
// this.testProject.delete(true, true, null);
// this.testProject = null;
}
}

View File

@ -33,7 +33,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.eclipse.cmd;
package net.sourceforge.pmd.runtime.cmd;
import java.io.InputStream;
import java.util.Map;
@ -41,13 +41,13 @@ import java.util.Map;
import junit.framework.TestCase;
import name.herlin.command.CommandException;
import name.herlin.command.UnsetInputPropertiesException;
import net.sourceforge.pmd.eclipse.cmd.ReviewCodeCmd;
import net.sourceforge.pmd.eclipse.EclipseUtils;
import net.sourceforge.pmd.runtime.cmd.ReviewCodeCmd;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import test.net.sourceforge.pmd.eclipse.EclipseUtils;
/**
* This tests the PMD Processor command
@ -56,6 +56,9 @@ import test.net.sourceforge.pmd.eclipse.EclipseUtils;
* @version $Revision$
*
* $Log$
* Revision 1.1 2006/06/18 22:29:51 phherlin
* Begin refactoring the unit tests for the plugin
*
* Revision 1.3 2006/01/17 21:26:24 phherlin
* Ignore exceptions occuring inside the teardown operation
*

View File

@ -0,0 +1,185 @@
/*
* Created on 17 juin 2006
*
* Copyright (c) 2006, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.runtime.preferences.vo;
import junit.framework.TestCase;
/**
* Unit tests of Priority object.
*
* @author Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2006/06/18 22:29:50 phherlin
* Begin refactoring the unit tests for the plugin
*
*
*/
public class PriorityTest extends TestCase {
/**
* A default constructed priority is set to level 3
*
*/
public void testDefaultPriority() {
assertEquals("Default priority is not set to 3", Priority.LEVEL3, new Priority());
}
/**
* It is legal to construct a priority object and to assign a value.
* This test implicitluy test the basic equality also.
* All in one test case!
*/
public void testSetPriority() {
Priority p = new Priority();
p.setPriority(Priority.LEVEL1_LITTERAL);
assertEquals("Constructing a priority level 1 has failed!", Priority.LEVEL1, p);
p.setPriority(Priority.LEVEL2_LITTERAL);
assertEquals("Constructing a priority level 2 has failed!", Priority.LEVEL2, p);
p.setPriority(Priority.LEVEL3_LITTERAL);
assertEquals("Constructing a priority level 3 has failed!", Priority.LEVEL3, p);
p.setPriority(Priority.LEVEL4_LITTERAL);
assertEquals("Constructing a priority level 4 has failed!", Priority.LEVEL4, p);
p.setPriority(Priority.LEVEL5_LITTERAL);
assertEquals("Constructing a priority level 5 has failed!", Priority.LEVEL5, p);
}
/**
* Test an illegal value that is 0
*
*/
public void testSetPriorityIllegal1() {
try {
Priority p = new Priority();
p.setPriority(0);
fail("Setting a priority level to 0 should raise an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// this is correct
}
}
/**
* Test an illegal value that is negative
*
*/
public void testSetPriorityIllegal2() {
try {
Priority p = new Priority();
p.setPriority(-15);
fail("Setting a priority level to a negative number should raise an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// this is correct
}
}
/**
* Test an illegal value that is too high
*
*/
public void testSetPriorityIllegal3() {
try {
Priority p = new Priority();
p.setPriority(6);
fail("Setting a priority level to a high value should raise an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// this is correct
}
}
/**
* 2 different instances assigned different levels are not equals
*
*/
public void testEquals1() {
Priority p = new Priority();
p.setPriority(1);
assertFalse("2 priorities with different levels (1:2) are not equals", Priority.LEVEL2.equals(p));
assertFalse("2 priorities with different levels (1:3) are not equals", Priority.LEVEL3.equals(p));
assertFalse("2 priorities with different levels (1:4) are not equals", Priority.LEVEL4.equals(p));
assertFalse("2 priorities with different levels (1:5) are not equals", Priority.LEVEL5.equals(p));
}
/**
* Any priority object is always not equal to null
*
*/
public void testEquals2() {
assertFalse("Priority object (1) is not equal to null", Priority.LEVEL1.equals(null));
assertFalse("Priority object (2) is not equal to null", Priority.LEVEL2.equals(null));
assertFalse("Priority object (3) is not equal to null", Priority.LEVEL3.equals(null));
assertFalse("Priority object (4) is not equal to null", Priority.LEVEL4.equals(null));
assertFalse("Priority object (5) is not equal to null", Priority.LEVEL5.equals(null));
}
/**
* Any priority objet is always different that any other objets
*
*/
public void testEquals3() {
assertFalse("Priority object must be different that any other object", Priority.LEVEL1.equals(new Integer(1)));
}
public void testEquals4() {
Priority p1 = new Priority();
assertEquals("A priority objetc must be equals to itself", p1, p1);
}
/**
* 2 equals objets have the same hashcode
*
*/
public void testHashCode1() {
Priority p = new Priority();
assertTrue("2 equal priority objects must have the same hashcode value", Priority.LEVEL3.hashCode() == p.hashCode());
}
/**
* 2 different priority objects must have different hashcode values
*
*/
public void testHashCode2() {
assertFalse("2 different priority objects must have different hashcode values", Priority.LEVEL1.hashCode() == Priority.LEVEL2.hashCode());
}
}

View File

@ -0,0 +1,255 @@
/*
* Created on 17 juin 2006
*
* Copyright (c) 2006, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.runtime.preferences.vo;
import junit.framework.TestCase;
/**
* Unit tests of class Property
*
* @author Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2006/06/18 22:29:50 phherlin
* Begin refactoring the unit tests for the plugin
*
*
*/
public class PropertyTest extends TestCase {
/**
* A new property objet has its name and value not null and assigned to an
* empty string.
*
*/
public void testDefaults() {
Property p = new Property();
assertNotNull("Name must not be null", p.getName());
assertTrue("Name must be an empty string", p.getName().length() == 0);
assertNotNull("Value must not be null", p.getValue());
assertTrue("Value must be an empty string", p.getValue().length() == 0);
}
/**
* Assigning any string to name is legal.
*
*/
public void testSetName1() {
Property p = new Property();
p.setName("any string");
assertEquals("Name can be assigned any string", "any string", p.getName());
}
/**
* Assigning null to name is illegal.
*
*/
public void testSetName2() {
try {
Property p = new Property();
p.setName(null);
fail("Assigning null to name is illegal");
} catch (IllegalArgumentException e) {
// success
}
}
/**
* Assigning any string to value is legal.
*
*/
public void testSetValue1() {
Property p = new Property();
p.setValue("any string");
assertEquals("Value can be assigned any string", "any string", p.getValue());
}
/**
* Assigning null to Value is illegal.
*
*/
public void testSetValue2() {
try {
Property p = new Property();
p.setValue(null);
fail("Assigning null to value is illegal");
} catch (IllegalArgumentException e) {
// success
}
}
/**
* 2 properties are equals if their names and value are equal
*
*/
public void testEquals1() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
Property p2 = new Property();
p2.setName("p1");
p2.setValue("value1");
assertEquals("2 properties must be equals if their names and values are equals", p1, p2);
}
/**
* 2 properties are diffrent if their names are different.
*
*/
public void testEquals2() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
Property p2 = new Property();
p2.setName("p2");
p2.setValue("value1");
assertFalse("2 properties must be different if their names are different", p1.equals(p2));
}
/**
* 2 properties are diffrent if their value are different.
*
*/
public void testEquals3() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
Property p2 = new Property();
p2.setName("p1");
p2.setValue("value2");
assertFalse("2 properties must be different if their values are different", p1.equals(p2));
}
/**
* A property is always different than null
*
*/
public void testEquals4() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
assertFalse("A property cannot be equals to null", p1.equals(null));
}
/**
* Any property objet is always different that any other objets
*
*/
public void testEquals5() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
assertFalse("Property object must be different that any other object", p1.equals("p1"));
}
/**
* Obviously a property object is equals to itself
*
*/
public void testEquals6() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
assertEquals("A property object is equals to itself", p1, p1);
}
/**
* 2 equal properties must have the same hashCode
*
*/
public void testHashCode1() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
Property p2 = new Property();
p2.setName("p1");
p2.setValue("value1");
assertTrue("2 equal properties must have the same hashCode", p1.hashCode() == p2.hashCode());
}
/**
* 2 different properties must have the different hashCodes.
*
*/
public void testHashCode2() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
Property p2 = new Property();
p2.setName("p2");
p2.setValue("value1");
assertFalse("2 different properties should have different hashCodes", p1.hashCode() == p2.hashCode());
}
/**
* 2 different properties must have the different hashCodes.
*
*/
public void testHashCode3() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
Property p2 = new Property();
p2.setName("p1");
p2.setValue("value2");
assertFalse("2 different properties should have different hashCodes", p1.hashCode() == p2.hashCode());
}
/**
* 2 different properties must have the different hashCodes.
*
*/
public void testHashCode4() {
Property p1 = new Property();
p1.setName("p1");
p1.setValue("value1");
Property p2 = new Property();
p2.setName("p2");
p2.setValue("value2");
assertFalse("2 different properties should have different hashCodes", p1.hashCode() == p2.hashCode());
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
package test.net.sourceforge.pmd.eclipse.properties;
package net.sourceforge.pmd.ui.properties;
import java.util.Iterator;
@ -7,15 +7,15 @@ import name.herlin.command.CommandException;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import net.sourceforge.pmd.eclipse.model.ModelException;
import net.sourceforge.pmd.eclipse.model.ModelFactory;
import net.sourceforge.pmd.eclipse.model.ProjectPropertiesModel;
import net.sourceforge.pmd.eclipse.properties.UpdateProjectPropertiesCmd;
import net.sourceforge.pmd.eclipse.EclipseUtils;
import net.sourceforge.pmd.runtime.PMDRuntimePlugin;
import net.sourceforge.pmd.runtime.properties.IProjectProperties;
import net.sourceforge.pmd.runtime.properties.IProjectPropertiesManager;
import net.sourceforge.pmd.runtime.properties.PropertiesException;
import net.sourceforge.pmd.ui.properties.UpdateProjectPropertiesCmd;
import org.eclipse.core.resources.IProject;
import test.net.sourceforge.pmd.eclipse.EclipseUtils;
public class UpdateProjectPropertiesCmdTest extends TestCase {
private IProject testProject;
@ -46,18 +46,21 @@ public class UpdateProjectPropertiesCmdTest extends TestCase {
}
}
/**
* Bug: when a user deselect a project rule it is not saved
*/
public void testBug() throws CommandException, ModelException {
public void testBug() throws CommandException, PropertiesException {
RuleSetFactory factory = new RuleSetFactory();
// First ensure that the plugin initial ruleset is equal to the project ruleset
ProjectPropertiesModel model = ModelFactory.getFactory().getProperiesModelForProject(this.testProject);
// First ensure that the plugin initial ruleset is equal to the project
// ruleset
IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager();
IProjectProperties model = mgr.loadProjectProperties(this.testProject);
RuleSet projectRuleSet = model.getProjectRuleSet();
assertEquals("The project ruleset is not equal to the plugin ruleset", PMDPlugin.getDefault().getRuleSet(), projectRuleSet);
assertEquals("The project ruleset is not equal to the plugin ruleset", PMDRuntimePlugin.getDefault()
.getPreferencesManager().getRuleSet(), projectRuleSet);
// 2. remove the first rule (keep its name for assertion)
RuleSet newRuleSet = new RuleSet();
newRuleSet.addRuleSet(projectRuleSet);

View File

@ -1,111 +0,0 @@
/*
* Created on 28 déc. 2005
*
* Copyright (c) 2005, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.eclipse.dao;
import java.io.File;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.preference.IPreferenceStore;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import net.sourceforge.pmd.eclipse.dao.ConfigurationsTO;
import net.sourceforge.pmd.eclipse.dao.DAOException;
import net.sourceforge.pmd.eclipse.dao.DAOFactory;
import net.sourceforge.pmd.eclipse.dao.PreferencesDAO;
import net.sourceforge.pmd.eclipse.dao.PreferencesTO;
import junit.framework.TestCase;
/**
* Test the Preferences Data Access Object
*
* @author Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2005/12/30 16:29:15 phherlin
* Implement a new preferences model and review some tests
*
*
*/
public class PreferencesDAOTest extends TestCase {
/**
* Test reading preferences
*
*/
public void testReadPreferences() throws DAOException {
PreferencesDAO dao = DAOFactory.getFactory().getPreferencesDAO();
PreferencesTO preferences = dao.readPreferences();
// Assert review preferences are not null
assertNotNull("Review additional comment should not be null", preferences.getReviewAdditionalComment());
assertNotNull("No PMD String should not be null", preferences.getReviewNoPmdString());
assertNotNull("There should be a configurations object", preferences.getConfigurations());
assertFalse("CPD minimum tile size should not be 0", preferences.getCpdMinTileSize() == 0);
}
/**
* Test writing preferences
* @throws DAOException
*/
public void testWritePreferences() throws DAOException {
PreferencesDAO dao = DAOFactory.getFactory().getPreferencesDAO();
PreferencesTO preferences = new PreferencesTO();
preferences.setCpdMinTileSize(25);
preferences.setReviewAdditionalComment("a review additional comment");
preferences.setDfaEnabled(false);
preferences.setReviewNoPmdString("NOPMD");
preferences.setSwitchPmdPerspective(true);
preferences.setConfigurations(new ConfigurationsTO());
dao.writePreferences(preferences);
IPreferenceStore store = PMDPlugin.getDefault().getPreferenceStore();
assertEquals("DFA has not been set correctly", -1, store.getInt("net.sourceforge.pmd.eclipse.use_dfa"));
assertEquals("Switch to PMD perspective has not been set correctly", 1, store.getInt("net.sourceforge.pmd.eclipse.show_perspective_on_check"));
assertEquals("CPD min tile size has not been set correctly", 25, store.getInt("net.sourceforge.pmd.eclipse.CPDPreference.mintilesize"));
assertEquals("Review additional comment has not been set correctly", "a review additional comment", store.getString("net.sourceforge.pmd.eclipse.review_additional_comment"));
assertEquals("No PMD String has not been set correctly", "NOPMD", store.getString("net.sourceforge.pmd.eclipse.no_pmd_string"));
IPath stateLocation = PMDPlugin.getDefault().getStateLocation();
File confFile = stateLocation.append("configurations.xml").toFile();
assertNotNull("Conf File object is not expected to be null", confFile);
assertTrue("Conf file should exist", confFile.canRead());
}
}

View File

@ -1,68 +0,0 @@
/*
* Created on 6 f<EFBFBD>vr. 2005
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package test.net.sourceforge.pmd.eclipse.model;
import net.sourceforge.pmd.eclipse.model.ModelFactory;
import net.sourceforge.pmd.eclipse.model.PreferencesModel;
import junit.framework.TestCase;
/**
*
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.2 2005/12/30 16:29:16 phherlin
* Implement a new preferences model and review some tests
*
* Revision 1.1 2005/06/15 21:14:56 phherlin
* Create the project for the Eclipse plugin unit tests
*
*
*/
public class PreferencesModelTest extends TestCase {
private static final String REVIEW_ADDITIONAL_COMMENT = "sample review additional comment";
/**
* Test case constructor
* @param name of the test case
*/
public PreferencesModelTest(String name) {
super(name);
}
}