From d3f923f863f769b2ca72fb691c052d4f7db13899 Mon Sep 17 00:00:00 2001 From: Philippe Herlin Date: Sun, 18 Jun 2006 22:30:33 +0000 Subject: [PATCH] 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 --- pmd-eclipse-test/build.properties | 6 +- pmd-eclipse-test/plugin.xml | 8 +- pmd-eclipse-test/test.xml | 4 +- .../pmd/core/PMDCorePluginTest.java | 64 +- .../pmd/core/RuleSetManagerTest.java | 5 +- .../ext/RuleSetsExtensionProcessorTest.java | 45 +- .../sourceforge/pmd/eclipse/BasicPMDTest.java | 58 +- .../sourceforge/pmd/eclipse/EclipseUtils.java | 138 ++-- .../cmd/DetectCutAndPasteCmdTest.java | 57 +- .../pmd/runtime}/cmd/RenderReportCmdTest.java | 71 ++- .../pmd/runtime}/cmd/ReviewCmdTest.java | 9 +- .../runtime/preferences/vo/PriorityTest.java | 185 ++++++ .../preferences/vo/PropertiesTest.java | 306 +++++++++ .../runtime/preferences/vo/PropertyTest.java | 255 ++++++++ .../runtime/preferences/vo/RuleSetTest.java | 599 ++++++++++++++++++ .../pmd/runtime/preferences/vo/RuleTest.java | 414 ++++++++++++ .../ProjectPropertiesModelTest.java | 238 ++++--- .../UpdateProjectPropertiesCmdTest.java | 29 +- .../pmd/eclipse/dao/PreferencesDAOTest.java | 111 ---- .../eclipse/model/PreferencesModelTest.java | 68 -- 20 files changed, 2171 insertions(+), 499 deletions(-) rename pmd-eclipse-test/test/{test => }/net/sourceforge/pmd/core/PMDCorePluginTest.java (82%) rename pmd-eclipse-test/test/{test => }/net/sourceforge/pmd/core/RuleSetManagerTest.java (98%) rename pmd-eclipse-test/test/{test => }/net/sourceforge/pmd/core/ext/RuleSetsExtensionProcessorTest.java (84%) rename pmd-eclipse-test/test/{test => }/net/sourceforge/pmd/eclipse/BasicPMDTest.java (82%) rename pmd-eclipse-test/test/{test => }/net/sourceforge/pmd/eclipse/EclipseUtils.java (79%) rename pmd-eclipse-test/test/{test/net/sourceforge/pmd/eclipse => net/sourceforge/pmd/runtime}/cmd/DetectCutAndPasteCmdTest.java (86%) rename pmd-eclipse-test/test/{test/net/sourceforge/pmd/eclipse => net/sourceforge/pmd/runtime}/cmd/RenderReportCmdTest.java (85%) rename pmd-eclipse-test/test/{test/net/sourceforge/pmd/eclipse => net/sourceforge/pmd/runtime}/cmd/ReviewCmdTest.java (96%) create mode 100644 pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PriorityTest.java create mode 100644 pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertiesTest.java create mode 100644 pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertyTest.java create mode 100644 pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleSetTest.java create mode 100644 pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleTest.java rename pmd-eclipse-test/test/{test/net/sourceforge/pmd/eclipse/model => net/sourceforge/pmd/runtime/properties}/ProjectPropertiesModelTest.java (61%) rename pmd-eclipse-test/test/{test/net/sourceforge/pmd/eclipse => net/sourceforge/pmd/ui}/properties/UpdateProjectPropertiesCmdTest.java (74%) delete mode 100644 pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/dao/PreferencesDAOTest.java delete mode 100644 pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/model/PreferencesModelTest.java diff --git a/pmd-eclipse-test/build.properties b/pmd-eclipse-test/build.properties index f14e8c4d9b..de313fdf5e 100644 --- a/pmd-eclipse-test/build.properties +++ b/pmd-eclipse-test/build.properties @@ -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 diff --git a/pmd-eclipse-test/plugin.xml b/pmd-eclipse-test/plugin.xml index 25b65edeeb..ef53f34aa4 100644 --- a/pmd-eclipse-test/plugin.xml +++ b/pmd-eclipse-test/plugin.xml @@ -1,13 +1,13 @@ - + @@ -20,6 +20,8 @@ + + diff --git a/pmd-eclipse-test/test.xml b/pmd-eclipse-test/test.xml index fe0a36e202..e17cca9469 100644 --- a/pmd-eclipse-test/test.xml +++ b/pmd-eclipse-test/test.xml @@ -6,7 +6,7 @@ - + @@ -21,7 +21,7 @@ - + diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/core/PMDCorePluginTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/core/PMDCorePluginTest.java similarity index 82% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/core/PMDCorePluginTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/core/PMDCorePluginTest.java index f776c7b116..b4b850c733 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/core/PMDCorePluginTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/core/PMDCorePluginTest.java @@ -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; } } diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/core/RuleSetManagerTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/core/RuleSetManagerTest.java similarity index 98% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/core/RuleSetManagerTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/core/RuleSetManagerTest.java index 8d6e5d9c52..f30fa57c34 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/core/RuleSetManagerTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/core/RuleSetManagerTest.java @@ -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 * diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/core/ext/RuleSetsExtensionProcessorTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/core/ext/RuleSetsExtensionProcessorTest.java similarity index 84% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/core/ext/RuleSetsExtensionProcessorTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/core/ext/RuleSetsExtensionProcessorTest.java index 4be9437b4b..c4fb0f6101 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/core/ext/RuleSetsExtensionProcessorTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/core/ext/RuleSetsExtensionProcessorTest.java @@ -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; } } diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java similarity index 82% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java index ae46507df5..488e4d17e7 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/BasicPMDTest.java @@ -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]); diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/EclipseUtils.java b/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/EclipseUtils.java similarity index 79% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/EclipseUtils.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/EclipseUtils.java index 6027a7e685..a90c82e792 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/EclipseUtils.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/eclipse/EclipseUtils.java @@ -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)); } } - + } diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/DetectCutAndPasteCmdTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/DetectCutAndPasteCmdTest.java similarity index 86% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/DetectCutAndPasteCmdTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/DetectCutAndPasteCmdTest.java index 0a9e97f43a..f2720aa0d2 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/DetectCutAndPasteCmdTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/DetectCutAndPasteCmdTest.java @@ -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; } } diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/RenderReportCmdTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/RenderReportCmdTest.java similarity index 85% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/RenderReportCmdTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/RenderReportCmdTest.java index 8eae948720..cf81838555 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/RenderReportCmdTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/RenderReportCmdTest.java @@ -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; } } diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/ReviewCmdTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/ReviewCmdTest.java similarity index 96% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/ReviewCmdTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/ReviewCmdTest.java index 8cd78709a9..5c4d16f261 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/cmd/ReviewCmdTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/cmd/ReviewCmdTest.java @@ -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 * diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PriorityTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PriorityTest.java new file mode 100644 index 0000000000..719962f1d9 --- /dev/null +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PriorityTest.java @@ -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()); + } + +} diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertiesTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertiesTest.java new file mode 100644 index 0000000000..9879165124 --- /dev/null +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertiesTest.java @@ -0,0 +1,306 @@ +/* + * Created on 18 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 java.util.HashSet; +import java.util.Set; + +import junit.framework.TestCase; + +/** + * Unit tests for Properties object + * + * @author Herlin + * @version $Revision$ + * + * $Log$ + * Revision 1.1 2006/06/18 22:29:51 phherlin + * Begin refactoring the unit tests for the plugin + * + * + */ + +public class PropertiesTest extends TestCase { + + /** + * A new properties object must have its set of properties not null and empty + * + */ + public void testDefaults() { + Properties p = new Properties(); + assertNotNull("Properties set cannot be null", p.getProperties()); + assertEquals("Properties set should be empty", 0, p.getProperties().size()); + } + + /** + * Setting any set as properties is allowed for now ! + * + */ + public void testSetProperties1() { + try { + Properties p = new Properties(); + Set set = new HashSet(); + set.add("foo"); + p.setProperties(set); + } catch (IllegalArgumentException e) { + fail("Setting any set as properties is allowed for now!"); + } + } + + /** + * Setting a null set as properties is illegal + * + */ + public void testSetProperties2() { + try { + Properties p = new Properties(); + p.setProperties(null); + fail("Setting a null set as properties is illegal"); + } catch (IllegalArgumentException e) { + // success! + } + } + + /** + * Obviously an instance is equals to itself + * + */ + public void testEquals1() { + Properties p = new Properties(); + assertEquals("An instance should be equals to itself", p, p); + } + + /** + * 2 different properties instances are equals if their set are the same + */ + public void testEquals2() { + Properties p1 = new Properties(); + Set set = new HashSet(); + set.add("foo"); + p1.setProperties(set); + Properties p2 = new Properties(); + p2.setProperties(set); + + assertEquals("2 different properties instances are equals if their set are the same", p1, p2); + } + + /** + * 2 different properties instances are equals if their set are the same. + * same as previous one but with a legal properties set. + */ + public void testEquals3() { + Property prop = new Property(); + prop.setName("p1"); + prop.setValue("value1"); + Set set = new HashSet(); + set.add(prop); + + Properties p1 = new Properties(); + p1.setProperties(set); + Properties p2 = new Properties(); + p2.setProperties(set); + + assertEquals("2 different properties instances are equals if their set are the same", p1, p2); + } + + /** + * 2 different properties instances are equals if their set are the same. + * same as previous one but with 2 differents sets + */ + public void testEquals4() { + Property prop = new Property(); + prop.setName("p1"); + prop.setValue("value1"); + + Set set1 = new HashSet(); + set1.add(prop); + + Set set2 = new HashSet(); + set2.add(prop); + + Properties p1 = new Properties(); + p1.setProperties(set1); + Properties p2 = new Properties(); + p2.setProperties(set2); + + assertEquals("2 different properties instances are equals if their set are the same", p1, p2); + } + + /** + * 2 different properties instances are equals if their set are the same. + * same as previous one but with 2 differents instances of property objets. + */ + public void testEquals5() { + Property prop1 = new Property(); + prop1.setName("p1"); + prop1.setValue("value1"); + + Property prop2 = new Property(); + prop2.setName("p1"); + prop2.setValue("value1"); + + Set set1 = new HashSet(); + set1.add(prop1); + + Set set2 = new HashSet(); + set2.add(prop2); + + Properties p1 = new Properties(); + p1.setProperties(set1); + Properties p2 = new Properties(); + p2.setProperties(set2); + + assertEquals("2 different properties instances are equals if their set are the same", p1, p2); + } + + /** + * 2 different properties instances are different if their set are the different + */ + public void testEquals6() { + Property prop1 = new Property(); + prop1.setName("p1"); + prop1.setValue("value1"); + + Property prop2 = new Property(); + prop2.setName("p2"); + prop2.setValue("value2"); + + Set set1 = new HashSet(); + set1.add(prop1); + + Set set2 = new HashSet(); + set1.add(prop2); + + Properties p1 = new Properties(); + p1.setProperties(set1); + Properties p2 = new Properties(); + p2.setProperties(set2); + + assertFalse("2 different properties instances are different if their set are the different", p1.equals(p2)); + } + + /** + * A properties Object cannot be equals to null + * + */ + public void testEquals7() { + Property prop1 = new Property(); + prop1.setName("p1"); + prop1.setValue("value1"); + + Set set1 = new HashSet(); + set1.add(prop1); + + Properties p1 = new Properties(); + p1.setProperties(set1); + + assertFalse("A properties object cannot be equals to null", p1.equals(null)); + } + + /** + * A properties Object cannot be equals to any other objects + * + */ + public void testEquals8() { + Property prop1 = new Property(); + prop1.setName("p1"); + prop1.setValue("value1"); + + Set set1 = new HashSet(); + set1.add(prop1); + + Properties p1 = new Properties(); + p1.setProperties(set1); + + assertFalse("A properties Object cannot be equals to any other objects", p1.equals(prop1)); + assertFalse("A properties Object cannot be equals to any other objects", p1.equals(set1)); + } + + /** + * 2 equal properties must have the same hashCode + * + */ + public void testHashCode1() { + Property prop1 = new Property(); + prop1.setName("p1"); + prop1.setValue("value1"); + + Property prop2 = new Property(); + prop2.setName("p1"); + prop2.setValue("value1"); + + Set set1 = new HashSet(); + set1.add(prop1); + + Set set2 = new HashSet(); + set2.add(prop2); + + Properties p1 = new Properties(); + p1.setProperties(set1); + Properties p2 = new Properties(); + p2.setProperties(set2); + + assertEquals("2 equal properties must have the same hashCode", p1.hashCode(), p2.hashCode()); + } + + /** + * 2 different properties must have the different hashCode + * + */ + public void testHashCode2() { + Property prop1 = new Property(); + prop1.setName("p1"); + prop1.setValue("value1"); + + Property prop2 = new Property(); + prop2.setName("p2"); + prop2.setValue("value2"); + + Set set1 = new HashSet(); + set1.add(prop1); + + Set set2 = new HashSet(); + set2.add(prop2); + + Properties p1 = new Properties(); + p1.setProperties(set1); + Properties p2 = new Properties(); + p2.setProperties(set2); + + assertFalse("2 different properties must have the different hashCode", p1.hashCode() == p2.hashCode()); + } +} diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertyTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertyTest.java new file mode 100644 index 0000000000..ba62fb7351 --- /dev/null +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/PropertyTest.java @@ -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()); + } + +} diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleSetTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleSetTest.java new file mode 100644 index 0000000000..b560d1d68e --- /dev/null +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleSetTest.java @@ -0,0 +1,599 @@ +/* + * Created on 18 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 java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; + +import junit.framework.TestCase; + +/** + * Unit tests for the RuleSet class + * + * @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 RuleSetTest extends TestCase { + + /** + * Test the default attributes of a rule set + * + */ + public void testDefaults() { + RuleSet rs = new RuleSet(); + + assertNotNull("Name should not be null", rs.getName()); + assertNotNull("Description should not be null", rs.getDescription()); + assertNotNull("Rules should not be null", rs.getRules()); + assertNotNull("Language should not be null", rs.getLanguage()); + + assertEquals("Name should be empty", 0, rs.getName().length()); + assertEquals("Description should be empty", 0, rs.getDescription().length()); + assertEquals("Rules collecction should be empty", 0, rs.getRules().size()); + assertEquals("Default language should be set to Java", RuleSet.LANGUAGE_JAVA, rs.getLanguage()); + } + + /** + * Setting any name is legal + * + */ + public void testSetName1() { + try { + RuleSet rs = new RuleSet(); + rs.setName("any name"); + } catch (IllegalArgumentException e) { + fail("setting name to any name is legal"); + } + } + + /** + * Setting name to null is illegal + * + */ + public void testSetName2() { + try { + RuleSet rs = new RuleSet(); + rs.setName(null); + fail("setting name to null is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting name to empty string is illegal + * + */ + public void testSetName3() { + try { + RuleSet rs = new RuleSet(); + rs.setName(""); + fail("setting name to empty string is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting name to empty string is illegal + * + */ + public void testSetName4() { + try { + RuleSet rs = new RuleSet(); + rs.setName(" \t\n"); + fail("setting name to empty string is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting any description is legal + * + */ + public void testSetDescription1() { + try { + RuleSet rs = new RuleSet(); + rs.setDescription("any description"); + } catch (IllegalArgumentException e) { + fail("setting description to any string is legal"); + } + } + + /** + * Setting description to null is illegal + * + */ + public void testSetDescription2() { + try { + RuleSet rs = new RuleSet(); + rs.setDescription(null); + fail("setting description to null is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting Description to empty string is legal + * + */ + public void testSetDescription3() { + try { + RuleSet rs = new RuleSet(); + rs.setDescription(""); + } catch (IllegalArgumentException e) { + fail("setting description to empty string is legal"); + } + } + + /** + * Setting Description to empty string is legal + * + */ + public void testSetDescription4() { + try { + RuleSet rs = new RuleSet(); + rs.setDescription(" \t\n"); + assertEquals("The string should not be modified", " \t\n", rs.getDescription()); + } catch (IllegalArgumentException e) { + fail("setting description to empty string is legal"); + } + } + + /** + * Language could be assigned the JAVA value + * + */ + public void testSetLanguage1() { + try { + RuleSet rs = new RuleSet(); + rs.setLanguage("JAVA"); + } catch (IllegalArgumentException e) { + fail("setting language to JAVA is legal"); + } + } + + /** + * Language could be assigned the JSP value + * + */ + public void testSetLanguage2() { + try { + RuleSet rs = new RuleSet(); + rs.setLanguage("JSP"); + } catch (IllegalArgumentException e) { + fail("setting language to JSP is legal"); + } + } + + /** + * Setting language to null is illegal + * + */ + public void testSetLanguage3() { + try { + RuleSet rs = new RuleSet(); + rs.setLanguage(null); + fail("setting language to null is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting language to empty string is illegal + * + */ + public void testSetLanguage4() { + try { + RuleSet rs = new RuleSet(); + rs.setLanguage(""); + fail("setting language to empty string is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting language to empty string is illegal + * + */ + public void testSetLanguage5() { + try { + RuleSet rs = new RuleSet(); + rs.setLanguage(" \t\n"); + fail("setting language to empty string is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting language to any string is illegal + * + */ + public void testSetLanguage6() { + try { + RuleSet rs = new RuleSet(); + rs.setLanguage("any string"); + fail("setting language to any string is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting rules to any collection is legal for now whatever it contains ! + * wait for JDK 1.5 implementation to do better + * + */ + public void testSetRules1() { + try { + Collection c = new HashSet(); + c.add("foo"); + RuleSet rs = new RuleSet(); + rs.setRules(c); + } catch (IllegalArgumentException e) { + fail("setting rules to any collection is legal !"); + } + } + + /** + * Setting rules to null is illegal. + * + */ + public void testSetRules2() { + try { + RuleSet rs = new RuleSet(); + rs.setRules(null); + fail("setting rules to null is illegal !"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * An instance of a rule set is equals to itself + * + */ + public void testEquals1() { + RuleSet rs = new RuleSet(); + rs.setName("default"); + assertEquals("A ruleset is equal to itself", rs, rs); + } + + /** + * A rule set is never equal to null + * + */ + public void testEquals2() { + RuleSet rs = new RuleSet(); + rs.setName("default"); + assertFalse("A ruleset is never equal to null", rs.equals(null)); + } + + /** + * RuleSets are equal if they have the same name, language and rules + * collection whatever the description. + * + */ + public void testEquals3() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("default"); + rs2.setLanguage(RuleSet.LANGUAGE_JSP); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertEquals("These rule sets should be equals", rs1, rs2); + } + + /** + * Rule sets with different names are different + * + */ + public void testEquals4() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("custom"); + rs2.setLanguage(RuleSet.LANGUAGE_JSP); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertFalse("Rule sets with different names are different", rs1.equals(rs2)); + } + + /** + * Rule sets with different languages are different + * + */ + public void testEquals5() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("default"); + rs2.setLanguage(RuleSet.LANGUAGE_JAVA); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertFalse("Rule sets with different languages are different", rs1.equals(rs2)); + } + + /** + * Rule sets with different rules collection are different + * + */ + public void testEquals7() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to yet another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("default"); + rs2.setLanguage(RuleSet.LANGUAGE_JSP); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertFalse("Rule sets with different rules collections are different", rs1.equals(rs2)); + } + + /** + * Equal rule sets must have the same hash code + * + */ + public void testHashCode1() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("default"); + rs2.setLanguage(RuleSet.LANGUAGE_JSP); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertEquals("Equal rule sets must have the same hash code", rs1.hashCode(), rs2.hashCode()); + } + + /** + * Different rule sets should have different hash codes + * + */ + public void testHashCode2() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("custom"); + rs2.setLanguage(RuleSet.LANGUAGE_JSP); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertFalse("Different rule sets should have different hash codes", rs1.hashCode() == rs2.hashCode()); + } + + /** + * Different rule sets should have different hash codes + * + */ + public void testHashCode3() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("default"); + rs2.setLanguage(RuleSet.LANGUAGE_JAVA); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertFalse("Different rule sets should have different hash codes", rs1.hashCode() == rs2.hashCode()); + } + + /** + * Different rule sets should have different hash codes + * + */ + public void testHashCode4() { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + Collection c1 = new ArrayList(); + c1.add(r1); + c1.add(r2); + + Rule r3 = new Rule(); + r3.setRef("ref to a rule"); + Rule r4 = new Rule(); + r4.setRef("ref to yet another rule"); + Collection c2 = new ArrayList(); + c2.add(r3); + c2.add(r4); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.setRules(c1); + + RuleSet rs2 = new RuleSet(); + rs2.setName("default"); + rs2.setLanguage(RuleSet.LANGUAGE_JSP); + rs2.setRules(c2); + rs2.setDescription("Description does not make the difference"); + + assertFalse("Different rule sets should have different hash codes", rs1.hashCode() == rs2.hashCode()); + } +} diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleTest.java new file mode 100644 index 0000000000..f4a8680bf6 --- /dev/null +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/preferences/vo/RuleTest.java @@ -0,0 +1,414 @@ +/* + * Created on 18 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 for class Rule + * + * @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 RuleTest extends TestCase { + + /** + * Asserts the defaults of a new rule object + * + */ + public void testDefaults() { + Rule r = new Rule(); + assertNotNull("The ref attribute must not be null", r.getRef()); + assertEquals("The ref attribute should be empty", 0, r.getRef().trim().length()); + assertNull("The message attribute should be null", r.getMessage()); + assertNull("The priority attribute should be null", r.getPriority()); + assertNull("The properties attribute should be null", r.getProperties()); + } + + /** + * Setting a null ref attribute is illegal + * + */ + public void testSetRef1() { + try { + Rule r = new Rule(); + r.setRef(null); + fail("Setting a null ref attribute is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting an empty ref attribute is illegal + * + */ + public void testSetRef2() { + try { + Rule r = new Rule(); + r.setRef(""); + fail("Setting an empty string for the ref attribute is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * Setting an empty ref attribute is illegal. + * Test with blank characters. + * + */ + public void testSetRef3() { + try { + Rule r = new Rule(); + r.setRef(" \r\n"); + fail("Setting an empty string for the ref attribute is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } + + /** + * To be sure, a ref attribute should be assigned any string + * + */ + public void testSetRef4() { + try { + Rule r = new Rule(); + r.setRef("any string"); + } catch (IllegalArgumentException e) { + fail("It should be legal to set any string to ref attribute"); + } + } + + /** + * an instance of a Rule object is equal to itself + */ + public void testEquals1() { + Rule r = new Rule(); + r.setRef("A reference"); + assertEquals("A instance of Rule is equals to itself", r, r); + } + + /** + * a rule cannot be equals to null + */ + public void testEquals2() { + Rule r = new Rule(); + r.setRef("A reference"); + assertFalse("A rule cannot be equal to null", r.equals(null)); + } + + /** + * To be equals, rules must share the same ref, priority and properties. + * + */ + public void testEquals3() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value1"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to a rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL3); + r2.setMessage("Message doesn't make the difference"); + + assertEquals("These 2 rules must be equal", r1, r2); + + } + + /** + * Rules with a different ref attribute are not equals + * + */ + public void testEquals4() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value1"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL3); + r2.setMessage("Message doesn't make the difference"); + + assertFalse("Rules with different ref attribute are different", r1.equals(r2)); + + } + + /** + * Rules with a different priority are not equals + * + */ + public void testEquals5() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value1"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to a rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL1); + r2.setMessage("Message doesn't make the difference"); + + assertFalse("Rules with different priority are different", r1.equals(r2)); + + } + + /** + * Rules with different properties are not equals + * + */ + public void testEquals6() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value2"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to a rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL3); + r2.setMessage("Message doesn't make the difference"); + + assertFalse("Rules with different properties are different", r1.equals(r2)); + + } + + /** + * Equal rules have the same hash code + * + */ + public void testHashCode1() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value1"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to a rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL3); + r2.setMessage("Message doesn't make the difference"); + + assertEquals("Equal rules have the same hashcode", r1.hashCode(), r2.hashCode()); + + } + + /** + * Different rules should have different hashCode + * + */ + public void testHashCode2() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value1"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL3); + r2.setMessage("Message doesn't make the difference"); + + assertFalse("Different rules should have different hash code", r1.hashCode() == r2.hashCode()); + + } + + /** + * Different rules should have different hash code + * + */ + public void testHashCode3() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value1"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to a rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL1); + r2.setMessage("Message doesn't make the difference"); + + assertFalse("Different rules should have different hash code", r1.hashCode() == r2.hashCode()); + + } + + /** + * Different rules should have different hash code + * + */ + public void testHashCode4() { + Property p1 = new Property(); + p1.setName("p1"); + p1.setValue("value1"); + + Properties s1 = new Properties(); + s1.getProperties().add(p1); + + Property p2 = new Property(); + p2.setName("p1"); + p2.setValue("value2"); + + Properties s2 = new Properties(); + s2.getProperties().add(p2); + + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setProperties(s1); + r1.setPriority(Priority.LEVEL3); + + Rule r2 = new Rule(); + r2.setRef("ref to a rule"); + r2.setProperties(s2); + r2.setPriority(Priority.LEVEL3); + r2.setMessage("Message doesn't make the difference"); + + assertFalse("Different rules should have different hash code", r1.hashCode() == r2.hashCode()); + + } +} diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/model/ProjectPropertiesModelTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/properties/ProjectPropertiesModelTest.java similarity index 61% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/model/ProjectPropertiesModelTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/runtime/properties/ProjectPropertiesModelTest.java index 90c54ab33b..4f8e8e8c66 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/model/ProjectPropertiesModelTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/properties/ProjectPropertiesModelTest.java @@ -33,10 +33,9 @@ * 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; +package net.sourceforge.pmd.runtime.properties; import java.util.Iterator; -import java.util.Set; import junit.framework.TestCase; import net.sourceforge.pmd.AbstractRule; @@ -44,19 +43,20 @@ import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetNotFoundException; -import net.sourceforge.pmd.eclipse.PMDPlugin; -import net.sourceforge.pmd.eclipse.PMDPluginConstants; -import net.sourceforge.pmd.eclipse.builder.PMDNature; -import net.sourceforge.pmd.eclipse.model.ModelException; -import net.sourceforge.pmd.eclipse.model.ProjectPropertiesModel; -import net.sourceforge.pmd.eclipse.model.ProjectPropertiesModelImpl; +import net.sourceforge.pmd.eclipse.EclipseUtils; +import net.sourceforge.pmd.runtime.PMDRuntimePlugin; +import net.sourceforge.pmd.runtime.builder.PMDNature; +import net.sourceforge.pmd.runtime.preferences.IPreferencesManager; +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.runtime.properties.impl.ProjectPropertiesImpl; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.ui.IWorkingSet; -import test.net.sourceforge.pmd.eclipse.EclipseUtils; /** * Test the project properties model. @@ -65,19 +65,20 @@ import test.net.sourceforge.pmd.eclipse.EclipseUtils; * @version $Revision$ * * $Log$ - * Revision 1.4 2005/12/30 16:29:16 phherlin - * Implement a new preferences model and review some tests - * - * Revision 1.3 2005/07/02 14:31:11 phherlin - * Fixing equals assertion to test on rules collection instead - * - * Revision 1.2 2005/07/01 00:06:38 phherlin - * Refactoring and writing more tests - * - * Revision 1.1 2005/06/15 21:14:56 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.4 2005/12/30 16:29:16 + * phherlin Implement a new preferences model and review some tests + * + * Revision 1.3 2005/07/02 14:31:11 phherlin Fixing equals assertion to test on + * rules collection instead + * + * Revision 1.2 2005/07/01 00:06:38 phherlin Refactoring and writing more tests + * + * Revision 1.1 2005/06/15 21:14:56 phherlin Create the project for the Eclipse + * plugin unit tests + * + * */ public class ProjectPropertiesModelTest extends TestCase { private IProject testProject; @@ -86,8 +87,7 @@ public class ProjectPropertiesModelTest extends TestCase { /** * Test case constructor * - * @param name - * of the test case + * @param name of the test case */ public ProjectPropertiesModelTest(String name) { super(name); @@ -96,10 +96,11 @@ public class ProjectPropertiesModelTest extends TestCase { /** * A property should be used to know if PMD is enabled for a project. Set to * TRUE - * + * */ - public void testPmdEnabledTRUE() throws ModelException, CoreException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testPmdEnabledTRUE() throws CoreException, PropertiesException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); model.setPmdEnabled(true); model.sync(); @@ -110,10 +111,11 @@ public class ProjectPropertiesModelTest extends TestCase { /** * A property should be used to know id PMD is enabled for a project. Set to * FALSE. - * + * */ - public void testPmdEnabledFALSE() throws ModelException, CoreException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testPmdEnabledFALSE() throws PropertiesException, CoreException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); model.setPmdEnabled(true); model.sync(); @@ -129,23 +131,29 @@ public class ProjectPropertiesModelTest extends TestCase { /** * A brand new project should be affected the Plugin ruleset in the global * ruleset. - * + * */ - public void testProjectRuleSet() throws ModelException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); - assertSame("A new project a is not set the plugin ruleset", model.getProjectRuleSet(), PMDPlugin.getDefault().getRuleSet()); + public void testProjectRuleSet() throws PropertiesException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + + IPreferencesManager pmgr = PMDRuntimePlugin.getDefault().getPreferencesManager(); + + assertSame("A new project is not set the plugin ruleset", model.getProjectRuleSet(), pmgr.getRuleSet()); } /** * Set another ruleset. */ - public void testProjectRuleSet1() throws ModelException, RuleSetNotFoundException, CoreException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testProjectRuleSet1() throws PropertiesException, RuleSetNotFoundException, CoreException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + RuleSetFactory factory = new RuleSetFactory(); // use the basic ruleset because it should be included in the plugin // ruleset. - RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); // First set the project ruleset model.setProjectRuleSet(basicRuleSet); @@ -153,38 +161,51 @@ public class ProjectPropertiesModelTest extends TestCase { // Test the ruleset we set is equal to the ruleset we queried RuleSet projectRuleSet = model.getProjectRuleSet(); - assertNotNull("Project ruleset has not been set", projectRuleSet); - assertTrue("The project ruleset is not the basic ruleset", EclipseUtils.assertRuleSetEquals(basicRuleSet.getRules(), projectRuleSet.getRules())); + assertNotNull("Project ruleset has not been set", projectRuleSet); + assertTrue("The project ruleset is not the basic ruleset", EclipseUtils.assertRuleSetEquals(basicRuleSet.getRules(), + projectRuleSet.getRules())); } /** - * When rules are removed from the plugin preferences, these rules should also be removed from the project + * When rules are removed from the plugin preferences, these rules should + * also be removed from the project */ - public void testProjectRuleSet2() throws ModelException, RuleSetNotFoundException, CoreException { + public void testProjectRuleSet2() throws PropertiesException, RuleSetNotFoundException, CoreException { + + // First ensure that the plugin initial ruleset is equal to the project + // ruleset + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); - // First ensure that the plugin initial ruleset is equal to the project ruleset - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); RuleSet projectRuleSet = model.getProjectRuleSet(); - assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet.getRules(), projectRuleSet.getRules()); - + assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet.getRules(), projectRuleSet + .getRules()); + // use the basic ruleset and set it at the only plugin ruleset RuleSetFactory factory = new RuleSetFactory(); - RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); - PMDPlugin.getDefault().setRuleSet(basicRuleSet, null); + RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); + + IPreferencesManager pmgr = PMDRuntimePlugin.getDefault().getPreferencesManager(); + pmgr.setRuleSet(basicRuleSet); + projectRuleSet = model.getProjectRuleSet(); - + assertEquals("The project ruleset is not equal to the plugin ruleset", basicRuleSet.getRules(), projectRuleSet.getRules()); } /** - * When rules are added to the plugin preferences, these rules should also be added to the project + * When rules are added to the plugin preferences, these rules should also + * be added to the project */ - public void testProjectRuleSet3() throws ModelException, RuleSetNotFoundException, CoreException { + public void testProjectRuleSet3() throws PropertiesException, RuleSetNotFoundException, CoreException { RuleSetFactory factory = new RuleSetFactory(); - RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); + + // First ensure that the plugin initial ruleset is equal to the project + // ruleset + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); - // First ensure that the plugin initial ruleset is equal to the project ruleset - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); RuleSet projectRuleSet = model.getProjectRuleSet(); assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet, projectRuleSet); @@ -194,37 +215,42 @@ public class ProjectPropertiesModelTest extends TestCase { return "MyRule"; } }; - + RuleSet newRuleSet = new RuleSet(); newRuleSet.setName("foo"); newRuleSet.addRuleSet(this.initialPluginRuleSet); newRuleSet.addRule(myRule); - PMDPlugin.getDefault().setRuleSet(newRuleSet, null); - - // Test that the project rule set should still be the same as the plugin rule set - model = new ProjectPropertiesModelImpl(this.testProject); + PMDRuntimePlugin.getDefault().getPreferencesManager().setRuleSet(newRuleSet); + + // Test that the project rule set should still be the same as the plugin + // rule set + model = new ProjectPropertiesImpl(this.testProject, mgr); projectRuleSet = model.getProjectRuleSet(); - assertEquals("The project ruleset is not equal to the plugin ruleset", PMDPlugin.getDefault().getRuleSet().getRules(), projectRuleSet.getRules()); + assertEquals("The project ruleset is not equal to the plugin ruleset", PMDRuntimePlugin.getDefault() + .getPreferencesManager().getRuleSet().getRules(), projectRuleSet.getRules()); } /** * Bug: when a user deselect a project rule it is not saved */ - public void testBug() throws ModelException, RuleSetNotFoundException, CoreException { + public void testBug() throws PropertiesException, RuleSetNotFoundException, CoreException { RuleSetFactory factory = new RuleSetFactory(); - // First ensure that the plugin initial ruleset is equal to the project ruleset - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(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", initialPluginRuleSet, projectRuleSet); - + assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet, projectRuleSet); + // 2. remove the first rule (keep its name for assertion) RuleSet newRuleSet = new RuleSet(); newRuleSet.addRuleSet(projectRuleSet); Iterator i = newRuleSet.getRules().iterator(); Rule removedRule = (Rule) i.next(); i.remove(); - + model.setProjectRuleSet(newRuleSet); model.sync(); @@ -235,14 +261,16 @@ public class ProjectPropertiesModelTest extends TestCase { /** * It should not be possible to set to null a project ruleset - * + * */ - public void testProjectRuleSetNull() throws ModelException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testProjectRuleSetNull() throws PropertiesException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + try { model.setProjectRuleSet(null); fail("A ModelException must be raised when setting a project ruleset to null"); - } catch (ModelException e) { + } catch (PropertiesException e) { // OK that's correct } @@ -252,23 +280,25 @@ public class ProjectPropertiesModelTest extends TestCase { * A project may have its ruleset stored in the project own directory. Test * set to TRUE. */ - public void testRuleSetStoredInProjectTRUE() throws ModelException, RuleSetNotFoundException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testRuleSetStoredInProjectTRUE() throws PropertiesException, RuleSetNotFoundException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + RuleSetFactory factory = new RuleSetFactory(); - RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); model.setPmdEnabled(true); model.setRuleSetStoredInProject(false); model.setProjectWorkingSet(null); model.setProjectRuleSet(basicRuleSet); model.sync(); - + model.createDefaultRuleSetFile(); model.setRuleSetStoredInProject(true); model.sync(); - + boolean b = model.isRuleSetStoredInProject(); - IFile file = this.testProject.getFile(PMDPluginConstants.PROJECT_RULESET_FILE); - RuleSet projectRuleSet = factory.createRuleSet(file.getLocation().toOSString()); + IFile file = this.testProject.getFile(".ruleset"); + RuleSet projectRuleSet = factory.createSingleRuleSet(file.getLocation().toOSString()); assertTrue("the ruleset should be stored in the project", b); assertEquals("The project ruleset must be equals to the one found in the project", model.getProjectRuleSet(), projectRuleSet); @@ -277,12 +307,14 @@ public class ProjectPropertiesModelTest extends TestCase { /** * A project may have its ruleset stored in the project own directory. Test * set to FALSE. - * + * */ - public void testRuleSetStoredInProjectFALSE() throws ModelException, RuleSetNotFoundException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testRuleSetStoredInProjectFALSE() throws PropertiesException, RuleSetNotFoundException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + RuleSetFactory factory = new RuleSetFactory(); - RuleSet basicRuleSet = factory.createRuleSet("rulesets/basic.xml"); + RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); model.setPmdEnabled(true); model.setRuleSetStoredInProject(false); model.setProjectWorkingSet(null); @@ -301,10 +333,12 @@ public class ProjectPropertiesModelTest extends TestCase { /** * A project may work only on a subset of files defined by a working set - * + * */ - public void testProjectWorkingSetNull() throws ModelException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testProjectWorkingSetNull() throws PropertiesException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + model.setProjectWorkingSet(null); IWorkingSet w = model.getProjectWorkingSet(); assertNull("The project should not have a working set defined", w); @@ -312,10 +346,12 @@ public class ProjectPropertiesModelTest extends TestCase { /** * A project may know if it should be rebuilt or not - * + * */ - public void testRebuild1() throws ModelException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testRebuild1() throws PropertiesException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + model.setPmdEnabled(false); model.setProjectWorkingSet(null); model.setRuleSetStoredInProject(false); @@ -325,23 +361,27 @@ public class ProjectPropertiesModelTest extends TestCase { /** * A project may know if it should be rebuilt or not - * + * */ - public void testRebuild2() throws ModelException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testRebuild2() throws PropertiesException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + model.setPmdEnabled(true); assertTrue(model.isNeedRebuild()); } /** * A project may know if it should be rebuilt or not - * + * */ - public void testRebuild3() throws ModelException { - ProjectPropertiesModel model = new ProjectPropertiesModelImpl(this.testProject); + public void testRebuild3() throws PropertiesException { + IProjectPropertiesManager mgr = PMDRuntimePlugin.getDefault().getPropertiesManager(); + IProjectProperties model = mgr.loadProjectProperties(this.testProject); + model.setPmdEnabled(true); - - RuleSet pmdRuleSet = PMDPlugin.getDefault().getRuleSet(); + + RuleSet pmdRuleSet = PMDRuntimePlugin.getDefault().getPreferencesManager().getRuleSet(); RuleSet fooRuleSet = new RuleSet(); Rule rule1 = pmdRuleSet.getRuleByName("EmptyCatchBlock"); @@ -362,9 +402,9 @@ public class ProjectPropertiesModelTest extends TestCase { this.testProject = EclipseUtils.createJavaProject("PMDTestProject"); assertTrue("A test project cannot be created; the tests cannot be performed.", (this.testProject != null) && this.testProject.exists() && this.testProject.isAccessible()); - + // 2. Keep the plugin ruleset - this.initialPluginRuleSet = PMDPlugin.getDefault().getRuleSet(); + this.initialPluginRuleSet = PMDRuntimePlugin.getDefault().getPreferencesManager().getRuleSet(); } /** @@ -378,13 +418,13 @@ public class ProjectPropertiesModelTest extends TestCase { this.testProject = null; } } - + // 2. Restore the plugin initial rule set - PMDPlugin.getDefault().setRuleSet(this.initialPluginRuleSet, null); + PMDRuntimePlugin.getDefault().getPreferencesManager().setRuleSet(this.initialPluginRuleSet); super.tearDown(); } - + private void dumpRuleSet(RuleSet ruleSet) { System.out.println("Dumping rule set:" + ruleSet.getName()); Iterator i = ruleSet.getRules().iterator(); diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/properties/UpdateProjectPropertiesCmdTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/ui/properties/UpdateProjectPropertiesCmdTest.java similarity index 74% rename from pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/properties/UpdateProjectPropertiesCmdTest.java rename to pmd-eclipse-test/test/net/sourceforge/pmd/ui/properties/UpdateProjectPropertiesCmdTest.java index 3443b9fcd8..a9da88b564 100644 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/properties/UpdateProjectPropertiesCmdTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/ui/properties/UpdateProjectPropertiesCmdTest.java @@ -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); diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/dao/PreferencesDAOTest.java b/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/dao/PreferencesDAOTest.java deleted file mode 100644 index fdaa105a9e..0000000000 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/dao/PreferencesDAOTest.java +++ /dev/null @@ -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()); - } - -} diff --git a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/model/PreferencesModelTest.java b/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/model/PreferencesModelTest.java deleted file mode 100644 index 73f04871b9..0000000000 --- a/pmd-eclipse-test/test/test/net/sourceforge/pmd/eclipse/model/PreferencesModelTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Created on 6 f�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); - } - -}