diff --git a/pmd-eclipse-pmd/.classpath b/pmd-eclipse-pmd/.classpath index 6bfce8d9a6..60adc4a667 100644 --- a/pmd-eclipse-pmd/.classpath +++ b/pmd-eclipse-pmd/.classpath @@ -6,6 +6,9 @@ + + + diff --git a/pmd-eclipse-runtime/lib/castor-0.9.6-xml.jar b/pmd-eclipse-pmd/lib/castor-0.9.6-xml.jar similarity index 100% rename from pmd-eclipse-runtime/lib/castor-0.9.6-xml.jar rename to pmd-eclipse-pmd/lib/castor-0.9.6-xml.jar diff --git a/pmd-eclipse-runtime/lib/commons-logging.jar b/pmd-eclipse-pmd/lib/commons-logging.jar similarity index 100% rename from pmd-eclipse-runtime/lib/commons-logging.jar rename to pmd-eclipse-pmd/lib/commons-logging.jar diff --git a/pmd-eclipse-runtime/lib/log4j-1.2.8.jar b/pmd-eclipse-pmd/lib/log4j-1.2.8.jar similarity index 100% rename from pmd-eclipse-runtime/lib/log4j-1.2.8.jar rename to pmd-eclipse-pmd/lib/log4j-1.2.8.jar diff --git a/pmd-eclipse-pmd/plugin.xml b/pmd-eclipse-pmd/plugin.xml index 73488c2f03..a190e28870 100644 --- a/pmd-eclipse-pmd/plugin.xml +++ b/pmd-eclipse-pmd/plugin.xml @@ -26,6 +26,15 @@ + + + + + + + + + diff --git a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/PMDCoreException.java b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/PMDCoreException.java new file mode 100644 index 0000000000..cf66a2ec25 --- /dev/null +++ b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/PMDCoreException.java @@ -0,0 +1,86 @@ +/* + * Created on 2 sept. 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.core; + +/** + * Root exception of the CORE plug-in + * + * @author Herlin + * @version $Revision$ + * + * $Log$ + * Revision 1.1 2006/10/06 16:42:47 phherlin + * Continue refactoring of rullesets management + * + * + */ + +public class PMDCoreException extends Exception { + + /** + * Default constructor. + */ + public PMDCoreException() { + super(); + } + + /** + * Constructor with a message and a root cause. + * @param arg0 exception message. + * @param arg1 root cause exception. + */ + public PMDCoreException(String arg0, Throwable arg1) { + super(arg0, arg1); + } + + /** + * Constructor with only a message. + * @param arg0 exception message. + */ + public PMDCoreException(String arg0) { + super(arg0); + } + + /** + * Constructor with a root cause exception only + * @param arg0 root cause exception + */ + public PMDCoreException(Throwable arg0) { + super(arg0); + } + +} diff --git a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/IRuleSetsManager.java b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/IRuleSetsManager.java index 59588f30ff..a668005493 100644 --- a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/IRuleSetsManager.java +++ b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/IRuleSetsManager.java @@ -36,11 +36,10 @@ package net.sourceforge.pmd.core.rulesets; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import net.sourceforge.pmd.RuleSetNotFoundException; +import net.sourceforge.pmd.core.PMDCoreException; import net.sourceforge.pmd.core.rulesets.vo.RuleSet; import net.sourceforge.pmd.core.rulesets.vo.RuleSets; @@ -54,6 +53,9 @@ import net.sourceforge.pmd.core.rulesets.vo.RuleSets; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:47 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:41 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. @@ -71,9 +73,9 @@ public interface IRuleSetsManager { * @param ruleSetUrls an array of standard PMD rule sets. * @return a plug-in specific rulesets structure. * - * @throws RuleSetNotFoundException if one of the rule set url is incorrect. + * @throws PMDCoreException if an error occurred. Check the root cause for details. */ - RuleSet valueOf(String[] ruleSetUrls) throws RuleSetNotFoundException; + RuleSet valueOf(String[] ruleSetUrls) throws PMDCoreException; /** * Serialize a rule sets structure to an output stream. @@ -81,9 +83,9 @@ public interface IRuleSetsManager { * @param ruleSets a rule sets structure. * @param output an open output stream. * - * @throws IOException if an error occurs while writing the stream. + * @throws PMDCoreException if an error occurred. Check the root cause for details. */ - void writeToXml(RuleSets ruleSets, OutputStream output) throws IOException; + void writeToXml(RuleSets ruleSets, OutputStream output) throws PMDCoreException; /** * Load a rule sets structure from an input stream than contains an XML @@ -92,8 +94,8 @@ public interface IRuleSetsManager { * @param input a valid XML input stream. * @return a rulesets structure ; this is never null. * - * @throws IOException if an error occurs while reading from the stream. + * @throws PMDCoreException if an error occurred. Check the root cause for details. */ - RuleSets readFromXml(InputStream input) throws IOException; + RuleSets readFromXml(InputStream input) throws PMDCoreException; } diff --git a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImpl.java b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImpl.java index a2a10694a7..772601347f 100644 --- a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImpl.java +++ b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImpl.java @@ -39,16 +39,27 @@ package net.sourceforge.pmd.core.rulesets.impl; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.StringWriter; +import java.net.URL; import java.util.Collection; import java.util.Iterator; import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetNotFoundException; +import net.sourceforge.pmd.core.PMDCoreException; import net.sourceforge.pmd.core.rulesets.IRuleSetsManager; import net.sourceforge.pmd.core.rulesets.vo.Rule; import net.sourceforge.pmd.core.rulesets.vo.RuleSet; import net.sourceforge.pmd.core.rulesets.vo.RuleSets; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.exolab.castor.mapping.Mapping; +import org.exolab.castor.mapping.MappingException; +import org.exolab.castor.xml.MarshalException; +import org.exolab.castor.xml.Marshaller; +import org.exolab.castor.xml.ValidationException; + /** * Implementation of an IRuleSetsManager. * The serialization is based on the usage of Castor. @@ -57,6 +68,9 @@ import net.sourceforge.pmd.core.rulesets.vo.RuleSets; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:46 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:41 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. @@ -65,11 +79,13 @@ import net.sourceforge.pmd.core.rulesets.vo.RuleSets; */ public class RuleSetsManagerImpl implements IRuleSetsManager { + private static final Log LOG = LogFactory.getLog(RuleSetsManagerImpl.class); + private static final String RULESETS_MAPPING = "/net/sourceforge/pmd/core/rulesets/impl/mapping.xml"; /* (non-Javadoc) * @see net.sourceforge.pmd.core.rulesets.IRuleSetsManager#readFromXml(java.io.InputStream) */ - public RuleSets readFromXml(InputStream input) throws IOException { + public RuleSets readFromXml(InputStream input) throws PMDCoreException { // TODO Auto-generated method stub return null; } @@ -78,7 +94,8 @@ public class RuleSetsManagerImpl implements IRuleSetsManager { * @throws RuleSetNotFoundException * @see net.sourceforge.pmd.core.rulesets.IRuleSetsManager#valueOf(java.lang.String[]) */ - public RuleSet valueOf(String[] ruleSetUrls) throws RuleSetNotFoundException { + public RuleSet valueOf(String[] ruleSetUrls) throws PMDCoreException { + LOG.debug("Compting value of a collection of rule set urls"); if (ruleSetUrls == null) { throw new IllegalArgumentException("ruleSetUrls cannot be null"); } @@ -86,28 +103,61 @@ public class RuleSetsManagerImpl implements IRuleSetsManager { throw new IllegalArgumentException("ruleSetsUrls cannot be empty"); } - final RuleSet ruleSet = new RuleSet(); - - for (int i = 0; i < ruleSetUrls.length; i++) { - final RuleSetFactory factory = new RuleSetFactory(); // NOPMD by Herlin on 21/06/06 23:25 - final Collection rules = factory.createSingleRuleSet(ruleSetUrls[i]).getRules(); - for (final Iterator j = rules.iterator(); j.hasNext();) { - final net.sourceforge.pmd.Rule pmdRule = (net.sourceforge.pmd.Rule) j.next(); - final Rule rule = new Rule(); // NOPMD by Herlin on 21/06/06 23:29 - rule.setRef(ruleSetUrls[i] + '/' + pmdRule.getName()); - rule.setPmdRule(pmdRule); - ruleSet.addRule(rule); + try { + final RuleSet ruleSet = new RuleSet(); + + for (int i = 0; i < ruleSetUrls.length; i++) { + final RuleSetFactory factory = new RuleSetFactory(); // NOPMD by Herlin on 21/06/06 23:25 + final Collection rules = factory.createSingleRuleSet(ruleSetUrls[i]).getRules(); + for (final Iterator j = rules.iterator(); j.hasNext();) { + final net.sourceforge.pmd.Rule pmdRule = (net.sourceforge.pmd.Rule) j.next(); + final Rule rule = new Rule(); // NOPMD by Herlin on 21/06/06 23:29 + rule.setRef(ruleSetUrls[i] + '/' + pmdRule.getName()); + rule.setPmdRule(pmdRule); + ruleSet.addRule(rule); + } } + + return ruleSet; + + } catch (RuleSetNotFoundException e) { + LOG.error("A RuleSetsNotFound Exception was thrown."); + throw new PMDCoreException("A RuleSetsNotFound Exception was thrown.", e); } - - return ruleSet; } - /* (non-Javadoc) + /** * @see net.sourceforge.pmd.core.rulesets.IRuleSetsManager#writeToXml(net.sourceforge.pmd.core.rulesets.vo.RuleSets, java.io.OutputStream) */ - public void writeToXml(RuleSets ruleSets, OutputStream output) throws IOException { - // TODO Auto-generated method stub - + public void writeToXml(RuleSets ruleSets, OutputStream output) throws PMDCoreException { + LOG.debug("Storing plug-in rulesets"); + try { + final Mapping mapping = new Mapping(this.getClass().getClassLoader()); + final URL mappingSpecUrl = this.getClass().getResource(RULESETS_MAPPING); + mapping.loadMapping(mappingSpecUrl); + + final StringWriter writer = new StringWriter(); + final Marshaller marshaller = new Marshaller(writer); + marshaller.setMapping(mapping); + marshaller.marshal(ruleSets); + writer.flush(); + writer.close(); + + output.write(writer.getBuffer().toString().getBytes()); + output.flush(); + + } catch (MarshalException e) { + LOG.error("A Marshal Exception was thrown."); + throw new PMDCoreException("A Marshal Exception was thrown.", e); + } catch (ValidationException e) { + LOG.error("A Validation Exception was thrown."); + throw new PMDCoreException("A Validation Exception was thrown.", e); + } catch (MappingException e) { + LOG.error("A Mapping Exception was thrown."); + throw new PMDCoreException("A Mapping Exception was thrown.", e); + } catch (IOException e) { + LOG.error("A IO Exception was thrown."); + throw new PMDCoreException("A IO Exception was thrown.", e); + } } } diff --git a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/impl/mapping.xml b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/impl/mapping.xml new file mode 100644 index 0000000000..e6d55db0aa --- /dev/null +++ b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/impl/mapping.xml @@ -0,0 +1,94 @@ + + + PMD For Eclipse RuleSets mapping file + + + Default mapping for class net.sourceforge.pmd.core.rulesets.vo.RuleSets + + + + + + + + + + + + + Default mapping for class net.sourceforge.pmd.core.rulesets.vo.RuleSet + + + + + + + + + + + + + + + + + + + + + Default mapping for class net.sourceforge.pmd.core.rulesets.vo.Rule + + + + + + + + + + + + + + + + + + + + + Default mapping for class net.sourceforge.pmd.core.rulesets.vo.Properties + + + + + + + + + Default mapping for class net.sourceforge.pmd.core.rulesets.vo.Property + + + + + + + + + + + + + Default mapping for class net.sourceforge.pmd.core.rulesets.vo.Priority + + + + + + + + \ No newline at end of file diff --git a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/Priority.java b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/Priority.java index 6b2cc53388..eee0df9ba7 100644 --- a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/Priority.java +++ b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/Priority.java @@ -47,6 +47,9 @@ package net.sourceforge.pmd.core.rulesets.vo; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:46 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:41 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. @@ -109,7 +112,7 @@ public class Priority { * * @param priorityValue The priorityValue to set. */ - public void setPriority(int priority) { + public void setPriorityValue(int priority) { if ((priority < LEVEL1_LITTERAL) || (priority > LEVEL5_LITTERAL)) { throw new IllegalArgumentException("priorityValue value invalid ; was " + priority + " and should be between " + LEVEL1_LITTERAL + " and " + LEVEL5_LITTERAL); diff --git a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSet.java b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSet.java index 57b5cd8be8..ef6045e00b 100644 --- a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSet.java +++ b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSet.java @@ -41,7 +41,7 @@ import java.util.Collection; import java.util.Iterator; /** - * This class is a value objet that composes the structure of a rulesets object. + * This class is a value objet which composes the structure of a rulesets object. * It holds the definition of a rule set which is actually a named collection of * rules. * @@ -49,6 +49,9 @@ import java.util.Iterator; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:46 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:41 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. diff --git a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSets.java b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSets.java index 429c891b7c..623b684e13 100644 --- a/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSets.java +++ b/pmd-eclipse-pmd/src/net/sourceforge/pmd/core/rulesets/vo/RuleSets.java @@ -49,6 +49,9 @@ import java.util.List; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:46 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:41 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. @@ -116,6 +119,33 @@ public class RuleSets { } this.ruleSetsList = ruleSets; } + + /** + * Return the name of the default ruleset + * @return the name of the default ruleset + */ + public String getDefaultRuleSetName() { + return this.defaultRuleSet.getName(); + } + + /** + * Sets the default ruleset by its name. If the ruleset does not exist, + * the default ruleset is not set. + * @param ruleSetName a name of an already defined ruleset. + */ + public void setDefaultRuleSetName(String ruleSetName) { + if (ruleSetName == null) { + throw new IllegalArgumentException("The default ruleset name must not ne null"); + } + + for (final Iterator i = this.ruleSetsList.iterator(); i.hasNext();) { + final RuleSet ruleSet = (RuleSet) i.next(); + if (ruleSet.getName().equals(ruleSetName)) { + setDefaultRuleSet(ruleSet); + break; + } + } + } /** * @see java.lang.Object#toString() diff --git a/pmd-eclipse-runtime/.classpath b/pmd-eclipse-runtime/.classpath index 115ffb5cd6..751c8f2e50 100644 --- a/pmd-eclipse-runtime/.classpath +++ b/pmd-eclipse-runtime/.classpath @@ -1,9 +1,6 @@ - - - diff --git a/pmd-eclipse-runtime/plugin.xml b/pmd-eclipse-runtime/plugin.xml index f8da53d5b1..0b613f7e50 100644 --- a/pmd-eclipse-runtime/plugin.xml +++ b/pmd-eclipse-runtime/plugin.xml @@ -11,15 +11,6 @@ - - - - - - - - - diff --git a/pmd-eclipse-runtime/src/net/sourceforge/pmd/runtime/properties/impl/ProjectPropertiesManagerImpl.java b/pmd-eclipse-runtime/src/net/sourceforge/pmd/runtime/properties/impl/ProjectPropertiesManagerImpl.java index 75f8bbd983..62cfe05ac6 100644 --- a/pmd-eclipse-runtime/src/net/sourceforge/pmd/runtime/properties/impl/ProjectPropertiesManagerImpl.java +++ b/pmd-eclipse-runtime/src/net/sourceforge/pmd/runtime/properties/impl/ProjectPropertiesManagerImpl.java @@ -77,6 +77,9 @@ import org.exolab.castor.xml.ValidationException; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:41:57 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/05/22 21:37:35 phherlin * Refactor the plug-in architecture to better support future evolutions * Revision 1.1 2005/06/07 18:38:14 phherlin Move classes to limit packages cycle @@ -91,12 +94,12 @@ import org.exolab.castor.xml.ValidationException; public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager { private static final Logger log = Logger.getLogger(ProjectPropertiesManagerImpl.class); - static final String PROJECT_RULESET_FILE = ".ruleset"; + static final String PROJECT_RULESET_FILE = ".ruleset"; // NOPMD by Herlin on 08/07/06 15:18 private static final String PROPERTIES_FILE = ".pmd"; private static final String PROPERTIES_MAPPING = "/net/sourceforge/pmd/runtime/properties/impl/mapping.xml"; - private Map projectsProperties = new HashMap(); + private final Map projectsProperties = new HashMap(); /** * Load a project properties @@ -109,7 +112,7 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager { IProjectProperties projectProperties = (IProjectProperties) this.projectsProperties.get(project); if (projectProperties == null) { projectProperties = new PropertiesFactoryImpl().newProjectProperties(project, this); - ProjectPropertiesTO to = readProjectProperties(project); + final ProjectPropertiesTO to = readProjectProperties(project); fillProjectProperties(projectProperties, to); this.projectsProperties.put(project, projectProperties); } @@ -121,7 +124,7 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager { // else resynchronize the ruleset else { - boolean needRebuild = synchronizeRuleSet(projectProperties); + final boolean needRebuild = synchronizeRuleSet(projectProperties); projectProperties.setNeedRebuild(projectProperties.isNeedRebuild() || needRebuild); } @@ -182,7 +185,7 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager { ProjectPropertiesTO projectProperties = null; try { - final Mapping mapping = new Mapping(); + final Mapping mapping = new Mapping(this.getClass().getClassLoader()); final URL mappingSpecUrl = this.getClass().getResource(PROPERTIES_MAPPING); mapping.loadMapping(mappingSpecUrl); @@ -242,7 +245,7 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager { * @param rules array of selected rules */ private void setRuleSetFromProperties(IProjectProperties projectProperties, RuleSpecTO[] rules) throws PropertiesException { - RuleSet ruleSet = new RuleSet(); + final RuleSet ruleSet = new RuleSet(); final RuleSet pluginRuleSet = PMDRuntimePlugin.getDefault().getPreferencesManager().getRuleSet(); for (int i = 0; i < rules.length; i++) { try { @@ -267,7 +270,7 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager { private void writeProjectProperties(final IProject project, final ProjectPropertiesTO projectProperties) throws PropertiesException { try { - final Mapping mapping = new Mapping(); + final Mapping mapping = new Mapping(this.getClass().getClassLoader()); final URL mappingSpecUrl = this.getClass().getResource(PROPERTIES_MAPPING); mapping.loadMapping(mappingSpecUrl); @@ -331,8 +334,8 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager { private boolean synchronizeRuleSet(IProjectProperties projectProperties) throws PropertiesException { log.debug("Synchronizing the project ruleset with the plugin ruleset"); final RuleSet pluginRuleSet = PMDRuntimePlugin.getDefault().getPreferencesManager().getRuleSet(); + final RuleSet projectRuleSet = projectProperties.getProjectRuleSet(); boolean flChanged = false; - RuleSet projectRuleSet = projectProperties.getProjectRuleSet(); if (!projectRuleSet.getRules().equals(pluginRuleSet.getRules())) { log.debug("The project ruleset is different from the plugin ruleset ; synchronizing."); diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImplTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImplTest.java index a1deda2b1a..e5f226b3ad 100644 --- a/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImplTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/impl/RuleSetsManagerImplTest.java @@ -36,15 +36,24 @@ package net.sourceforge.pmd.core.rulesets.impl; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.List; +import junit.framework.TestCase; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetNotFoundException; +import net.sourceforge.pmd.core.PMDCoreException; import net.sourceforge.pmd.core.rulesets.IRuleSetsManager; import net.sourceforge.pmd.core.rulesets.vo.RuleSet; -import junit.framework.TestCase; +import net.sourceforge.pmd.core.rulesets.vo.RuleSets; /** * RuleSetsManager unit tests @@ -53,6 +62,9 @@ import junit.framework.TestCase; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:04 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:54 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. @@ -67,7 +79,7 @@ public class RuleSetsManagerImplTest extends TestCase { * * @throws RuleSetNotFoundException */ - public void testValueOf1() throws RuleSetNotFoundException { + public void testValueOf1() throws PMDCoreException, RuleSetNotFoundException { IRuleSetsManager rsm = new RuleSetsManagerImpl(); RuleSet ruleSet = rsm.valueOf(new String[] { "rulesets/basic.xml" }); @@ -87,7 +99,7 @@ public class RuleSetsManagerImplTest extends TestCase { * @throws RuleSetNotFoundException * */ - public void testValueOf2() throws RuleSetNotFoundException { + public void testValueOf2() throws PMDCoreException { try { IRuleSetsManager rsm = new RuleSetsManagerImpl(); RuleSet ruleSet = rsm.valueOf(null); @@ -103,7 +115,7 @@ public class RuleSetsManagerImplTest extends TestCase { * @throws RuleSetNotFoundException * */ - public void testValueOf3() throws RuleSetNotFoundException { + public void testValueOf3() throws PMDCoreException { try { IRuleSetsManager rsm = new RuleSetsManagerImpl(); RuleSet ruleSet = rsm.valueOf(new String[] {}); @@ -112,6 +124,55 @@ public class RuleSetsManagerImplTest extends TestCase { // Sucess } } + + /** + * Basically test the writeToXml operation. + * + */ + public void testWriteToXml() throws PMDCoreException, UnsupportedEncodingException, IOException { + ByteArrayOutputStream out = null; + InputStream in = new FileInputStream("./test/testRuleSetsManager.rulesets"); + if (in == null) { + throw new IllegalStateException("The test file testRuleSetsManager.rulesets cannot be found. The test cannot be performed."); + } + + byte[] bytes = new byte[in.available()]; + in.read(bytes); + + String reference = new String(bytes, "UTF-8"); + in.close(); + + System.out.println("--reference"); + System.out.println(reference); + + try { + IRuleSetsManager rsm = new RuleSetsManagerImpl(); + RuleSet ruleSet = rsm.valueOf(new String[] { "rulesets/basic.xml" }); + ruleSet.setName("basic"); + ruleSet.setLanguage(RuleSet.LANGUAGE_JAVA); + + List ruleSetsList = new ArrayList(); + ruleSetsList.add(ruleSet); + + RuleSets ruleSets = new RuleSets(); + ruleSets.setRuleSets(ruleSetsList); + ruleSets.setDefaultRuleSet(ruleSet); + + out = new ByteArrayOutputStream(); + rsm.writeToXml(ruleSets, out); + + String result = new String(out.toByteArray(), "UTF-8"); + + System.out.println("--result"); + System.out.println(result); + + assertEquals("The outpout rulesets is not the expected one", reference, result); + } finally { + if (out != null) { + out.close(); + } + } + } /** * Dump a collection of rules diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/PriorityTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/PriorityTest.java index f187c689c5..e810ac1c43 100644 --- a/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/PriorityTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/PriorityTest.java @@ -45,6 +45,9 @@ import junit.framework.TestCase; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:03 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:52 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. @@ -72,19 +75,19 @@ public class PriorityTest extends TestCase { */ public void testSetPriority() { Priority p = new Priority(); - p.setPriority(Priority.LEVEL1_LITTERAL); + p.setPriorityValue(Priority.LEVEL1_LITTERAL); assertEquals("Constructing a priority level 1 has failed!", Priority.LEVEL1, p); - p.setPriority(Priority.LEVEL2_LITTERAL); + p.setPriorityValue(Priority.LEVEL2_LITTERAL); assertEquals("Constructing a priority level 2 has failed!", Priority.LEVEL2, p); - p.setPriority(Priority.LEVEL3_LITTERAL); + p.setPriorityValue(Priority.LEVEL3_LITTERAL); assertEquals("Constructing a priority level 3 has failed!", Priority.LEVEL3, p); - p.setPriority(Priority.LEVEL4_LITTERAL); + p.setPriorityValue(Priority.LEVEL4_LITTERAL); assertEquals("Constructing a priority level 4 has failed!", Priority.LEVEL4, p); - p.setPriority(Priority.LEVEL5_LITTERAL); + p.setPriorityValue(Priority.LEVEL5_LITTERAL); assertEquals("Constructing a priority level 5 has failed!", Priority.LEVEL5, p); } @@ -96,7 +99,7 @@ public class PriorityTest extends TestCase { public void testSetPriorityIllegal1() { try { Priority p = new Priority(); - p.setPriority(0); + p.setPriorityValue(0); fail("Setting a priority level to 0 should raise an IllegalArgumentException"); } catch (IllegalArgumentException e) { // this is correct @@ -110,7 +113,7 @@ public class PriorityTest extends TestCase { public void testSetPriorityIllegal2() { try { Priority p = new Priority(); - p.setPriority(-15); + p.setPriorityValue(-15); fail("Setting a priority level to a negative number should raise an IllegalArgumentException"); } catch (IllegalArgumentException e) { // this is correct @@ -124,7 +127,7 @@ public class PriorityTest extends TestCase { public void testSetPriorityIllegal3() { try { Priority p = new Priority(); - p.setPriority(6); + p.setPriorityValue(6); fail("Setting a priority level to a high value should raise an IllegalArgumentException"); } catch (IllegalArgumentException e) { // this is correct @@ -137,7 +140,7 @@ public class PriorityTest extends TestCase { */ public void testEquals1() { Priority p = new Priority(); - p.setPriority(1); + p.setPriorityValue(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)); diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/RuleSetsTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/RuleSetsTest.java index f297bbbb0e..dd22d3cb20 100644 --- a/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/RuleSetsTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/core/rulesets/vo/RuleSetsTest.java @@ -50,6 +50,9 @@ import junit.framework.TestCase; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:03 phherlin + * Continue refactoring of rullesets management + * * Revision 1.1 2006/06/21 23:06:52 phherlin * Move the new rule sets management to the core plugin instead of the runtime. * Continue the development. @@ -123,7 +126,7 @@ public class RuleSetsTest extends TestCase { try { RuleSets rs = new RuleSets(); rs.setDefaultRuleSet(null); - fail("setting a defaulr rule set to null should be illegal"); + fail("setting a default rule set to null should be illegal"); } catch (IllegalArgumentException e) { // success } @@ -266,4 +269,48 @@ public class RuleSetsTest extends TestCase { assertFalse("Different rule sets should have different hash code", rs1.hashCode() == rs2.hashCode()); } + + /** + * Test the basic usage of the default ruleset setter + * + */ + public void testSetDefaultRuleSetName() throws RuleSetNotFoundException { + Rule r1 = new Rule(); + r1.setRef("ref to a rule"); + r1.setPmdRule(TestManager.getRule(0)); + + Rule r2 = new Rule(); + r2.setRef("ref to another rule"); + r2.setPmdRule(TestManager.getRule(1)); + + RuleSet rs1 = new RuleSet(); + rs1.setName("default"); + rs1.setLanguage(RuleSet.LANGUAGE_JSP); + rs1.addRule(r1); + rs1.addRule(r2); + + List ruleSetsList = new ArrayList(); + ruleSetsList.add(rs1); + + RuleSets ruleSets = new RuleSets(); + ruleSets.setRuleSets(ruleSetsList); + + ruleSets.setDefaultRuleSetName("default"); + + assertSame("The default ruleset has not been set correctly", rs1, ruleSets.getDefaultRuleSet()); + } + + /** + * Test setting a default ruleset name to null + * + */ + public void testSetDefaultRuleSetNameNull() { + try { + RuleSets ruleSets = new RuleSets(); + ruleSets.setDefaultRuleSetName(null); + fail("Setting a default ruleset name to null is illegal"); + } catch (IllegalArgumentException e) { + // success + } + } } diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/properties/ProjectPropertiesModelTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/properties/ProjectPropertiesModelTest.java index 4f8e8e8c66..bfd580baeb 100644 --- a/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/properties/ProjectPropertiesModelTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/runtime/properties/ProjectPropertiesModelTest.java @@ -35,7 +35,10 @@ */ package net.sourceforge.pmd.runtime.properties; +import java.util.HashSet; import java.util.Iterator; +import java.util.List; +import java.util.Set; import junit.framework.TestCase; import net.sourceforge.pmd.AbstractRule; @@ -43,6 +46,7 @@ import net.sourceforge.pmd.Rule; 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.eclipse.EclipseUtils; import net.sourceforge.pmd.runtime.PMDRuntimePlugin; import net.sourceforge.pmd.runtime.builder.PMDNature; @@ -65,6 +69,9 @@ import org.eclipse.ui.IWorkingSet; * @version $Revision$ * * $Log$ + * Revision 1.2 2006/10/06 16:42:03 phherlin + * Continue refactoring of rullesets management + * * 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 @@ -169,9 +176,10 @@ public class ProjectPropertiesModelTest extends TestCase { /** * When rules are removed from the plugin preferences, these rules should * also be removed from the project + * euh... ben en fait non. annulé. */ 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(); @@ -190,7 +198,10 @@ public class ProjectPropertiesModelTest extends TestCase { projectRuleSet = model.getProjectRuleSet(); + dumpRuleSet(basicRuleSet); + dumpRuleSet(projectRuleSet); assertEquals("The project ruleset is not equal to the plugin ruleset", basicRuleSet.getRules(), projectRuleSet.getRules()); +*/ } /** @@ -198,8 +209,6 @@ public class ProjectPropertiesModelTest extends TestCase { * be added to the project */ public void testProjectRuleSet3() throws PropertiesException, RuleSetNotFoundException, CoreException { - RuleSetFactory factory = new RuleSetFactory(); - RuleSet basicRuleSet = factory.createSingleRuleSet("rulesets/basic.xml"); // First ensure that the plugin initial ruleset is equal to the project // ruleset @@ -207,7 +216,7 @@ public class ProjectPropertiesModelTest extends TestCase { IProjectProperties model = mgr.loadProjectProperties(this.testProject); RuleSet projectRuleSet = model.getProjectRuleSet(); - assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet, projectRuleSet); + assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet.getRules(), projectRuleSet.getRules()); // 2. add a rule to the plugin rule set Rule myRule = new AbstractRule() { @@ -224,7 +233,7 @@ public class ProjectPropertiesModelTest extends TestCase { // Test that the project rule set should still be the same as the plugin // rule set - model = new ProjectPropertiesImpl(this.testProject, mgr); + model = mgr.loadProjectProperties(this.testProject); projectRuleSet = model.getProjectRuleSet(); assertEquals("The project ruleset is not equal to the plugin ruleset", PMDRuntimePlugin.getDefault() .getPreferencesManager().getRuleSet().getRules(), projectRuleSet.getRules()); @@ -242,7 +251,7 @@ public class ProjectPropertiesModelTest extends TestCase { IProjectProperties model = mgr.loadProjectProperties(this.testProject); RuleSet projectRuleSet = model.getProjectRuleSet(); - assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet, projectRuleSet); + assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet.getRules(), projectRuleSet.getRules()); // 2. remove the first rule (keep its name for assertion) RuleSet newRuleSet = new RuleSet(); @@ -405,6 +414,11 @@ public class ProjectPropertiesModelTest extends TestCase { // 2. Keep the plugin ruleset this.initialPluginRuleSet = PMDRuntimePlugin.getDefault().getPreferencesManager().getRuleSet(); + this.initialPluginRuleSet.getRules().clear(); + Set defaultRuleSets = PMDCorePlugin.getDefault().getRuleSetManager().getDefaultRuleSets(); + for (Iterator i = defaultRuleSets.iterator(); i.hasNext();) { + this.initialPluginRuleSet.addRuleSet((RuleSet) i.next()); + } } /** @@ -434,5 +448,5 @@ public class ProjectPropertiesModelTest extends TestCase { } System.out.println(); } - + } \ No newline at end of file diff --git a/pmd-eclipse-test/test/net/sourceforge/pmd/ui/properties/UpdateProjectPropertiesCmdTest.java b/pmd-eclipse-test/test/net/sourceforge/pmd/ui/properties/UpdateProjectPropertiesCmdTest.java index a9da88b564..ca48fb34c4 100644 --- a/pmd-eclipse-test/test/net/sourceforge/pmd/ui/properties/UpdateProjectPropertiesCmdTest.java +++ b/pmd-eclipse-test/test/net/sourceforge/pmd/ui/properties/UpdateProjectPropertiesCmdTest.java @@ -59,7 +59,7 @@ public class UpdateProjectPropertiesCmdTest extends TestCase { RuleSet projectRuleSet = model.getProjectRuleSet(); assertEquals("The project ruleset is not equal to the plugin ruleset", PMDRuntimePlugin.getDefault() - .getPreferencesManager().getRuleSet(), projectRuleSet); + .getPreferencesManager().getRuleSet().getRules(), projectRuleSet.getRules()); // 2. remove the first rule (keep its name for assertion) RuleSet newRuleSet = new RuleSet(); diff --git a/pmd-eclipse-test/test/testRuleSetsManager.rulesets b/pmd-eclipse-test/test/testRuleSetsManager.rulesets new file mode 100644 index 0000000000..57f9e82bd8 --- /dev/null +++ b/pmd-eclipse-test/test/testRuleSetsManager.rulesets @@ -0,0 +1,2 @@ + + \ No newline at end of file