From: Julien Lancelot Date: Fri, 14 Mar 2014 13:02:28 +0000 (+0100) Subject: Remove useless old debt model synchronization and renamed some debt classes X-Git-Tag: 4.3~433 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f49602ab2261ba05e9395557958eb23ce563978f;p=sonarqube.git Remove useless old debt model synchronization and renamed some debt classes --- diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelSynchronizer.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelSynchronizer.java deleted file mode 100644 index 2caec82d248..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelSynchronizer.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.core.technicaldebt; - -import org.apache.commons.io.IOUtils; -import org.apache.ibatis.session.SqlSession; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.ServerExtension; -import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.core.persistence.MyBatis; -import org.sonar.core.technicaldebt.db.CharacteristicDao; -import org.sonar.core.technicaldebt.db.CharacteristicDto; - -import java.io.Reader; -import java.util.List; - -import static com.google.common.collect.Lists.newArrayList; - -public class CharacteristicsDebtModelSynchronizer implements ServerExtension { - - private static final Logger LOG = LoggerFactory.getLogger(CharacteristicsDebtModelSynchronizer.class); - - private final MyBatis mybatis; - private final CharacteristicDao dao; - private final TechnicalDebtModelRepository languageModelFinder; - private final CharacteristicsDebtModelXMLImporter importer; - - public CharacteristicsDebtModelSynchronizer(MyBatis mybatis, CharacteristicDao dao, TechnicalDebtModelRepository modelRepository, CharacteristicsDebtModelXMLImporter importer) { - this.mybatis = mybatis; - this.dao = dao; - this.languageModelFinder = modelRepository; - this.importer = importer; - } - - public List synchronize(ValidationMessages messages) { - SqlSession session = mybatis.openSession(); - - List model = newArrayList(); - try { - model = synchronize(messages, session); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } - return model; - } - - public List synchronize(ValidationMessages messages, SqlSession session) { - DefaultTechnicalDebtModel defaultModel = loadModelFromXml(TechnicalDebtModelRepository.DEFAULT_MODEL, messages); - List model = loadOrCreateModelFromDb(defaultModel, session); - messages.log(LOG); - return model; - } - - private List loadOrCreateModelFromDb(DefaultTechnicalDebtModel defaultModel, SqlSession session) { - List characteristicDtos = loadModel(); - if (characteristicDtos.isEmpty()) { - return createTechnicalDebtModel(defaultModel, session); - } - return characteristicDtos; - } - - private List loadModel() { - return dao.selectEnabledCharacteristics(); - } - - private List createTechnicalDebtModel(DefaultTechnicalDebtModel defaultModel, SqlSession session) { - List characteristics = newArrayList(); - for (DefaultCharacteristic rootCharacteristic : defaultModel.rootCharacteristics()) { - CharacteristicDto rootCharacteristicDto = CharacteristicDto.toDto(rootCharacteristic, null); - dao.insert(rootCharacteristicDto, session); - characteristics.add(rootCharacteristicDto); - for (DefaultCharacteristic characteristic : rootCharacteristic.children()) { - CharacteristicDto characteristicDto = CharacteristicDto.toDto(characteristic, rootCharacteristicDto.getId()); - dao.insert(characteristicDto, session); - characteristics.add(characteristicDto); - } - } - return characteristics; - } - - public DefaultTechnicalDebtModel loadModelFromXml(String pluginKey, ValidationMessages messages) { - Reader xmlFileReader = null; - try { - xmlFileReader = languageModelFinder.createReaderForXMLFile(pluginKey); - return importer.importXML(xmlFileReader, messages); - } finally { - IOUtils.closeQuietly(xmlFileReader); - } - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporter.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporter.java deleted file mode 100644 index 24c6876d3ae..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporter.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.core.technicaldebt; - -import org.apache.commons.lang.StringUtils; -import org.codehaus.stax2.XMLInputFactory2; -import org.codehaus.staxmate.SMInputFactory; -import org.codehaus.staxmate.in.SMHierarchicCursor; -import org.codehaus.staxmate.in.SMInputCursor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.ServerExtension; -import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; -import org.sonar.api.utils.ValidationMessages; - -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamException; - -import java.io.Reader; -import java.io.StringReader; - -public class CharacteristicsDebtModelXMLImporter implements ServerExtension { - - private static final Logger LOG = LoggerFactory.getLogger(CharacteristicsDebtModelXMLImporter.class); - - public static final String CHARACTERISTIC = "chc"; - public static final String CHARACTERISTIC_KEY = "key"; - public static final String CHARACTERISTIC_NAME = "name"; - - public DefaultTechnicalDebtModel importXML(String xml, ValidationMessages messages) { - return importXML(new StringReader(xml), messages); - } - - public DefaultTechnicalDebtModel importXML(Reader xml, ValidationMessages messages) { - DefaultTechnicalDebtModel model = new DefaultTechnicalDebtModel(); - try { - SMInputFactory inputFactory = initStax(); - SMHierarchicCursor cursor = inputFactory.rootElementCursor(xml); - - // advance to - cursor.advance(); - SMInputCursor chcCursor = cursor.childElementCursor(CHARACTERISTIC); - - while (chcCursor.getNext() != null) { - processCharacteristic(model, null, chcCursor, messages); - } - - cursor.getStreamReader().closeCompletely(); - - } catch (XMLStreamException e) { - LOG.error("XML is not valid", e); - messages.addErrorText("XML is not valid: " + e.getMessage()); - } - return model; - } - - private SMInputFactory initStax() { - XMLInputFactory xmlFactory = XMLInputFactory2.newInstance(); - xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); - xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); - xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); - xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); - return new SMInputFactory(xmlFactory); - } - - private DefaultCharacteristic processCharacteristic(DefaultTechnicalDebtModel model, DefaultCharacteristic parent, SMInputCursor chcCursor, - ValidationMessages messages) throws XMLStreamException { - DefaultCharacteristic characteristic = new DefaultCharacteristic(); - SMInputCursor cursor = chcCursor.childElementCursor(); - while (cursor.getNext() != null) { - String node = cursor.getLocalName(); - if (StringUtils.equals(node, CHARACTERISTIC_KEY)) { - characteristic.setKey(cursor.collectDescendantText().trim()); - // Attached to parent only if a key is existing, otherwise characteristic with empty key can be added. - characteristic.setParent(parent); - - } else if (StringUtils.equals(node, CHARACTERISTIC_NAME)) { - characteristic.setName(cursor.collectDescendantText().trim(), false); - - // can contain characteristics or requirements - } else if (StringUtils.equals(node, CHARACTERISTIC)) { - processCharacteristic(model, characteristic, cursor, messages); - - } - } - - if (StringUtils.isNotBlank(characteristic.key()) && characteristic.isRoot()) { - characteristic.setOrder(model.rootCharacteristics().size() + 1); - model.addRootCharacteristic(characteristic); - return characteristic; - } - return null; - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporter.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporter.java new file mode 100644 index 00000000000..5b35bdf02db --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporter.java @@ -0,0 +1,113 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.technicaldebt; + +import org.apache.commons.lang.StringUtils; +import org.codehaus.stax2.XMLInputFactory2; +import org.codehaus.staxmate.SMInputFactory; +import org.codehaus.staxmate.in.SMHierarchicCursor; +import org.codehaus.staxmate.in.SMInputCursor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.ServerExtension; +import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; +import org.sonar.api.utils.ValidationMessages; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; + +import java.io.Reader; +import java.io.StringReader; + +public class DebtCharacteristicsXMLImporter implements ServerExtension { + + private static final Logger LOG = LoggerFactory.getLogger(DebtCharacteristicsXMLImporter.class); + + public static final String CHARACTERISTIC = "chc"; + public static final String CHARACTERISTIC_KEY = "key"; + public static final String CHARACTERISTIC_NAME = "name"; + + public DefaultTechnicalDebtModel importXML(String xml, ValidationMessages messages) { + return importXML(new StringReader(xml), messages); + } + + public DefaultTechnicalDebtModel importXML(Reader xml, ValidationMessages messages) { + DefaultTechnicalDebtModel model = new DefaultTechnicalDebtModel(); + try { + SMInputFactory inputFactory = initStax(); + SMHierarchicCursor cursor = inputFactory.rootElementCursor(xml); + + // advance to + cursor.advance(); + SMInputCursor chcCursor = cursor.childElementCursor(CHARACTERISTIC); + + while (chcCursor.getNext() != null) { + processCharacteristic(model, null, chcCursor, messages); + } + + cursor.getStreamReader().closeCompletely(); + + } catch (XMLStreamException e) { + LOG.error("XML is not valid", e); + messages.addErrorText("XML is not valid: " + e.getMessage()); + } + return model; + } + + private SMInputFactory initStax() { + XMLInputFactory xmlFactory = XMLInputFactory2.newInstance(); + xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); + xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); + xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); + xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); + return new SMInputFactory(xmlFactory); + } + + private DefaultCharacteristic processCharacteristic(DefaultTechnicalDebtModel model, DefaultCharacteristic parent, SMInputCursor chcCursor, + ValidationMessages messages) throws XMLStreamException { + DefaultCharacteristic characteristic = new DefaultCharacteristic(); + SMInputCursor cursor = chcCursor.childElementCursor(); + while (cursor.getNext() != null) { + String node = cursor.getLocalName(); + if (StringUtils.equals(node, CHARACTERISTIC_KEY)) { + characteristic.setKey(cursor.collectDescendantText().trim()); + // Attached to parent only if a key is existing, otherwise characteristic with empty key can be added. + characteristic.setParent(parent); + + } else if (StringUtils.equals(node, CHARACTERISTIC_NAME)) { + characteristic.setName(cursor.collectDescendantText().trim(), false); + + // can contain characteristics or requirements + } else if (StringUtils.equals(node, CHARACTERISTIC)) { + processCharacteristic(model, characteristic, cursor, messages); + + } + } + + if (StringUtils.isNotBlank(characteristic.key()) && characteristic.isRoot()) { + characteristic.setOrder(model.rootCharacteristics().size() + 1); + model.addRootCharacteristic(characteristic); + return characteristic; + } + return null; + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtModelSynchronizer.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtModelSynchronizer.java new file mode 100644 index 00000000000..7437d4d3885 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtModelSynchronizer.java @@ -0,0 +1,112 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.technicaldebt; + +import org.apache.commons.io.IOUtils; +import org.apache.ibatis.session.SqlSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.ServerExtension; +import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.core.persistence.MyBatis; +import org.sonar.core.technicaldebt.db.CharacteristicDao; +import org.sonar.core.technicaldebt.db.CharacteristicDto; + +import java.io.Reader; +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; + +public class DebtModelSynchronizer implements ServerExtension { + + private static final Logger LOG = LoggerFactory.getLogger(DebtModelSynchronizer.class); + + private final MyBatis mybatis; + private final CharacteristicDao dao; + private final TechnicalDebtModelRepository languageModelFinder; + private final DebtCharacteristicsXMLImporter importer; + + public DebtModelSynchronizer(MyBatis mybatis, CharacteristicDao dao, TechnicalDebtModelRepository modelRepository, DebtCharacteristicsXMLImporter importer) { + this.mybatis = mybatis; + this.dao = dao; + this.languageModelFinder = modelRepository; + this.importer = importer; + } + + public List synchronize(ValidationMessages messages) { + SqlSession session = mybatis.openSession(); + + List model = newArrayList(); + try { + model = synchronize(messages, session); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + return model; + } + + public List synchronize(ValidationMessages messages, SqlSession session) { + DefaultTechnicalDebtModel defaultModel = loadModelFromXml(TechnicalDebtModelRepository.DEFAULT_MODEL, messages); + List model = loadOrCreateModelFromDb(defaultModel, session); + messages.log(LOG); + return model; + } + + private List loadOrCreateModelFromDb(DefaultTechnicalDebtModel defaultModel, SqlSession session) { + List characteristicDtos = loadModel(); + if (characteristicDtos.isEmpty()) { + return createTechnicalDebtModel(defaultModel, session); + } + return characteristicDtos; + } + + private List loadModel() { + return dao.selectEnabledCharacteristics(); + } + + private List createTechnicalDebtModel(DefaultTechnicalDebtModel defaultModel, SqlSession session) { + List characteristics = newArrayList(); + for (DefaultCharacteristic rootCharacteristic : defaultModel.rootCharacteristics()) { + CharacteristicDto rootCharacteristicDto = CharacteristicDto.toDto(rootCharacteristic, null); + dao.insert(rootCharacteristicDto, session); + characteristics.add(rootCharacteristicDto); + for (DefaultCharacteristic characteristic : rootCharacteristic.children()) { + CharacteristicDto characteristicDto = CharacteristicDto.toDto(characteristic, rootCharacteristicDto.getId()); + dao.insert(characteristicDto, session); + characteristics.add(characteristicDto); + } + } + return characteristics; + } + + public DefaultTechnicalDebtModel loadModelFromXml(String pluginKey, ValidationMessages messages) { + Reader xmlFileReader = null; + try { + xmlFileReader = languageModelFinder.createReaderForXMLFile(pluginKey); + return importer.importXML(xmlFileReader, messages); + } finally { + IOUtils.closeQuietly(xmlFileReader); + } + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtRulesXMLImporter.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtRulesXMLImporter.java new file mode 100644 index 00000000000..f0a43c34c06 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/technicaldebt/DebtRulesXMLImporter.java @@ -0,0 +1,331 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.technicaldebt; + +import com.google.common.base.Predicate; +import com.google.common.base.Strings; +import com.google.common.collect.Iterables; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.codehaus.stax2.XMLInputFactory2; +import org.codehaus.staxmate.SMInputFactory; +import org.codehaus.staxmate.in.SMHierarchicCursor; +import org.codehaus.staxmate.in.SMInputCursor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.ServerExtension; +import org.sonar.api.rule.RemediationFunction; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.utils.Duration; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; + +import java.io.Reader; +import java.io.StringReader; +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; + +public class DebtRulesXMLImporter implements ServerExtension { + + private static final Logger LOG = LoggerFactory.getLogger(DebtRulesXMLImporter.class); + + public static final String CHARACTERISTIC = "chc"; + public static final String CHARACTERISTIC_KEY = "key"; + public static final String PROPERTY = "prop"; + + public static final String PROPERTY_KEY = "key"; + public static final String PROPERTY_VALUE = "val"; + public static final String PROPERTY_TEXT_VALUE = "txt"; + + public static final String REPOSITORY_KEY = "rule-repo"; + public static final String RULE_KEY = "rule-key"; + + public static final String PROPERTY_FUNCTION = "remediationFunction"; + public static final String PROPERTY_FACTOR = "remediationFactor"; + public static final String PROPERTY_OFFSET = "offset"; + + public List importXML(String xml) { + return importXML(new StringReader(xml)); + } + + public List importXML(Reader xml) { + List ruleDebts = newArrayList(); + try { + SMInputFactory inputFactory = initStax(); + SMHierarchicCursor cursor = inputFactory.rootElementCursor(xml); + + // advance to + cursor.advance(); + SMInputCursor rootCursor = cursor.childElementCursor(CHARACTERISTIC); + while (rootCursor.getNext() != null) { + processCharacteristic(ruleDebts, null, null, rootCursor); + } + + cursor.getStreamReader().closeCompletely(); + } catch (XMLStreamException e) { + LOG.error("XML is not valid", e); + } + + return ruleDebts; + } + + private SMInputFactory initStax() { + XMLInputFactory xmlFactory = XMLInputFactory2.newInstance(); + xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); + xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); + xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); + xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); + return new SMInputFactory(xmlFactory); + } + + private void processCharacteristic(List ruleDebts, @Nullable String rootKey, @Nullable String parentKey, SMInputCursor chcCursor) throws XMLStreamException { + String currentCharacteristicKey = null; + SMInputCursor cursor = chcCursor.childElementCursor(); + while (cursor.getNext() != null) { + String node = cursor.getLocalName(); + if (StringUtils.equals(node, CHARACTERISTIC_KEY)) { + currentCharacteristicKey = cursor.collectDescendantText().trim(); + } else if (StringUtils.equals(node, CHARACTERISTIC)) { + processCharacteristic(ruleDebts, parentKey, currentCharacteristicKey, cursor); + } else if (StringUtils.equals(node, REPOSITORY_KEY)) { + RuleDebt ruleDebt = processRule(cursor); + if (ruleDebt != null) { + if (rootKey != null) { + ruleDebt.characteristicKey = parentKey; + ruleDebts.add(ruleDebt); + } else { + LOG.warn("Rule '" + ruleDebt.ruleKey + "' is ignored because it's defined directly under a root characteristic."); + } + } + } + } + } + + private RuleDebt processRule(SMInputCursor cursor) + throws XMLStreamException { + + RuleDebt ruleDebt = new RuleDebt(); + String ruleRepositoryKey = cursor.collectDescendantText().trim(); + String ruleKey = null; + Properties properties = new Properties(); + while (cursor.getNext() != null) { + String node = cursor.getLocalName(); + if (StringUtils.equals(node, PROPERTY)) { + properties.add(processProperty(cursor)); + } else if (StringUtils.equals(node, RULE_KEY)) { + ruleKey = cursor.collectDescendantText().trim(); + } + } + if (StringUtils.isNotBlank(ruleRepositoryKey) && StringUtils.isNotBlank(ruleKey)) { + ruleDebt.ruleKey = RuleKey.of(ruleRepositoryKey, ruleKey); + } else { + return null; + } + return processFunctionsOnRequirement(ruleDebt, properties); + } + + private Property processProperty(SMInputCursor cursor) throws XMLStreamException { + SMInputCursor c = cursor.childElementCursor(); + String key = null; + int value = 0; + String textValue = null; + while (c.getNext() != null) { + String node = c.getLocalName(); + if (StringUtils.equals(node, PROPERTY_KEY)) { + key = c.collectDescendantText().trim(); + + } else if (StringUtils.equals(node, PROPERTY_VALUE)) { + String s = c.collectDescendantText().trim(); + try { + Double valueDouble = NumberUtils.createDouble(s); + value = valueDouble.intValue(); + } catch (NumberFormatException ex) { + LOG.error(String.format("Cannot import value '%s' for field %s - Expected a numeric value instead", s, key)); + } + } else if (StringUtils.equals(node, PROPERTY_TEXT_VALUE)) { + textValue = c.collectDescendantText().trim(); + } + } + return new Property(key, value, textValue); + } + + @CheckForNull + private RuleDebt processFunctionsOnRequirement(RuleDebt requirement, Properties properties) { + Property function = properties.function(); + Property factor = properties.factor(); + Property offset = properties.offset(); + + if (function != null) { + // Init with default values + requirement.factor = "0" + Duration.DAY; + requirement.offset = "0" + Duration.DAY; + + String functionKey = function.getTextValue(); + if ("linear_threshold".equals(functionKey)) { + function.setTextValue(RemediationFunction.LINEAR.name().toLowerCase()); + offset.setValue(0); + offset.setTextValue(Duration.DAY); + LOG.warn(String.format("Linear with threshold function is no longer used, remediation function of '%s' is replaced by linear.", requirement.ruleKey)); + } else if ("constant_resource".equals(functionKey)) { + LOG.warn(String.format("Constant/file function is no longer used, technical debt definitions on '%s' are ignored.", requirement.ruleKey)); + return null; + } + + requirement.function = RemediationFunction.valueOf(function.getTextValue().toUpperCase()); + if (factor != null) { + requirement.factor = Integer.toString(factor.getValue()); + requirement.factor += !Strings.isNullOrEmpty(factor.getTextValue()) ? factor.getTextValue() : Duration.DAY; + } + if (offset != null) { + requirement.offset = Integer.toString(offset.getValue()); + requirement.offset += !Strings.isNullOrEmpty(offset.getTextValue()) ? offset.getTextValue() : Duration.DAY; + } + return requirement; + } + return null; + } + + private static class Properties { + List properties; + + public Properties() { + this.properties = newArrayList(); + } + + public Properties add(Property property) { + this.properties.add(property); + return this; + } + + public Property function() { + return find(PROPERTY_FUNCTION); + } + + public Property factor() { + return find(PROPERTY_FACTOR); + } + + public Property offset() { + return find(PROPERTY_OFFSET); + } + + private Property find(final String key) { + return Iterables.find(properties, new Predicate() { + @Override + public boolean apply(Property input) { + return input.getKey().equals(key); + } + }, null); + } + } + + private static class Property { + String key; + int value; + String textValue; + + private Property(String key, int value, String textValue) { + this.key = key; + this.value = value; + this.textValue = textValue; + } + + private Property setValue(int value) { + this.value = value; + return this; + } + + private Property setTextValue(String textValue) { + this.textValue = textValue; + return this; + } + + private String getKey() { + return key; + } + + private int getValue() { + return value; + } + + private String getTextValue() { + return "mn".equals(textValue) ? Duration.MINUTE : textValue; + } + } + + public static class RuleDebt { + private RuleKey ruleKey; + private String characteristicKey; + private RemediationFunction function; + private String factor; + private String offset; + + public RuleKey ruleKey() { + return ruleKey; + } + + public RuleDebt setRuleKey(RuleKey ruleKey) { + this.ruleKey = ruleKey; + return this; + } + + public String characteristicKey() { + return characteristicKey; + } + + public RuleDebt setCharacteristicKey(String characteristicKey) { + this.characteristicKey = characteristicKey; + return this; + } + + public RemediationFunction function() { + return function; + } + + public RuleDebt setFunction(RemediationFunction function) { + this.function = function; + return this; + } + + public String factor() { + return factor; + } + + public RuleDebt setFactor(String factor) { + this.factor = factor; + return this; + } + + public String offset() { + return offset; + } + + public RuleDebt setOffset(String offset) { + this.offset = offset; + return this; + } + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/RulesDebtModelXMLImporter.java b/sonar-core/src/main/java/org/sonar/core/technicaldebt/RulesDebtModelXMLImporter.java deleted file mode 100644 index f63c1cc6a8f..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/technicaldebt/RulesDebtModelXMLImporter.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.core.technicaldebt; - -import com.google.common.base.Predicate; -import com.google.common.base.Strings; -import com.google.common.collect.Iterables; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.math.NumberUtils; -import org.codehaus.stax2.XMLInputFactory2; -import org.codehaus.staxmate.SMInputFactory; -import org.codehaus.staxmate.in.SMHierarchicCursor; -import org.codehaus.staxmate.in.SMInputCursor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.ServerExtension; -import org.sonar.api.rule.RemediationFunction; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.Duration; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamException; - -import java.io.Reader; -import java.io.StringReader; -import java.util.List; - -import static com.google.common.collect.Lists.newArrayList; - -public class RulesDebtModelXMLImporter implements ServerExtension { - - private static final Logger LOG = LoggerFactory.getLogger(RulesDebtModelXMLImporter.class); - - public static final String CHARACTERISTIC = "chc"; - public static final String CHARACTERISTIC_KEY = "key"; - public static final String PROPERTY = "prop"; - - public static final String PROPERTY_KEY = "key"; - public static final String PROPERTY_VALUE = "val"; - public static final String PROPERTY_TEXT_VALUE = "txt"; - - public static final String REPOSITORY_KEY = "rule-repo"; - public static final String RULE_KEY = "rule-key"; - - public static final String PROPERTY_FUNCTION = "remediationFunction"; - public static final String PROPERTY_FACTOR = "remediationFactor"; - public static final String PROPERTY_OFFSET = "offset"; - - public List importXML(String xml) { - return importXML(new StringReader(xml)); - } - - public List importXML(Reader xml) { - List ruleDebts = newArrayList(); - try { - SMInputFactory inputFactory = initStax(); - SMHierarchicCursor cursor = inputFactory.rootElementCursor(xml); - - // advance to - cursor.advance(); - SMInputCursor rootCursor = cursor.childElementCursor(CHARACTERISTIC); - while (rootCursor.getNext() != null) { - processCharacteristic(ruleDebts, null, null, rootCursor); - } - - cursor.getStreamReader().closeCompletely(); - } catch (XMLStreamException e) { - LOG.error("XML is not valid", e); - } - - return ruleDebts; - } - - private SMInputFactory initStax() { - XMLInputFactory xmlFactory = XMLInputFactory2.newInstance(); - xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); - xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); - xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); - xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); - return new SMInputFactory(xmlFactory); - } - - private void processCharacteristic(List ruleDebts, @Nullable String rootKey, @Nullable String parentKey, SMInputCursor chcCursor) throws XMLStreamException { - String currentCharacteristicKey = null; - SMInputCursor cursor = chcCursor.childElementCursor(); - while (cursor.getNext() != null) { - String node = cursor.getLocalName(); - if (StringUtils.equals(node, CHARACTERISTIC_KEY)) { - currentCharacteristicKey = cursor.collectDescendantText().trim(); - } else if (StringUtils.equals(node, CHARACTERISTIC)) { - processCharacteristic(ruleDebts, parentKey, currentCharacteristicKey, cursor); - } else if (StringUtils.equals(node, REPOSITORY_KEY)) { - RuleDebt ruleDebt = processRule(cursor); - if (ruleDebt != null) { - if (rootKey != null) { - ruleDebt.characteristicKey = parentKey; - ruleDebts.add(ruleDebt); - } else { - LOG.warn("Rule '" + ruleDebt.ruleKey + "' is ignored because it's defined directly under a root characteristic."); - } - } - } - } - } - - private RuleDebt processRule(SMInputCursor cursor) - throws XMLStreamException { - - RuleDebt ruleDebt = new RuleDebt(); - String ruleRepositoryKey = cursor.collectDescendantText().trim(); - String ruleKey = null; - Properties properties = new Properties(); - while (cursor.getNext() != null) { - String node = cursor.getLocalName(); - if (StringUtils.equals(node, PROPERTY)) { - properties.add(processProperty(cursor)); - } else if (StringUtils.equals(node, RULE_KEY)) { - ruleKey = cursor.collectDescendantText().trim(); - } - } - if (StringUtils.isNotBlank(ruleRepositoryKey) && StringUtils.isNotBlank(ruleKey)) { - ruleDebt.ruleKey = RuleKey.of(ruleRepositoryKey, ruleKey); - } else { - return null; - } - return processFunctionsOnRequirement(ruleDebt, properties); - } - - private Property processProperty(SMInputCursor cursor) throws XMLStreamException { - SMInputCursor c = cursor.childElementCursor(); - String key = null; - int value = 0; - String textValue = null; - while (c.getNext() != null) { - String node = c.getLocalName(); - if (StringUtils.equals(node, PROPERTY_KEY)) { - key = c.collectDescendantText().trim(); - - } else if (StringUtils.equals(node, PROPERTY_VALUE)) { - String s = c.collectDescendantText().trim(); - try { - Double valueDouble = NumberUtils.createDouble(s); - value = valueDouble.intValue(); - } catch (NumberFormatException ex) { - LOG.error(String.format("Cannot import value '%s' for field %s - Expected a numeric value instead", s, key)); - } - } else if (StringUtils.equals(node, PROPERTY_TEXT_VALUE)) { - textValue = c.collectDescendantText().trim(); - } - } - return new Property(key, value, textValue); - } - - @CheckForNull - private RuleDebt processFunctionsOnRequirement(RuleDebt requirement, Properties properties) { - Property function = properties.function(); - Property factor = properties.factor(); - Property offset = properties.offset(); - - if (function != null) { - // Init with default values - requirement.factor = "0" + Duration.DAY; - requirement.offset = "0" + Duration.DAY; - - String functionKey = function.getTextValue(); - if ("linear_threshold".equals(functionKey)) { - function.setTextValue(RemediationFunction.LINEAR.name().toLowerCase()); - offset.setValue(0); - offset.setTextValue(Duration.DAY); - LOG.warn(String.format("Linear with threshold function is no longer used, remediation function of '%s' is replaced by linear.", requirement.ruleKey)); - } else if ("constant_resource".equals(functionKey)) { - LOG.warn(String.format("Constant/file function is no longer used, technical debt definitions on '%s' are ignored.", requirement.ruleKey)); - return null; - } - - requirement.function = RemediationFunction.valueOf(function.getTextValue().toUpperCase()); - if (factor != null) { - requirement.factor = Integer.toString(factor.getValue()); - requirement.factor += !Strings.isNullOrEmpty(factor.getTextValue()) ? factor.getTextValue() : Duration.DAY; - } - if (offset != null) { - requirement.offset = Integer.toString(offset.getValue()); - requirement.offset += !Strings.isNullOrEmpty(offset.getTextValue()) ? offset.getTextValue() : Duration.DAY; - } - return requirement; - } - return null; - } - - private static class Properties { - List properties; - - public Properties() { - this.properties = newArrayList(); - } - - public Properties add(Property property) { - this.properties.add(property); - return this; - } - - public Property function() { - return find(PROPERTY_FUNCTION); - } - - public Property factor() { - return find(PROPERTY_FACTOR); - } - - public Property offset() { - return find(PROPERTY_OFFSET); - } - - private Property find(final String key) { - return Iterables.find(properties, new Predicate() { - @Override - public boolean apply(Property input) { - return input.getKey().equals(key); - } - }, null); - } - } - - private static class Property { - String key; - int value; - String textValue; - - private Property(String key, int value, String textValue) { - this.key = key; - this.value = value; - this.textValue = textValue; - } - - private Property setValue(int value) { - this.value = value; - return this; - } - - private Property setTextValue(String textValue) { - this.textValue = textValue; - return this; - } - - private String getKey() { - return key; - } - - private int getValue() { - return value; - } - - private String getTextValue() { - return "mn".equals(textValue) ? Duration.MINUTE : textValue; - } - } - - public static class RuleDebt { - private RuleKey ruleKey; - private String characteristicKey; - private RemediationFunction function; - private String factor; - private String offset; - - public RuleKey ruleKey() { - return ruleKey; - } - - public RuleDebt setRuleKey(RuleKey ruleKey) { - this.ruleKey = ruleKey; - return this; - } - - public String characteristicKey() { - return characteristicKey; - } - - public RuleDebt setCharacteristicKey(String characteristicKey) { - this.characteristicKey = characteristicKey; - return this; - } - - public RemediationFunction function() { - return function; - } - - public RuleDebt setFunction(RemediationFunction function) { - this.function = function; - return this; - } - - public String factor() { - return factor; - } - - public RuleDebt setFactor(String factor) { - this.factor = factor; - return this; - } - - public String offset() { - return offset; - } - - public RuleDebt setOffset(String offset) { - this.offset = offset; - return this; - } - } - -} diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelSynchronizerTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelSynchronizerTest.java deleted file mode 100644 index bdcd88db979..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelSynchronizerTest.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.core.technicaldebt; - -import com.google.common.collect.Lists; -import org.apache.ibatis.session.SqlSession; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.runners.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; -import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.core.persistence.MyBatis; -import org.sonar.core.technicaldebt.db.CharacteristicDao; -import org.sonar.core.technicaldebt.db.CharacteristicDto; - -import java.io.Reader; -import java.util.Collections; -import java.util.List; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.*; - -@RunWith(MockitoJUnitRunner.class) -public class CharacteristicsDebtModelSynchronizerTest { - - @Mock - MyBatis myBatis; - - @Mock - SqlSession session; - - @Mock - TechnicalDebtModelRepository technicalDebtModelRepository; - - @Mock - CharacteristicDao dao; - - @Mock - CharacteristicsDebtModelXMLImporter xmlImporter; - - Integer currentId = 1; - - private DefaultTechnicalDebtModel defaultModel; - - private CharacteristicsDebtModelSynchronizer manager; - - @Before - public void initAndMerge() throws Exception { - when(myBatis.openSession()).thenReturn(session); - - defaultModel = new DefaultTechnicalDebtModel(); - Reader defaultModelReader = mock(Reader.class); - when(technicalDebtModelRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader); - when(xmlImporter.importXML(eq(defaultModelReader), any(ValidationMessages.class))).thenReturn(defaultModel); - - doAnswer(new Answer() { - public Object answer(InvocationOnMock invocation) { - Object[] args = invocation.getArguments(); - CharacteristicDto dto = (CharacteristicDto) args[0]; - dto.setId(currentId++); - return null; - } - }).when(dao).insert(any(CharacteristicDto.class), any(SqlSession.class)); - - - manager = new CharacteristicsDebtModelSynchronizer(myBatis, dao, technicalDebtModelRepository, xmlImporter); - } - - @Test - public void create_default_model_on_first_execution_when_no_plugin() throws Exception { - DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("PORTABILITY"); - new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic); - defaultModel.addRootCharacteristic(rootCharacteristic); - - when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.emptyList()); - when(dao.selectEnabledCharacteristics()).thenReturn(Lists.newArrayList()); - - manager.synchronize(ValidationMessages.create()); - - verify(dao).selectEnabledCharacteristics(); - ArgumentCaptor characteristicCaptor = ArgumentCaptor.forClass(CharacteristicDto.class); - verify(dao, times(2)).insert(characteristicCaptor.capture(), eq(session)); - - List result = characteristicCaptor.getAllValues(); - assertThat(result.get(0).getKey()).isEqualTo("PORTABILITY"); - assertThat(result.get(1).getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY"); - verifyNoMoreInteractions(dao); - } - - @Test - public void not_create_default_model_if_already_exists() throws Exception { - DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("PORTABILITY"); - new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic); - defaultModel.addRootCharacteristic(rootCharacteristic); - - when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.emptyList()); - when(dao.selectEnabledCharacteristics()).thenReturn(Lists.newArrayList(new CharacteristicDto())); - - manager.synchronize(ValidationMessages.create()); - - verify(dao, never()).insert(any(CharacteristicDto.class), eq(session)); - } - -} diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest.java deleted file mode 100644 index 4e2f75e5ff7..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.core.technicaldebt; - -import com.google.common.base.Charsets; -import com.google.common.io.Resources; -import org.junit.Test; -import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; -import org.sonar.api.utils.ValidationMessages; - -import java.io.IOException; - -import static org.fest.assertions.Assertions.assertThat; - -public class CharacteristicsDebtModelXMLImporterTest { - - @Test - public void import_characteristics() { - String xml = getFileContent("import_characteristics.xml"); - - ValidationMessages messages = ValidationMessages.create(); - DefaultTechnicalDebtModel debtModel = new CharacteristicsDebtModelXMLImporter().importXML(xml, messages); - - assertThat(debtModel.rootCharacteristics()).hasSize(2); - assertThat(debtModel.rootCharacteristics().get(0).key()).isEqualTo("PORTABILITY"); - assertThat(debtModel.rootCharacteristics().get(1).key()).isEqualTo("MAINTAINABILITY"); - - DefaultCharacteristic portability = debtModel.characteristicByKey("PORTABILITY"); - assertThat(portability.order()).isEqualTo(1); - assertThat(portability.children()).hasSize(2); - assertThat(portability.children().get(0).key()).isEqualTo("COMPILER_RELATED_PORTABILITY"); - assertThat(debtModel.characteristicByKey("COMPILER_RELATED_PORTABILITY").parent().key()).isEqualTo("PORTABILITY"); - assertThat(portability.children().get(1).key()).isEqualTo("HARDWARE_RELATED_PORTABILITY"); - assertThat(debtModel.characteristicByKey("HARDWARE_RELATED_PORTABILITY").parent().key()).isEqualTo("PORTABILITY"); - - DefaultCharacteristic maintainability = debtModel.characteristicByKey("MAINTAINABILITY"); - assertThat(maintainability.order()).isEqualTo(2); - assertThat(maintainability.children()).hasSize(1); - assertThat(maintainability.children().get(0).key()).isEqualTo("READABILITY"); - assertThat(debtModel.characteristicByKey("READABILITY").parent().key()).isEqualTo("MAINTAINABILITY"); - } - - @Test - public void import_badly_formatted_xml() { - String xml = getFileContent("import_badly_formatted_xml.xml"); - - ValidationMessages messages = ValidationMessages.create(); - DefaultTechnicalDebtModel debtModel = new CharacteristicsDebtModelXMLImporter().importXML(xml, messages); - - checkXmlCorrectlyImported(debtModel, messages); - } - - private void checkXmlCorrectlyImported(DefaultTechnicalDebtModel sqale, ValidationMessages messages) { - assertThat(messages.getErrors()).isEmpty(); - - // characteristics - assertThat(sqale.rootCharacteristics()).hasSize(2); - DefaultCharacteristic efficiency = sqale.characteristicByKey("EFFICIENCY"); - assertThat(efficiency.name()).isEqualTo("Efficiency"); - - // sub-characteristics - assertThat(efficiency.children()).hasSize(1); - DefaultCharacteristic memoryEfficiency = sqale.characteristicByKey("MEMORY_EFFICIENCY"); - assertThat(memoryEfficiency.name()).isEqualTo("Memory use"); - } - - private String getFileContent(String file) { - try { - return Resources.toString(Resources.getResource(CharacteristicsDebtModelXMLImporterTest.class, "CharacteristicsDebtModelXMLImporterTest/" + file), Charsets.UTF_8); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest.java new file mode 100644 index 00000000000..d1a924636a3 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest.java @@ -0,0 +1,93 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.technicaldebt; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; +import org.junit.Test; +import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; +import org.sonar.api.utils.ValidationMessages; + +import java.io.IOException; + +import static org.fest.assertions.Assertions.assertThat; + +public class DebtCharacteristicsXMLImporterTest { + + @Test + public void import_characteristics() { + String xml = getFileContent("import_characteristics.xml"); + + ValidationMessages messages = ValidationMessages.create(); + DefaultTechnicalDebtModel debtModel = new DebtCharacteristicsXMLImporter().importXML(xml, messages); + + assertThat(debtModel.rootCharacteristics()).hasSize(2); + assertThat(debtModel.rootCharacteristics().get(0).key()).isEqualTo("PORTABILITY"); + assertThat(debtModel.rootCharacteristics().get(1).key()).isEqualTo("MAINTAINABILITY"); + + DefaultCharacteristic portability = debtModel.characteristicByKey("PORTABILITY"); + assertThat(portability.order()).isEqualTo(1); + assertThat(portability.children()).hasSize(2); + assertThat(portability.children().get(0).key()).isEqualTo("COMPILER_RELATED_PORTABILITY"); + assertThat(debtModel.characteristicByKey("COMPILER_RELATED_PORTABILITY").parent().key()).isEqualTo("PORTABILITY"); + assertThat(portability.children().get(1).key()).isEqualTo("HARDWARE_RELATED_PORTABILITY"); + assertThat(debtModel.characteristicByKey("HARDWARE_RELATED_PORTABILITY").parent().key()).isEqualTo("PORTABILITY"); + + DefaultCharacteristic maintainability = debtModel.characteristicByKey("MAINTAINABILITY"); + assertThat(maintainability.order()).isEqualTo(2); + assertThat(maintainability.children()).hasSize(1); + assertThat(maintainability.children().get(0).key()).isEqualTo("READABILITY"); + assertThat(debtModel.characteristicByKey("READABILITY").parent().key()).isEqualTo("MAINTAINABILITY"); + } + + @Test + public void import_badly_formatted_xml() { + String xml = getFileContent("import_badly_formatted_xml.xml"); + + ValidationMessages messages = ValidationMessages.create(); + DefaultTechnicalDebtModel debtModel = new DebtCharacteristicsXMLImporter().importXML(xml, messages); + + checkXmlCorrectlyImported(debtModel, messages); + } + + private void checkXmlCorrectlyImported(DefaultTechnicalDebtModel sqale, ValidationMessages messages) { + assertThat(messages.getErrors()).isEmpty(); + + // characteristics + assertThat(sqale.rootCharacteristics()).hasSize(2); + DefaultCharacteristic efficiency = sqale.characteristicByKey("EFFICIENCY"); + assertThat(efficiency.name()).isEqualTo("Efficiency"); + + // sub-characteristics + assertThat(efficiency.children()).hasSize(1); + DefaultCharacteristic memoryEfficiency = sqale.characteristicByKey("MEMORY_EFFICIENCY"); + assertThat(memoryEfficiency.name()).isEqualTo("Memory use"); + } + + private String getFileContent(String file) { + try { + return Resources.toString(Resources.getResource(DebtCharacteristicsXMLImporterTest.class, "DebtCharacteristicsXMLImporterTest/" + file), Charsets.UTF_8); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtModelSynchronizerTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtModelSynchronizerTest.java new file mode 100644 index 00000000000..64525b009d9 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtModelSynchronizerTest.java @@ -0,0 +1,129 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.technicaldebt; + +import com.google.common.collect.Lists; +import org.apache.ibatis.session.SqlSession; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.core.persistence.MyBatis; +import org.sonar.core.technicaldebt.db.CharacteristicDao; +import org.sonar.core.technicaldebt.db.CharacteristicDto; + +import java.io.Reader; +import java.util.Collections; +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class DebtModelSynchronizerTest { + + @Mock + MyBatis myBatis; + + @Mock + SqlSession session; + + @Mock + TechnicalDebtModelRepository technicalDebtModelRepository; + + @Mock + CharacteristicDao dao; + + @Mock + DebtCharacteristicsXMLImporter xmlImporter; + + Integer currentId = 1; + + private DefaultTechnicalDebtModel defaultModel; + + private DebtModelSynchronizer manager; + + @Before + public void initAndMerge() throws Exception { + when(myBatis.openSession()).thenReturn(session); + + defaultModel = new DefaultTechnicalDebtModel(); + Reader defaultModelReader = mock(Reader.class); + when(technicalDebtModelRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader); + when(xmlImporter.importXML(eq(defaultModelReader), any(ValidationMessages.class))).thenReturn(defaultModel); + + doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) { + Object[] args = invocation.getArguments(); + CharacteristicDto dto = (CharacteristicDto) args[0]; + dto.setId(currentId++); + return null; + } + }).when(dao).insert(any(CharacteristicDto.class), any(SqlSession.class)); + + + manager = new DebtModelSynchronizer(myBatis, dao, technicalDebtModelRepository, xmlImporter); + } + + @Test + public void create_default_model_on_first_execution_when_no_plugin() throws Exception { + DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("PORTABILITY"); + new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic); + defaultModel.addRootCharacteristic(rootCharacteristic); + + when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.emptyList()); + when(dao.selectEnabledCharacteristics()).thenReturn(Lists.newArrayList()); + + manager.synchronize(ValidationMessages.create()); + + verify(dao).selectEnabledCharacteristics(); + ArgumentCaptor characteristicCaptor = ArgumentCaptor.forClass(CharacteristicDto.class); + verify(dao, times(2)).insert(characteristicCaptor.capture(), eq(session)); + + List result = characteristicCaptor.getAllValues(); + assertThat(result.get(0).getKey()).isEqualTo("PORTABILITY"); + assertThat(result.get(1).getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY"); + verifyNoMoreInteractions(dao); + } + + @Test + public void not_create_default_model_if_already_exists() throws Exception { + DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("PORTABILITY"); + new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic); + defaultModel.addRootCharacteristic(rootCharacteristic); + + when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.emptyList()); + when(dao.selectEnabledCharacteristics()).thenReturn(Lists.newArrayList(new CharacteristicDto())); + + manager.synchronize(ValidationMessages.create()); + + verify(dao, never()).insert(any(CharacteristicDto.class), eq(session)); + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest.java new file mode 100644 index 00000000000..5fd8081867b --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest.java @@ -0,0 +1,177 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.technicaldebt; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; +import org.junit.Test; +import org.sonar.api.rule.RemediationFunction; +import org.sonar.api.rule.RuleKey; + +import java.io.IOException; +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; + +public class DebtRulesXMLImporterTest { + + DebtRulesXMLImporter importer = new DebtRulesXMLImporter(); + + @Test + public void import_rules() { + String xml = getFileContent("import_rules.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(2); + } + + @Test + public void import_linear() { + String xml = getFileContent("import_linear.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(1); + + DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp")); + assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); + assertThat(ruleDebt.factor()).isEqualTo("3h"); + assertThat(ruleDebt.offset()).isEqualTo("0d"); + } + + @Test + public void import_linear_with_offset() { + String xml = getFileContent("import_linear_with_offset.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(1); + + DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR_OFFSET); + assertThat(ruleDebt.factor()).isEqualTo("3h"); + assertThat(ruleDebt.offset()).isEqualTo("1min"); + } + + @Test + public void import_constant_issue() { + String xml = getFileContent("import_constant_issue.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(1); + + DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.CONSTANT_ISSUE); + assertThat(ruleDebt.factor()).isEqualTo("0d"); + assertThat(ruleDebt.offset()).isEqualTo("3d"); + } + + @Test + public void use_default_unit_when_no_unit() { + String xml = getFileContent("use_default_unit_when_no_unit.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(1); + + DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); + assertThat(ruleDebt.factor()).isEqualTo("3d"); + assertThat(ruleDebt.offset()).isEqualTo("1d"); + } + + @Test + public void replace_mn_by_min() { + String xml = getFileContent("replace_mn_by_min.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(1); + + DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); + assertThat(ruleDebt.factor()).isEqualTo("3min"); + assertThat(ruleDebt.offset()).isEqualTo("0d"); + } + + @Test + public void convert_deprecated_linear_with_threshold_function_by_linear_function() { + String xml = getFileContent("convert_deprecated_linear_with_threshold_function_by_linear_function.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(1); + + DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); + assertThat(ruleDebt.factor()).isEqualTo("3h"); + assertThat(ruleDebt.offset()).isEqualTo("0d"); + } + + @Test + public void ignore_deprecated_constant_per_file_function() { + String xml = getFileContent("ignore_deprecated_constant_per_file_function.xml"); + + List results = importer.importXML(xml); + assertThat(results).isEmpty(); + } + + @Test + public void ignore_rule_on_root_characteristics() { + String xml = getFileContent("ignore_rule_on_root_characteristics.xml"); + + List results = importer.importXML(xml); + assertThat(results).isEmpty(); + } + + @Test + public void import_badly_formatted_xml() { + String xml = getFileContent("import_badly_formatted_xml.xml"); + + List results = importer.importXML(xml); + assertThat(results).hasSize(1); + + DebtRulesXMLImporter.RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp")); + assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); + assertThat(ruleDebt.factor()).isEqualTo("3h"); + assertThat(ruleDebt.offset()).isEqualTo("0d"); + } + + @Test + public void ignore_invalid_value() throws Exception { + String xml = getFileContent("ignore_invalid_value.xml"); + List results = importer.importXML(xml); + assertThat(results).isEmpty(); + } + + private String getFileContent(String file) { + try { + return Resources.toString(Resources.getResource(DebtRulesXMLImporterTest.class, "DebtRulesXMLImporterTest/" + file), Charsets.UTF_8); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest.java deleted file mode 100644 index bdd72eb7736..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.core.technicaldebt; - -import com.google.common.base.Charsets; -import com.google.common.io.Resources; -import org.junit.Test; -import org.sonar.api.rule.RemediationFunction; -import org.sonar.api.rule.RuleKey; - -import java.io.IOException; -import java.util.List; - -import static org.fest.assertions.Assertions.assertThat; - -public class RulesDebtModelXMLImporterTest { - - RulesDebtModelXMLImporter importer = new RulesDebtModelXMLImporter(); - - @Test - public void import_rules() { - String xml = getFileContent("import_rules.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(2); - } - - @Test - public void import_linear() { - String xml = getFileContent("import_linear.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(1); - - RulesDebtModelXMLImporter.RuleDebt ruleDebt = results.get(0); - assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp")); - assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); - assertThat(ruleDebt.factor()).isEqualTo("3h"); - assertThat(ruleDebt.offset()).isEqualTo("0d"); - } - - @Test - public void import_linear_with_offset() { - String xml = getFileContent("import_linear_with_offset.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(1); - - RulesDebtModelXMLImporter.RuleDebt ruleDebt = results.get(0); - assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR_OFFSET); - assertThat(ruleDebt.factor()).isEqualTo("3h"); - assertThat(ruleDebt.offset()).isEqualTo("1min"); - } - - @Test - public void import_constant_issue() { - String xml = getFileContent("import_constant_issue.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(1); - - RulesDebtModelXMLImporter.RuleDebt ruleDebt = results.get(0); - assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.CONSTANT_ISSUE); - assertThat(ruleDebt.factor()).isEqualTo("0d"); - assertThat(ruleDebt.offset()).isEqualTo("3d"); - } - - @Test - public void use_default_unit_when_no_unit() { - String xml = getFileContent("use_default_unit_when_no_unit.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(1); - - RulesDebtModelXMLImporter.RuleDebt ruleDebt = results.get(0); - assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); - assertThat(ruleDebt.factor()).isEqualTo("3d"); - assertThat(ruleDebt.offset()).isEqualTo("1d"); - } - - @Test - public void replace_mn_by_min() { - String xml = getFileContent("replace_mn_by_min.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(1); - - RulesDebtModelXMLImporter.RuleDebt ruleDebt = results.get(0); - assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); - assertThat(ruleDebt.factor()).isEqualTo("3min"); - assertThat(ruleDebt.offset()).isEqualTo("0d"); - } - - @Test - public void convert_deprecated_linear_with_threshold_function_by_linear_function() { - String xml = getFileContent("convert_deprecated_linear_with_threshold_function_by_linear_function.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(1); - - RulesDebtModelXMLImporter.RuleDebt ruleDebt = results.get(0); - assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); - assertThat(ruleDebt.factor()).isEqualTo("3h"); - assertThat(ruleDebt.offset()).isEqualTo("0d"); - } - - @Test - public void ignore_deprecated_constant_per_file_function() { - String xml = getFileContent("ignore_deprecated_constant_per_file_function.xml"); - - List results = importer.importXML(xml); - assertThat(results).isEmpty(); - } - - @Test - public void ignore_rule_on_root_characteristics() { - String xml = getFileContent("ignore_rule_on_root_characteristics.xml"); - - List results = importer.importXML(xml); - assertThat(results).isEmpty(); - } - - @Test - public void import_badly_formatted_xml() { - String xml = getFileContent("import_badly_formatted_xml.xml"); - - List results = importer.importXML(xml); - assertThat(results).hasSize(1); - - RulesDebtModelXMLImporter.RuleDebt ruleDebt = results.get(0); - assertThat(ruleDebt.characteristicKey()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp")); - assertThat(ruleDebt.function()).isEqualTo(RemediationFunction.LINEAR); - assertThat(ruleDebt.factor()).isEqualTo("3h"); - assertThat(ruleDebt.offset()).isEqualTo("0d"); - } - - @Test - public void ignore_invalid_value() throws Exception { - String xml = getFileContent("ignore_invalid_value.xml"); - List results = importer.importXML(xml); - assertThat(results).isEmpty(); - } - - private String getFileContent(String file) { - try { - return Resources.toString(Resources.getResource(RulesDebtModelXMLImporterTest.class, "RulesDebtModelXMLImporterTest/" + file), Charsets.UTF_8); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest/import_badly_formatted_xml.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest/import_badly_formatted_xml.xml deleted file mode 100644 index 7f19cf39efb..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest/import_badly_formatted_xml.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - USABILITY - - Usability - - Estimate usability - - - - EFFICIENCY - - Efficiency - - - MEMORY_EFFICIENCY - - Memory use - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest/import_characteristics.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest/import_characteristics.xml deleted file mode 100644 index bc7f7569560..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/CharacteristicsDebtModelXMLImporterTest/import_characteristics.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - PORTABILITY - Portability - - COMPILER_RELATED_PORTABILITY - Compiler related portability - - - HARDWARE_RELATED_PORTABILITY - Hardware related portability - - - - MAINTAINABILITY - Maintainability - - READABILITY - Readability - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest/import_badly_formatted_xml.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest/import_badly_formatted_xml.xml new file mode 100644 index 00000000000..7f19cf39efb --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest/import_badly_formatted_xml.xml @@ -0,0 +1,43 @@ + + + + + USABILITY + + Usability + + Estimate usability + + + + EFFICIENCY + + Efficiency + + + MEMORY_EFFICIENCY + + Memory use + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest/import_characteristics.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest/import_characteristics.xml new file mode 100644 index 00000000000..bc7f7569560 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtCharacteristicsXMLImporterTest/import_characteristics.xml @@ -0,0 +1,22 @@ + + + PORTABILITY + Portability + + COMPILER_RELATED_PORTABILITY + Compiler related portability + + + HARDWARE_RELATED_PORTABILITY + Hardware related portability + + + + MAINTAINABILITY + Maintainability + + READABILITY + Readability + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/convert_deprecated_linear_with_threshold_function_by_linear_function.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/convert_deprecated_linear_with_threshold_function_by_linear_function.xml new file mode 100644 index 00000000000..9ebc69b94a6 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/convert_deprecated_linear_with_threshold_function_by_linear_function.xml @@ -0,0 +1,36 @@ + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + remediationFunction + + linear_threshold + + + remediationFactor + 3.0 + h + + + + offset + 1.0 + h + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_deprecated_constant_per_file_function.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_deprecated_constant_per_file_function.xml new file mode 100644 index 00000000000..4b8ae3f6475 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_deprecated_constant_per_file_function.xml @@ -0,0 +1,25 @@ + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + + checkstyle + Regexp + + remediationFactor + 3.0 + h + + + remediationFunction + constant_resource + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_invalid_value.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_invalid_value.xml new file mode 100644 index 00000000000..bb6bdbb4afb --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_invalid_value.xml @@ -0,0 +1,28 @@ + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + factor + abc + + + function + linear + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_rule_on_root_characteristics.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_rule_on_root_characteristics.xml new file mode 100644 index 00000000000..bcf3ed867d3 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/ignore_rule_on_root_characteristics.xml @@ -0,0 +1,19 @@ + + + EFFICIENCY + Efficiency + + checkstyle + Regexp + + remediationFactor + 3.0 + h + + + remediationFunction + linear + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_badly_formatted_xml.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_badly_formatted_xml.xml new file mode 100644 index 00000000000..6c7d153992c --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_badly_formatted_xml.xml @@ -0,0 +1,43 @@ + + + USABILITY + + Usability + + Estimate usability + + + + EFFICIENCY + + Efficiency + + + MEMORY_EFFICIENCY + + Memory use + + + checkstyle + + Regexp + + + remediationFactor + + 3.0 + + h + + + + remediationFunction + + linear + + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_constant_issue.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_constant_issue.xml new file mode 100644 index 00000000000..86b1f551fbe --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_constant_issue.xml @@ -0,0 +1,29 @@ + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + offset + 3.0 + d + + + remediationFunction + constant_issue + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_linear.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_linear.xml new file mode 100644 index 00000000000..f641a5185ec --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_linear.xml @@ -0,0 +1,29 @@ + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + remediationFactor + 3.0 + h + + + remediationFunction + linear + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_linear_with_offset.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_linear_with_offset.xml new file mode 100644 index 00000000000..af05b27015b --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_linear_with_offset.xml @@ -0,0 +1,54 @@ + + + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + remediationFactor + 3.0 + h + + + remediationFunction + linear_offset + + + offset + 1.0 + min + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_rules.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_rules.xml new file mode 100644 index 00000000000..d035d7bde00 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/import_rules.xml @@ -0,0 +1,58 @@ + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + remediationFactor + 3.0 + h + + + remediationFunction + linear + + + + + + PORTABILITY + Portability + + COMPILER_RELATED_PORTABILITY + Compiler related portability + + + HARDWARE_RELATED_PORTABILITY + Hardware related portability + + checkstyle + Regexp2 + + remediationFactor + 3.0 + h + + + remediationFunction + linear + + + offset + 1.0 + h + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/replace_mn_by_min.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/replace_mn_by_min.xml new file mode 100644 index 00000000000..a1aafd489b6 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/replace_mn_by_min.xml @@ -0,0 +1,49 @@ + + + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + remediationFactor + 3.0 + mn + + + remediationFunction + linear + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/use_default_unit_when_no_unit.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/use_default_unit_when_no_unit.xml new file mode 100644 index 00000000000..f05e51228b3 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/DebtRulesXMLImporterTest/use_default_unit_when_no_unit.xml @@ -0,0 +1,32 @@ + + + USABILITY + Usability + Estimate usability + + + EFFICIENCY + Efficiency + + MEMORY_EFFICIENCY + Memory use + + checkstyle + Regexp + + remediationFactor + 3.0 + + + remediationFunction + linear + + + offset + 1.0 + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/convert_deprecated_linear_with_threshold_function_by_linear_function.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/convert_deprecated_linear_with_threshold_function_by_linear_function.xml deleted file mode 100644 index 9ebc69b94a6..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/convert_deprecated_linear_with_threshold_function_by_linear_function.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - remediationFunction - - linear_threshold - - - remediationFactor - 3.0 - h - - - - offset - 1.0 - h - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_deprecated_constant_per_file_function.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_deprecated_constant_per_file_function.xml deleted file mode 100644 index 4b8ae3f6475..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_deprecated_constant_per_file_function.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - - checkstyle - Regexp - - remediationFactor - 3.0 - h - - - remediationFunction - constant_resource - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_invalid_value.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_invalid_value.xml deleted file mode 100644 index bb6bdbb4afb..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_invalid_value.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - factor - abc - - - function - linear - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_rule_on_root_characteristics.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_rule_on_root_characteristics.xml deleted file mode 100644 index bcf3ed867d3..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/ignore_rule_on_root_characteristics.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - EFFICIENCY - Efficiency - - checkstyle - Regexp - - remediationFactor - 3.0 - h - - - remediationFunction - linear - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_badly_formatted_xml.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_badly_formatted_xml.xml deleted file mode 100644 index 6c7d153992c..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_badly_formatted_xml.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - USABILITY - - Usability - - Estimate usability - - - - EFFICIENCY - - Efficiency - - - MEMORY_EFFICIENCY - - Memory use - - - checkstyle - - Regexp - - - remediationFactor - - 3.0 - - h - - - - remediationFunction - - linear - - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_constant_issue.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_constant_issue.xml deleted file mode 100644 index 86b1f551fbe..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_constant_issue.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - offset - 3.0 - d - - - remediationFunction - constant_issue - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_linear.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_linear.xml deleted file mode 100644 index f641a5185ec..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_linear.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - remediationFactor - 3.0 - h - - - remediationFunction - linear - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_linear_with_offset.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_linear_with_offset.xml deleted file mode 100644 index af05b27015b..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_linear_with_offset.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - remediationFactor - 3.0 - h - - - remediationFunction - linear_offset - - - offset - 1.0 - min - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_rules.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_rules.xml deleted file mode 100644 index d035d7bde00..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/import_rules.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - remediationFactor - 3.0 - h - - - remediationFunction - linear - - - - - - PORTABILITY - Portability - - COMPILER_RELATED_PORTABILITY - Compiler related portability - - - HARDWARE_RELATED_PORTABILITY - Hardware related portability - - checkstyle - Regexp2 - - remediationFactor - 3.0 - h - - - remediationFunction - linear - - - offset - 1.0 - h - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/replace_mn_by_min.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/replace_mn_by_min.xml deleted file mode 100644 index a1aafd489b6..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/replace_mn_by_min.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - remediationFactor - 3.0 - mn - - - remediationFunction - linear - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/use_default_unit_when_no_unit.xml b/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/use_default_unit_when_no_unit.xml deleted file mode 100644 index f05e51228b3..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/technicaldebt/RulesDebtModelXMLImporterTest/use_default_unit_when_no_unit.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - USABILITY - Usability - Estimate usability - - - EFFICIENCY - Efficiency - - MEMORY_EFFICIENCY - Memory use - - checkstyle - Regexp - - remediationFactor - 3.0 - - - remediationFunction - linear - - - offset - 1.0 - - - - - - diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 556a357015c..b72c019dc07 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -384,11 +384,11 @@ public final class Platform { // technical debt servicesContainer.addSingleton(DebtService.class); servicesContainer.addSingleton(TechnicalDebtModelSynchronizer.class); - servicesContainer.addSingleton(CharacteristicsDebtModelSynchronizer.class); + servicesContainer.addSingleton(DebtModelSynchronizer.class); servicesContainer.addSingleton(TechnicalDebtModelRepository.class); servicesContainer.addSingleton(TechnicalDebtXMLImporter.class); - servicesContainer.addSingleton(RulesDebtModelXMLImporter.class); - servicesContainer.addSingleton(CharacteristicsDebtModelXMLImporter.class); + servicesContainer.addSingleton(DebtRulesXMLImporter.class); + servicesContainer.addSingleton(DebtCharacteristicsXMLImporter.class); servicesContainer.addSingleton(DefaultTechnicalDebtManager.class); // source @@ -437,8 +437,7 @@ public final class Platform { startupContainer.addSingleton(RuleRegistration.class); startupContainer.addSingleton(RegisterNewProfiles.class); startupContainer.addSingleton(JdbcDriverDeployer.class); - startupContainer.addSingleton(RegisterDebtCharacteristicModel.class); - startupContainer.addSingleton(RegisterTechnicalDebtModel.class); + startupContainer.addSingleton(RegisterDebtModel.class); startupContainer.addSingleton(GeneratePluginIndex.class); startupContainer.addSingleton(GenerateBootstrapIndex.class); startupContainer.addSingleton(RegisterNewMeasureFilters.class); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java index 571d4af77ce..42171caef46 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java @@ -31,7 +31,7 @@ import org.sonar.api.server.rule.RuleDefinitions; import org.sonar.api.server.rule.RuleParamType; import org.sonar.check.Cardinality; import org.sonar.core.i18n.RuleI18nManager; -import org.sonar.core.technicaldebt.RulesDebtModelXMLImporter; +import org.sonar.core.technicaldebt.DebtRulesXMLImporter; import org.sonar.core.technicaldebt.TechnicalDebtModelRepository; import javax.annotation.CheckForNull; @@ -53,23 +53,23 @@ public class DeprecatedRuleDefinitions implements RuleDefinitions { private final RuleRepository[] repositories; private final TechnicalDebtModelRepository languageModelFinder; - private final RulesDebtModelXMLImporter importer; + private final DebtRulesXMLImporter importer; - public DeprecatedRuleDefinitions(RuleI18nManager i18n, RuleRepository[] repositories, TechnicalDebtModelRepository languageModelFinder, RulesDebtModelXMLImporter importer) { + public DeprecatedRuleDefinitions(RuleI18nManager i18n, RuleRepository[] repositories, TechnicalDebtModelRepository languageModelFinder, DebtRulesXMLImporter importer) { this.i18n = i18n; this.repositories = repositories; this.languageModelFinder = languageModelFinder; this.importer = importer; } - public DeprecatedRuleDefinitions(RuleI18nManager i18n, TechnicalDebtModelRepository languageModelFinder, RulesDebtModelXMLImporter importer) { + public DeprecatedRuleDefinitions(RuleI18nManager i18n, TechnicalDebtModelRepository languageModelFinder, DebtRulesXMLImporter importer) { this(i18n, new RuleRepository[0], languageModelFinder, importer); } @Override public void define(Context context) { // Load rule debt definitions from xml files provided by plugin - List ruleDebts = loadRuleDebtList(); + List ruleDebts = loadRuleDebtList(); for (RuleRepository repository : repositories) { // RuleRepository API does not handle difference between new and extended repositories, @@ -101,8 +101,8 @@ public class DeprecatedRuleDefinitions implements RuleDefinitions { } } - private void updateRuleDebtDefinitions(NewRule newRule, String repoKey, String ruleKey, List ruleDebts){ - RulesDebtModelXMLImporter.RuleDebt ruleDebt = findRequirement(ruleDebts, repoKey, ruleKey); + private void updateRuleDebtDefinitions(NewRule newRule, String repoKey, String ruleKey, List ruleDebts){ + DebtRulesXMLImporter.RuleDebt ruleDebt = findRequirement(ruleDebts, repoKey, ruleKey); if (ruleDebt != null) { newRule.setCharacteristicKey(ruleDebt.characteristicKey()); newRule.setRemediationFunction(ruleDebt.function()); @@ -138,15 +138,15 @@ public class DeprecatedRuleDefinitions implements RuleDefinitions { return StringUtils.defaultIfBlank(desc, null); } - public List loadRuleDebtList() { - List ruleDebtList = newArrayList(); + public List loadRuleDebtList() { + List ruleDebtList = newArrayList(); for (String pluginKey : getContributingPluginListWithoutSqale()) { ruleDebtList.addAll(loadRuleDebtsFromXml(pluginKey)); } return ruleDebtList; } - public List loadRuleDebtsFromXml(String pluginKey) { + public List loadRuleDebtsFromXml(String pluginKey) { Reader xmlFileReader = null; try { xmlFileReader = languageModelFinder.createReaderForXMLFile(pluginKey); @@ -163,10 +163,10 @@ public class DeprecatedRuleDefinitions implements RuleDefinitions { } @CheckForNull - private RulesDebtModelXMLImporter.RuleDebt findRequirement(List requirements, final String repoKey, final String ruleKey) { - return Iterables.find(requirements, new Predicate() { + private DebtRulesXMLImporter.RuleDebt findRequirement(List requirements, final String repoKey, final String ruleKey) { + return Iterables.find(requirements, new Predicate() { @Override - public boolean apply(RulesDebtModelXMLImporter.RuleDebt input) { + public boolean apply(DebtRulesXMLImporter.RuleDebt input) { return input.ruleKey().equals(RuleKey.of(repoKey, ruleKey)); } }, null); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistration.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistration.java index afaa1b7d0fe..f3e699dba4e 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistration.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistration.java @@ -42,7 +42,7 @@ import org.sonar.core.rule.*; import org.sonar.core.technicaldebt.db.CharacteristicDao; import org.sonar.core.technicaldebt.db.CharacteristicDto; import org.sonar.server.qualityprofile.ProfilesManager; -import org.sonar.server.startup.RegisterDebtCharacteristicModel; +import org.sonar.server.startup.RegisterDebtModel; import javax.annotation.CheckForNull; @@ -77,7 +77,7 @@ public class RuleRegistration implements Startable { public RuleRegistration(RuleDefinitionsLoader defLoader, ProfilesManager profilesManager, RuleRegistry ruleRegistry, ESRuleTags esRuleTags, RuleTagOperations ruleTagOperations, MyBatis myBatis, RuleDao ruleDao, RuleTagDao ruleTagDao, ActiveRuleDao activeRuleDao, CharacteristicDao characteristicDao, - RegisterDebtCharacteristicModel registerTechnicalDebtModel) { + RegisterDebtModel registerTechnicalDebtModel) { this.defLoader = defLoader; this.profilesManager = profilesManager; this.ruleRegistry = ruleRegistry; diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterDebtCharacteristicModel.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterDebtCharacteristicModel.java deleted file mode 100644 index fcd9f8bd27e..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterDebtCharacteristicModel.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.startup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.utils.TimeProfiler; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.core.technicaldebt.CharacteristicsDebtModelSynchronizer; - -public class RegisterDebtCharacteristicModel { - - private static final Logger LOGGER = LoggerFactory.getLogger(RegisterDebtCharacteristicModel.class); - - private final CharacteristicsDebtModelSynchronizer manager; - - public RegisterDebtCharacteristicModel(CharacteristicsDebtModelSynchronizer manager) { - this.manager = manager; - } - - public void start() { - TimeProfiler profiler = new TimeProfiler(LOGGER).start("Register Debt Characteristics Model"); - manager.synchronize(ValidationMessages.create()); - profiler.stop(); - } - -} diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterDebtModel.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterDebtModel.java new file mode 100644 index 00000000000..7006a1f8926 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterDebtModel.java @@ -0,0 +1,45 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.startup; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.utils.TimeProfiler; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.core.technicaldebt.DebtModelSynchronizer; + +public class RegisterDebtModel { + + private static final Logger LOGGER = LoggerFactory.getLogger(RegisterDebtModel.class); + + private final DebtModelSynchronizer manager; + + public RegisterDebtModel(DebtModelSynchronizer manager) { + this.manager = manager; + } + + public void start() { + TimeProfiler profiler = new TimeProfiler(LOGGER).start("Register technical debt model"); + manager.synchronize(ValidationMessages.create()); + profiler.stop(); + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterTechnicalDebtModel.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterTechnicalDebtModel.java deleted file mode 100644 index d533dd7cf28..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterTechnicalDebtModel.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.startup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.utils.TimeProfiler; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.core.technicaldebt.TechnicalDebtModelSynchronizer; -import org.sonar.core.technicaldebt.TechnicalDebtRuleCache; -import org.sonar.server.rule.RuleRegistration; - -public final class RegisterTechnicalDebtModel { - - private static final Logger LOGGER = LoggerFactory.getLogger(RegisterTechnicalDebtModel.class); - - private final TechnicalDebtModelSynchronizer manager; - private final RuleFinder ruleFinder; - - /** - * @param registerRulesBeforeModels used only to be started after the creation of check templates - */ - public RegisterTechnicalDebtModel(TechnicalDebtModelSynchronizer manager, RuleFinder ruleFinder, RuleRegistration registerRulesBeforeModels) { - this.manager = manager; - this.ruleFinder = ruleFinder; - } - - public void start() { - TimeProfiler profiler = new TimeProfiler(LOGGER).start("Register Technical Debt Model"); - TechnicalDebtRuleCache technicalDebtRuleCache = new TechnicalDebtRuleCache(ruleFinder); - manager.synchronize(ValidationMessages.create(), technicalDebtRuleCache); - profiler.stop(); - } - -} diff --git a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java index a34677da554..d03dcefd2d6 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java @@ -32,7 +32,7 @@ import org.sonar.api.rules.RulePriority; import org.sonar.api.rules.RuleRepository; import org.sonar.api.server.rule.RuleDefinitions; import org.sonar.core.i18n.RuleI18nManager; -import org.sonar.core.technicaldebt.RulesDebtModelXMLImporter; +import org.sonar.core.technicaldebt.DebtRulesXMLImporter; import org.sonar.core.technicaldebt.TechnicalDebtModelRepository; import java.io.Reader; @@ -55,7 +55,7 @@ public class DeprecatedRuleDefinitionsTest { TechnicalDebtModelRepository debtModelRepository; @Mock - RulesDebtModelXMLImporter importer; + DebtRulesXMLImporter importer; static class CheckstyleRules extends RuleRepository { public CheckstyleRules() { @@ -154,8 +154,8 @@ public class DeprecatedRuleDefinitionsTest { public void define_rule_debt() throws Exception { RuleDefinitions.Context context = new RuleDefinitions.Context(); - List ruleDebts = newArrayList( - new RulesDebtModelXMLImporter.RuleDebt() + List ruleDebts = newArrayList( + new DebtRulesXMLImporter.RuleDebt() .setCharacteristicKey("MEMORY_EFFICIENCY") .setRuleKey(RuleKey.of("checkstyle", "ConstantName")) .setFunction(RemediationFunction.LINEAR_OFFSET) diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistrationTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistrationTest.java index 90f691f353f..5c07b56addd 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistrationTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleRegistrationTest.java @@ -34,7 +34,7 @@ import org.sonar.core.rule.RuleDao; import org.sonar.core.rule.RuleTagDao; import org.sonar.core.technicaldebt.db.CharacteristicDao; import org.sonar.server.qualityprofile.ProfilesManager; -import org.sonar.server.startup.RegisterDebtCharacteristicModel; +import org.sonar.server.startup.RegisterDebtModel; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -68,7 +68,7 @@ public class RuleRegistrationTest extends AbstractDaoTestCase { ruleTagOperations = new RuleTagOperations(ruleTagDao, esRuleTags); characteristicDao = new CharacteristicDao(myBatis); task = new RuleRegistration(new RuleDefinitionsLoader(mock(RuleRepositories.class), new RuleDefinitions[]{new FakeRepository()}), - profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtCharacteristicModel.class)); + profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtModel.class)); } @Test @@ -215,7 +215,7 @@ public class RuleRegistrationTest extends AbstractDaoTestCase { @Test public void test_high_number_of_rules() { task = new RuleRegistration(new RuleDefinitionsLoader(mock(RuleRepositories.class), new RuleDefinitions[]{new BigRepository()}), - profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtCharacteristicModel.class)); + profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtModel.class)); setupData("shared"); task.start(); @@ -230,7 +230,7 @@ public class RuleRegistrationTest extends AbstractDaoTestCase { public void insert_extended_repositories() { task = new RuleRegistration(new RuleDefinitionsLoader(mock(RuleRepositories.class), new RuleDefinitions[]{ new FindbugsRepository(), new FbContribRepository()}), - profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtCharacteristicModel.class)); + profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtModel.class)); setupData("empty"); task.start(); diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterDebtCharacteristicModelTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterDebtCharacteristicModelTest.java deleted file mode 100644 index 52607362d49..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/startup/RegisterDebtCharacteristicModelTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.startup; - -import org.junit.Test; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.core.technicaldebt.CharacteristicsDebtModelSynchronizer; - -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; - -public class RegisterDebtCharacteristicModelTest { - - @Test - public void create_model() throws Exception { - CharacteristicsDebtModelSynchronizer synchronizer = mock(CharacteristicsDebtModelSynchronizer.class); - RegisterDebtCharacteristicModel sqaleDefinition = new RegisterDebtCharacteristicModel(synchronizer); - - sqaleDefinition.start(); - - verify(synchronizer, times(1)).synchronize(any(ValidationMessages.class)); - } -} diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterDebtModelTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterDebtModelTest.java new file mode 100644 index 00000000000..be235f4a8f6 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/startup/RegisterDebtModelTest.java @@ -0,0 +1,41 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.startup; + +import org.junit.Test; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.core.technicaldebt.DebtModelSynchronizer; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class RegisterDebtModelTest { + + @Test + public void create_model() throws Exception { + DebtModelSynchronizer synchronizer = mock(DebtModelSynchronizer.class); + RegisterDebtModel sqaleDefinition = new RegisterDebtModel(synchronizer); + + sqaleDefinition.start(); + + verify(synchronizer, times(1)).synchronize(any(ValidationMessages.class)); + } +} diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterTechnicalDebtModelTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterTechnicalDebtModelTest.java deleted file mode 100644 index 21015edad8d..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/startup/RegisterTechnicalDebtModelTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.startup; - -import org.junit.Test; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.core.technicaldebt.TechnicalDebtModelSynchronizer; -import org.sonar.core.technicaldebt.TechnicalDebtRuleCache; - -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; - -public class RegisterTechnicalDebtModelTest { - - @Test - public void create_model() throws Exception { - TechnicalDebtModelSynchronizer manger = mock(TechnicalDebtModelSynchronizer.class); - RuleFinder ruleFinder = mock(RuleFinder.class); - RegisterTechnicalDebtModel sqaleDefinition = new RegisterTechnicalDebtModel(manger, ruleFinder, null); - - sqaleDefinition.start(); - - verify(manger, times(1)).synchronize(any(ValidationMessages.class), any(TechnicalDebtRuleCache.class)); - } -}