Search in sources :

Example 1 with PropertyDescriptor

use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.

the class RuleSetFactoryTest method testStringMultiPropertyDelimiter.

@Test
public void testStringMultiPropertyDelimiter() throws Exception {
    Rule r = loadFirstRule("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<ruleset name=\"test\">\n " + " <description>ruleset desc</description>\n     " + "<rule name=\"myRule\" message=\"Do not place to this package. Move to \n{0} package/s" + " instead.\" \n" + "class=\"net.sourceforge.pmd.lang.rule.XPathRule\" language=\"dummy\">\n" + "         <description>Please move your class to the right folder(rest \nfolder)</description>\n" + "         <priority>2</priority>\n         <properties>\n             <property name=\"packageRegEx\"" + " value=\"com.aptsssss,com.abc\" \ntype=\"List[String]\" delimiter=\",\" " + "description=\"valid packages\"/>\n" + "         </properties></rule>" + "</ruleset>");
    PropertyDescriptor<List<String>> prop = (PropertyDescriptor<List<String>>) r.getPropertyDescriptor("packageRegEx");
    List<String> values = r.getProperty(prop);
    assertEquals(Arrays.asList("com.aptsssss", "com.abc"), values);
}
Also used : PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) JavaUtilLoggingRule(net.sourceforge.pmd.junit.JavaUtilLoggingRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 2 with PropertyDescriptor

use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.

the class RuleSetFactoryTest method testProps.

@Test
@SuppressWarnings("unchecked")
public void testProps() throws RuleSetNotFoundException {
    Rule r = loadFirstRule(PROPERTIES);
    assertEquals("bar", r.getProperty((PropertyDescriptor<String>) r.getPropertyDescriptor("fooString")));
    assertEquals(new Integer(3), r.getProperty((PropertyDescriptor<Integer>) r.getPropertyDescriptor("fooInt")));
    assertTrue(r.getProperty((PropertyDescriptor<Boolean>) r.getPropertyDescriptor("fooBoolean")));
    assertEquals(3.0d, r.getProperty((PropertyDescriptor<Double>) r.getPropertyDescriptor("fooDouble")), 0.05);
    assertNull(r.getPropertyDescriptor("BuggleFish"));
    assertNotSame(r.getDescription().indexOf("testdesc2"), -1);
}
Also used : PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) JavaUtilLoggingRule(net.sourceforge.pmd.junit.JavaUtilLoggingRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) Test(org.junit.Test)

Example 3 with PropertyDescriptor

use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.

the class JaxenXPathRuleQuery method createXPath.

private BaseXPath createXPath(final String xpathQueryString, final Navigator navigator) throws JaxenException {
    final BaseXPath xpath = new BaseXPath(xpathQueryString, navigator);
    if (properties.size() > 1) {
        final SimpleVariableContext vc = new SimpleVariableContext();
        for (Entry<PropertyDescriptor<?>, Object> e : properties.entrySet()) {
            final String propName = e.getKey().name();
            if (!"xpath".equals(propName)) {
                final Object value = e.getValue();
                vc.setVariableValue(propName, value != null ? value.toString() : null);
            }
        }
        xpath.setVariableContext(vc);
    }
    return xpath;
}
Also used : PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) BaseXPath(org.jaxen.BaseXPath) SimpleVariableContext(org.jaxen.SimpleVariableContext)

Example 4 with PropertyDescriptor

use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.

the class RuleSetWriter method createPropertiesElement.

@SuppressWarnings("PMD.CompareObjectsWithEquals")
private Element createPropertiesElement(List<PropertyDescriptor<?>> propertyDescriptors, Map<PropertyDescriptor<?>, Object> propertiesByPropertyDescriptor) {
    Element propertiesElement = null;
    if (propertyDescriptors != null) {
        for (PropertyDescriptor<?> propertyDescriptor : propertyDescriptors) {
            if (propertyDescriptor.isDefinedExternally()) {
                // Any externally defined property needs to go out as a definition.
                if (propertiesElement == null) {
                    propertiesElement = createPropertiesElement();
                }
                Element propertyElement = createPropertyDefinitionElementBR(propertyDescriptor);
                propertiesElement.appendChild(propertyElement);
            } else {
                if (propertiesByPropertyDescriptor != null) {
                    // Otherwise, any property which has a value different than the default needs to go out as a value.
                    Object defaultValue = propertyDescriptor.defaultValue();
                    Object value = propertiesByPropertyDescriptor.get(propertyDescriptor);
                    if (value != defaultValue && (value == null || !value.equals(defaultValue))) {
                        if (propertiesElement == null) {
                            propertiesElement = createPropertiesElement();
                        }
                        Element propertyElement = createPropertyValueElement(propertyDescriptor, value);
                        propertiesElement.appendChild(propertyElement);
                    }
                }
            }
        }
    }
    if (propertiesByPropertyDescriptor != null) {
        // Then, for each PropertyDescriptor not explicitly provided
        for (Map.Entry<PropertyDescriptor<?>, Object> entry : propertiesByPropertyDescriptor.entrySet()) {
            // If not explicitly given...
            PropertyDescriptor<?> propertyDescriptor = entry.getKey();
            if (!propertyDescriptors.contains(propertyDescriptor)) {
                // Otherwise, any property which has a value different than
                // the
                // default needs to go out as a value.
                Object defaultValue = propertyDescriptor.defaultValue();
                Object value = entry.getValue();
                if (value != defaultValue && (value == null || !value.equals(defaultValue))) {
                    if (propertiesElement == null) {
                        propertiesElement = createPropertiesElement();
                    }
                    Element propertyElement = createPropertyValueElement(propertyDescriptor, value);
                    propertiesElement.appendChild(propertyElement);
                }
            }
        }
    }
    return propertiesElement;
}
Also used : PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) Element(org.w3c.dom.Element) Map(java.util.Map)

Example 5 with PropertyDescriptor

use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd-eclipse-plugin by pmd.

the class ReportManager method saveReportProperties.

/**
 * Save the properties of all renderers to the specified filename.
 *
 * @param propertyFilename
 *            String
 */
private static void saveReportProperties(String propertyFilename) {
    Properties props = new Properties();
    for (Renderer renderer : ReportManager.INSTANCE.allRenderers()) {
        Map<PropertyDescriptor<?>, Object> valuesByProp = renderer.getPropertiesByPropertyDescriptor();
        for (Map.Entry<PropertyDescriptor<?>, Object> entry : valuesByProp.entrySet()) {
            PropertyDescriptor desc = entry.getKey();
            props.put(keyOf(renderer, desc), desc.asDelimitedString(entry.getValue()));
        }
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(propertyFilename);
        props.storeToXML(fos, "asdf");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (Exception e) {
        // ignored;
        }
    }
}
Also used : PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) FileOutputStream(java.io.FileOutputStream) Renderer(net.sourceforge.pmd.renderers.Renderer) Properties(java.util.Properties) Map(java.util.Map)

Aggregations

PropertyDescriptor (net.sourceforge.pmd.properties.PropertyDescriptor)18 Test (org.junit.Test)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 JavaUtilLoggingRule (net.sourceforge.pmd.junit.JavaUtilLoggingRule)4 MockRule (net.sourceforge.pmd.lang.rule.MockRule)4 Rule (net.sourceforge.pmd.Rule)3 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)3 RuleReference (net.sourceforge.pmd.lang.rule.RuleReference)3 StringReader (java.io.StringReader)2 Properties (java.util.Properties)2 RuleContext (net.sourceforge.pmd.RuleContext)2 Language (net.sourceforge.pmd.lang.Language)2 Parser (net.sourceforge.pmd.lang.Parser)2 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)2 Node (net.sourceforge.pmd.lang.ast.Node)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 XPathRule (net.sourceforge.pmd.lang.rule.XPathRule)2 JaxenXPathRuleQuery (net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery)2