Search in sources :

Example 11 with SonarException

use of org.sonar.api.utils.SonarException in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method shouldNotCreateFileIfFailToDownload.

@Test
public void shouldNotCreateFileIfFailToDownload() throws Exception {
    File toDir = temporaryFolder.newFolder();
    File toFile = new File(toDir, "downloadToFile.txt");
    try {
        int port = new InetSocketAddress("localhost", 0).getPort();
        new DefaultHttpDownloader(new MapSettings()).download(new URI("http://localhost:" + port), toFile);
    } catch (SonarException e) {
        assertThat(toFile).doesNotExist();
    }
}
Also used : MapSettings(org.sonar.api.config.MapSettings) InetSocketAddress(java.net.InetSocketAddress) SonarException(org.sonar.api.utils.SonarException) File(java.io.File) URI(java.net.URI) Test(org.junit.Test)

Example 12 with SonarException

use of org.sonar.api.utils.SonarException in project sonarqube by SonarSource.

the class XMLRuleParser method processRule.

private static void processRule(Rule rule, SMInputCursor ruleC) throws XMLStreamException {
    /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
    String keyAttribute = ruleC.getAttrValue("key");
    if (StringUtils.isNotBlank(keyAttribute)) {
        rule.setKey(StringUtils.trim(keyAttribute));
    }
    /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
    String priorityAttribute = ruleC.getAttrValue("priority");
    if (StringUtils.isNotBlank(priorityAttribute)) {
        rule.setSeverity(RulePriority.valueOf(StringUtils.trim(priorityAttribute)));
    }
    List<String> tags = Lists.newArrayList();
    SMInputCursor cursor = ruleC.childElementCursor();
    while (cursor.getNext() != null) {
        String nodeName = cursor.getLocalName();
        if (StringUtils.equalsIgnoreCase("name", nodeName)) {
            rule.setName(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("description", nodeName)) {
            rule.setDescription(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("key", nodeName)) {
            rule.setKey(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("configKey", nodeName)) {
            rule.setConfigKey(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("priority", nodeName)) {
            rule.setSeverity(RulePriority.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
        } else if (StringUtils.equalsIgnoreCase("cardinality", nodeName)) {
            rule.setCardinality(Cardinality.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
        } else if (StringUtils.equalsIgnoreCase("status", nodeName)) {
            rule.setStatus(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("param", nodeName)) {
            processParameter(rule, cursor);
        } else if (StringUtils.equalsIgnoreCase("tag", nodeName)) {
            tags.add(StringUtils.trim(cursor.collectDescendantText(false)));
        }
    }
    if (Strings.isNullOrEmpty(rule.getKey())) {
        throw new SonarException("Node <key> is missing in <rule>");
    }
    rule.setTags(tags.toArray(new String[tags.size()]));
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SonarException(org.sonar.api.utils.SonarException)

Example 13 with SonarException

use of org.sonar.api.utils.SonarException in project sonarqube by SonarSource.

the class XMLRuleParser method processParameter.

private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException {
    RuleParam param = rule.createParameter();
    String keyAttribute = ruleC.getAttrValue("key");
    if (StringUtils.isNotBlank(keyAttribute)) {
        /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
        param.setKey(StringUtils.trim(keyAttribute));
    }
    String typeAttribute = ruleC.getAttrValue("type");
    if (StringUtils.isNotBlank(typeAttribute)) {
        /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
        param.setType(type(StringUtils.trim(typeAttribute)));
    }
    SMInputCursor paramC = ruleC.childElementCursor();
    while (paramC.getNext() != null) {
        String propNodeName = paramC.getLocalName();
        String propText = StringUtils.trim(paramC.collectDescendantText(false));
        if (StringUtils.equalsIgnoreCase("key", propNodeName)) {
            param.setKey(propText);
        } else if (StringUtils.equalsIgnoreCase("description", propNodeName)) {
            param.setDescription(propText);
        } else if (StringUtils.equalsIgnoreCase("type", propNodeName)) {
            param.setType(type(propText));
        } else if (StringUtils.equalsIgnoreCase("defaultValue", propNodeName)) {
            param.setDefaultValue(propText);
        }
    }
    if (Strings.isNullOrEmpty(param.getKey())) {
        throw new SonarException("Node <key> is missing in <param>");
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SonarException(org.sonar.api.utils.SonarException)

Example 14 with SonarException

use of org.sonar.api.utils.SonarException in project sonarqube by SonarSource.

the class XMLRuleParser method parse.

public List<Rule> parse(Reader reader) {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    try {
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        // <rules>
        rootC.advance();
        List<Rule> rules = new ArrayList<>();
        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            Rule rule = Rule.create();
            rules.add(rule);
            processRule(rule, rulesC);
        }
        return rules;
    } catch (XMLStreamException e) {
        throw new SonarException("XML is not valid", e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) SonarException(org.sonar.api.utils.SonarException) SMInputFactory(org.codehaus.staxmate.SMInputFactory) ArrayList(java.util.ArrayList) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 15 with SonarException

use of org.sonar.api.utils.SonarException in project sonarqube by SonarSource.

the class AnnotationRuleParser method addRuleProperty.

private static void addRuleProperty(Rule rule, Field field) {
    org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
    if (propertyAnnotation != null) {
        String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
        RuleParam param = rule.createParameter(fieldKey);
        param.setDescription(propertyAnnotation.description());
        param.setDefaultValue(propertyAnnotation.defaultValue());
        if (!StringUtils.isBlank(propertyAnnotation.type())) {
            try {
                param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
            } catch (IllegalArgumentException e) {
                throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
            }
        } else {
            param.setType(guessType(field.getType()).name());
        }
    }
}
Also used : SonarException(org.sonar.api.utils.SonarException)

Aggregations

SonarException (org.sonar.api.utils.SonarException)16 Test (org.junit.Test)6 IOException (java.io.IOException)4 URI (java.net.URI)4 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)3 Release (org.sonar.updatecenter.common.Release)3 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 MapSettings (org.sonar.api.config.MapSettings)2 Plugin (org.sonar.updatecenter.common.Plugin)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1