private final MyBatis mybatis;
private final CharacteristicDao dao;
- private final DebtModelPluginRepository languageModelFinder;
+ private final TechnicalDebtModelRepository languageModelFinder;
private final CharacteristicsDebtModelXMLImporter importer;
- public CharacteristicsDebtModelSynchronizer(MyBatis mybatis, CharacteristicDao dao, DebtModelPluginRepository modelRepository, CharacteristicsDebtModelXMLImporter importer) {
+ public CharacteristicsDebtModelSynchronizer(MyBatis mybatis, CharacteristicDao dao, TechnicalDebtModelRepository modelRepository, CharacteristicsDebtModelXMLImporter importer) {
this.mybatis = mybatis;
this.dao = dao;
this.languageModelFinder = modelRepository;
}
public List<CharacteristicDto> synchronize(ValidationMessages messages, SqlSession session) {
- DefaultTechnicalDebtModel defaultModel = loadModelFromXml(DebtModelPluginRepository.DEFAULT_MODEL, messages);
+ DefaultTechnicalDebtModel defaultModel = loadModelFromXml(TechnicalDebtModelRepository.DEFAULT_MODEL, messages);
List<CharacteristicDto> model = loadOrCreateModelFromDb(defaultModel, session);
messages.log(LOG);
return model;
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 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.annotations.VisibleForTesting;
-import com.google.common.collect.Maps;
-import org.picocontainer.Startable;
-import org.sonar.api.ServerExtension;
-import org.sonar.api.platform.PluginMetadata;
-import org.sonar.api.platform.PluginRepository;
-
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-
-import static com.google.common.collect.Lists.newArrayList;
-
-/**
- * <p>This class is used to find which technical debt model XML files exist in the Sonar instance.</p>
- * <p>
- * Those XML files are provided by language plugins that embed their own contribution to the definition of the Technical debt model.
- * They must be located in the classpath of those language plugins, more specifically in the "com.sonar.sqale" package, and
- * they must be named "<pluginKey>-model.xml".
- * </p>
- */
-public class DebtModelPluginRepository implements ServerExtension, Startable {
-
- public static final String DEFAULT_MODEL = "technical-debt";
-
- private static final String XML_FILE_SUFFIX = "-model.xml";
- private static final String XML_FILE_PREFIX = "com/sonar/sqale/";
-
- private String xmlFilePrefix;
-
- private PluginRepository pluginRepository;
- private Map<String, ClassLoader> contributingPluginKeyToClassLoader;
-
- public DebtModelPluginRepository(PluginRepository pluginRepository) {
- this.pluginRepository = pluginRepository;
- this.xmlFilePrefix = XML_FILE_PREFIX;
- }
-
- @VisibleForTesting
- DebtModelPluginRepository(PluginRepository pluginRepository, String xmlFilePrefix) {
- this.pluginRepository = pluginRepository;
- this.xmlFilePrefix = xmlFilePrefix;
- }
-
- @VisibleForTesting
- DebtModelPluginRepository(Map<String, ClassLoader> contributingPluginKeyToClassLoader, String xmlFilePrefix) {
- this.contributingPluginKeyToClassLoader = contributingPluginKeyToClassLoader;
- this.xmlFilePrefix = xmlFilePrefix;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void start() {
- findAvailableXMLFiles();
- }
-
- protected void findAvailableXMLFiles() {
- if (contributingPluginKeyToClassLoader == null) {
- contributingPluginKeyToClassLoader = Maps.newTreeMap();
- // Add default model
- contributingPluginKeyToClassLoader.put(DEFAULT_MODEL, getClass().getClassLoader());
- for (PluginMetadata metadata : pluginRepository.getMetadata()) {
- String pluginKey = metadata.getKey();
- ClassLoader classLoader = pluginRepository.getPlugin(pluginKey).getClass().getClassLoader();
- if (classLoader.getResource(getXMLFilePath(pluginKey)) != null) {
- contributingPluginKeyToClassLoader.put(pluginKey, classLoader);
- }
- }
- }
- contributingPluginKeyToClassLoader = Collections.unmodifiableMap(contributingPluginKeyToClassLoader);
- }
-
- @VisibleForTesting
- String getXMLFilePath(String model) {
- return xmlFilePrefix + model + XML_FILE_SUFFIX;
- }
-
- /**
- * Returns the list of plugins that can contribute to the technical debt model.
- *
- * @return the list of plugin keys
- */
- public Collection<String> getContributingPluginList() {
- return newArrayList(contributingPluginKeyToClassLoader.keySet());
- }
-
- /**
- * Creates a new {@link java.io.Reader} for the XML file that contains the model contributed by the given plugin.
- *
- * @param pluginKey the key of the plugin that contributes the XML file
- * @return the reader, that must be closed once its use is finished.
- */
- public Reader createReaderForXMLFile(String pluginKey) {
- ClassLoader classLoader = contributingPluginKeyToClassLoader.get(pluginKey);
- String xmlFilePath = getXMLFilePath(pluginKey);
- return new InputStreamReader(classLoader.getResourceAsStream(xmlFilePath));
- }
-
- @VisibleForTesting
- Map<String, ClassLoader> getContributingPluginKeyToClassLoader(){
- return contributingPluginKeyToClassLoader;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void stop() {
- // Nothing to do
- }
-
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 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.annotations.VisibleForTesting;
+import com.google.common.collect.Maps;
+import org.picocontainer.Startable;
+import org.sonar.api.ServerExtension;
+import org.sonar.api.platform.PluginMetadata;
+import org.sonar.api.platform.PluginRepository;
+
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+/**
+ * <p>This class is used to find which technical debt model XML files exist in the Sonar instance.</p>
+ * <p>
+ * Those XML files are provided by language plugins that embed their own contribution to the definition of the Technical debt model.
+ * They must be located in the classpath of those language plugins, more specifically in the "com.sonar.sqale" package, and
+ * they must be named "<pluginKey>-model.xml".
+ * </p>
+ */
+public class TechnicalDebtModelRepository implements ServerExtension, Startable {
+
+ public static final String DEFAULT_MODEL = "technical-debt";
+
+ private static final String XML_FILE_SUFFIX = "-model.xml";
+ private static final String XML_FILE_PREFIX = "com/sonar/sqale/";
+
+ private String xmlFilePrefix;
+
+ private PluginRepository pluginRepository;
+ private Map<String, ClassLoader> contributingPluginKeyToClassLoader;
+
+ public TechnicalDebtModelRepository(PluginRepository pluginRepository) {
+ this.pluginRepository = pluginRepository;
+ this.xmlFilePrefix = XML_FILE_PREFIX;
+ }
+
+ @VisibleForTesting
+ TechnicalDebtModelRepository(PluginRepository pluginRepository, String xmlFilePrefix) {
+ this.pluginRepository = pluginRepository;
+ this.xmlFilePrefix = xmlFilePrefix;
+ }
+
+ @VisibleForTesting
+ TechnicalDebtModelRepository(Map<String, ClassLoader> contributingPluginKeyToClassLoader, String xmlFilePrefix) {
+ this.contributingPluginKeyToClassLoader = contributingPluginKeyToClassLoader;
+ this.xmlFilePrefix = xmlFilePrefix;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void start() {
+ findAvailableXMLFiles();
+ }
+
+ protected void findAvailableXMLFiles() {
+ if (contributingPluginKeyToClassLoader == null) {
+ contributingPluginKeyToClassLoader = Maps.newTreeMap();
+ // Add default model
+ contributingPluginKeyToClassLoader.put(DEFAULT_MODEL, getClass().getClassLoader());
+ for (PluginMetadata metadata : pluginRepository.getMetadata()) {
+ String pluginKey = metadata.getKey();
+ ClassLoader classLoader = pluginRepository.getPlugin(pluginKey).getClass().getClassLoader();
+ if (classLoader.getResource(getXMLFilePath(pluginKey)) != null) {
+ contributingPluginKeyToClassLoader.put(pluginKey, classLoader);
+ }
+ }
+ }
+ contributingPluginKeyToClassLoader = Collections.unmodifiableMap(contributingPluginKeyToClassLoader);
+ }
+
+ @VisibleForTesting
+ String getXMLFilePath(String model) {
+ return xmlFilePrefix + model + XML_FILE_SUFFIX;
+ }
+
+ /**
+ * Returns the list of plugins that can contribute to the technical debt model.
+ *
+ * @return the list of plugin keys
+ */
+ public Collection<String> getContributingPluginList() {
+ return newArrayList(contributingPluginKeyToClassLoader.keySet());
+ }
+
+ /**
+ * Creates a new {@link java.io.Reader} for the XML file that contains the model contributed by the given plugin.
+ *
+ * @param pluginKey the key of the plugin that contributes the XML file
+ * @return the reader, that must be closed once its use is finished.
+ */
+ public Reader createReaderForXMLFile(String pluginKey) {
+ ClassLoader classLoader = contributingPluginKeyToClassLoader.get(pluginKey);
+ String xmlFilePath = getXMLFilePath(pluginKey);
+ return new InputStreamReader(classLoader.getResourceAsStream(xmlFilePath));
+ }
+
+ @VisibleForTesting
+ Map<String, ClassLoader> getContributingPluginKeyToClassLoader(){
+ return contributingPluginKeyToClassLoader;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void stop() {
+ // Nothing to do
+ }
+
+}
private final MyBatis mybatis;
private final CharacteristicDao dao;
- private final DebtModelPluginRepository languageModelFinder;
+ private final TechnicalDebtModelRepository languageModelFinder;
private final TechnicalDebtXMLImporter importer;
public TechnicalDebtModelSynchronizer(MyBatis mybatis, CharacteristicDao dao,
- DebtModelPluginRepository modelRepository, TechnicalDebtXMLImporter importer) {
+ TechnicalDebtModelRepository modelRepository, TechnicalDebtXMLImporter importer) {
this.mybatis = mybatis;
this.dao = dao;
this.languageModelFinder = modelRepository;
}
public List<CharacteristicDto> synchronize(ValidationMessages messages, TechnicalDebtRuleCache rulesCache, SqlSession session) {
- DefaultTechnicalDebtModel defaultModel = loadModelFromXml(DebtModelPluginRepository.DEFAULT_MODEL, messages, rulesCache);
+ DefaultTechnicalDebtModel defaultModel = loadModelFromXml(TechnicalDebtModelRepository.DEFAULT_MODEL, messages, rulesCache);
List<CharacteristicDto> model = loadOrCreateModelFromDb(defaultModel, session);
disableRequirementsOnRemovedRules(model, rulesCache, session);
mergePlugins(model, defaultModel, messages, rulesCache, session);
private Collection<String> getContributingPluginListWithoutSqale() {
Collection<String> pluginList = newArrayList(languageModelFinder.getContributingPluginList());
- pluginList.remove(DebtModelPluginRepository.DEFAULT_MODEL);
+ pluginList.remove(TechnicalDebtModelRepository.DEFAULT_MODEL);
return pluginList;
}
SqlSession session;
@Mock
- DebtModelPluginRepository debtModelPluginRepository;
+ TechnicalDebtModelRepository technicalDebtModelRepository;
@Mock
CharacteristicDao dao;
defaultModel = new DefaultTechnicalDebtModel();
Reader defaultModelReader = mock(Reader.class);
- when(debtModelPluginRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
+ when(technicalDebtModelRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
when(xmlImporter.importXML(eq(defaultModelReader), any(ValidationMessages.class))).thenReturn(defaultModel);
doAnswer(new Answer() {
}).when(dao).insert(any(CharacteristicDto.class), any(SqlSession.class));
- manager = new CharacteristicsDebtModelSynchronizer(myBatis, dao, debtModelPluginRepository, xmlImporter);
+ manager = new CharacteristicsDebtModelSynchronizer(myBatis, dao, technicalDebtModelRepository, xmlImporter);
}
@Test
new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic);
defaultModel.addRootCharacteristic(rootCharacteristic);
- when(debtModelPluginRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
+ when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
when(dao.selectEnabledCharacteristics()).thenReturn(Lists.<CharacteristicDto>newArrayList());
manager.synchronize(ValidationMessages.create());
new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic);
defaultModel.addRootCharacteristic(rootCharacteristic);
- when(debtModelPluginRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
+ when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
when(dao.selectEnabledCharacteristics()).thenReturn(Lists.newArrayList(new CharacteristicDto()));
manager.synchronize(ValidationMessages.create());
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 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 com.google.common.collect.Maps;
-import com.google.common.io.Resources;
-import org.apache.commons.io.IOUtils;
-import org.junit.Test;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-import org.sonar.api.SonarPlugin;
-import org.sonar.api.platform.PluginMetadata;
-import org.sonar.api.platform.PluginRepository;
-
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.io.Reader;
-import java.net.MalformedURLException;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class DebtModelPluginRepositoryTest {
-
- private static final String TEST_XML_PREFIX_PATH = "org/sonar/core/technicaldebt/TechnicalDebtModelRepositoryTest/";
-
- private DebtModelPluginRepository modelFinder;
-
- @Test
- public void test_component_initialization() throws Exception {
- // we do have the "csharp-model.xml" file in src/test/resources
- PluginMetadata csharpPluginMetadata = mock(PluginMetadata.class);
- when(csharpPluginMetadata.getKey()).thenReturn("csharp");
-
- // but we don' have the "php-model.xml" one
- PluginMetadata phpPluginMetadata = mock(PluginMetadata.class);
- when(phpPluginMetadata.getKey()).thenReturn("php");
-
- PluginRepository repository = mock(PluginRepository.class);
- when(repository.getMetadata()).thenReturn(Lists.newArrayList(csharpPluginMetadata, phpPluginMetadata));
- FakePlugin fakePlugin = new FakePlugin();
- when(repository.getPlugin(anyString())).thenReturn(fakePlugin);
- modelFinder = new DebtModelPluginRepository(repository, TEST_XML_PREFIX_PATH);
-
- // when
- modelFinder.start();
-
- // assert
- Collection<String> contributingPluginList = modelFinder.getContributingPluginList();
- assertThat(contributingPluginList.size()).isEqualTo(2);
- assertThat(contributingPluginList).containsOnly("technical-debt", "csharp");
- }
-
- @Test
- public void contributing_plugin_list() throws Exception {
- initModel();
- Collection<String> contributingPluginList = modelFinder.getContributingPluginList();
- assertThat(contributingPluginList.size()).isEqualTo(2);
- assertThat(contributingPluginList).contains("csharp", "java");
- }
-
- @Test
- public void get_content_for_xml_file() throws Exception {
- initModel();
- Reader xmlFileReader = null;
- try {
- xmlFileReader = modelFinder.createReaderForXMLFile("csharp");
- assertNotNull(xmlFileReader);
- List<String> lines = IOUtils.readLines(xmlFileReader);
- assertThat(lines.size()).isEqualTo(25);
- assertThat(lines.get(0)).isEqualTo("<sqale>");
- } catch (Exception e) {
- fail("Should be able to read the XML file.");
- } finally {
- IOUtils.closeQuietly(xmlFileReader);
- }
- }
-
- @Test
- public void return_xml_file_path_for_plugin() throws Exception {
- initModel();
- assertThat(modelFinder.getXMLFilePath("foo")).isEqualTo(TEST_XML_PREFIX_PATH + "foo-model.xml");
- }
-
- @Test
- public void contain_default_model() throws Exception {
- modelFinder = new DebtModelPluginRepository(mock(PluginRepository.class));
- modelFinder.start();
- assertThat(modelFinder.getContributingPluginKeyToClassLoader().keySet()).containsOnly("technical-debt");
- }
-
- private void initModel() throws MalformedURLException {
- Map<String, ClassLoader> contributingPluginKeyToClassLoader = Maps.newHashMap();
- contributingPluginKeyToClassLoader.put("csharp", newClassLoader());
- contributingPluginKeyToClassLoader.put("java", newClassLoader());
- modelFinder = new DebtModelPluginRepository(contributingPluginKeyToClassLoader, TEST_XML_PREFIX_PATH);
- }
-
- private ClassLoader newClassLoader() throws MalformedURLException {
- ClassLoader loader = mock(ClassLoader.class);
- when(loader.getResourceAsStream(anyString())).thenAnswer(new Answer<InputStream>() {
- public InputStream answer(InvocationOnMock invocation) throws Throwable {
- return new FileInputStream(Resources.getResource((String) invocation.getArguments()[0]).getPath());
- }
- });
- return loader;
- }
-
- class FakePlugin extends SonarPlugin {
- public List getExtensions() {
- return null;
- }
- }
-
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 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 com.google.common.collect.Maps;
+import com.google.common.io.Resources;
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.sonar.api.SonarPlugin;
+import org.sonar.api.platform.PluginMetadata;
+import org.sonar.api.platform.PluginRepository;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class TechnicalDebtModelRepositoryTest {
+
+ private static final String TEST_XML_PREFIX_PATH = "org/sonar/core/technicaldebt/TechnicalDebtModelRepositoryTest/";
+
+ private TechnicalDebtModelRepository modelFinder;
+
+ @Test
+ public void test_component_initialization() throws Exception {
+ // we do have the "csharp-model.xml" file in src/test/resources
+ PluginMetadata csharpPluginMetadata = mock(PluginMetadata.class);
+ when(csharpPluginMetadata.getKey()).thenReturn("csharp");
+
+ // but we don' have the "php-model.xml" one
+ PluginMetadata phpPluginMetadata = mock(PluginMetadata.class);
+ when(phpPluginMetadata.getKey()).thenReturn("php");
+
+ PluginRepository repository = mock(PluginRepository.class);
+ when(repository.getMetadata()).thenReturn(Lists.newArrayList(csharpPluginMetadata, phpPluginMetadata));
+ FakePlugin fakePlugin = new FakePlugin();
+ when(repository.getPlugin(anyString())).thenReturn(fakePlugin);
+ modelFinder = new TechnicalDebtModelRepository(repository, TEST_XML_PREFIX_PATH);
+
+ // when
+ modelFinder.start();
+
+ // assert
+ Collection<String> contributingPluginList = modelFinder.getContributingPluginList();
+ assertThat(contributingPluginList.size()).isEqualTo(2);
+ assertThat(contributingPluginList).containsOnly("technical-debt", "csharp");
+ }
+
+ @Test
+ public void contributing_plugin_list() throws Exception {
+ initModel();
+ Collection<String> contributingPluginList = modelFinder.getContributingPluginList();
+ assertThat(contributingPluginList.size()).isEqualTo(2);
+ assertThat(contributingPluginList).contains("csharp", "java");
+ }
+
+ @Test
+ public void get_content_for_xml_file() throws Exception {
+ initModel();
+ Reader xmlFileReader = null;
+ try {
+ xmlFileReader = modelFinder.createReaderForXMLFile("csharp");
+ assertNotNull(xmlFileReader);
+ List<String> lines = IOUtils.readLines(xmlFileReader);
+ assertThat(lines.size()).isEqualTo(25);
+ assertThat(lines.get(0)).isEqualTo("<sqale>");
+ } catch (Exception e) {
+ fail("Should be able to read the XML file.");
+ } finally {
+ IOUtils.closeQuietly(xmlFileReader);
+ }
+ }
+
+ @Test
+ public void return_xml_file_path_for_plugin() throws Exception {
+ initModel();
+ assertThat(modelFinder.getXMLFilePath("foo")).isEqualTo(TEST_XML_PREFIX_PATH + "foo-model.xml");
+ }
+
+ @Test
+ public void contain_default_model() throws Exception {
+ modelFinder = new TechnicalDebtModelRepository(mock(PluginRepository.class));
+ modelFinder.start();
+ assertThat(modelFinder.getContributingPluginKeyToClassLoader().keySet()).containsOnly("technical-debt");
+ }
+
+ private void initModel() throws MalformedURLException {
+ Map<String, ClassLoader> contributingPluginKeyToClassLoader = Maps.newHashMap();
+ contributingPluginKeyToClassLoader.put("csharp", newClassLoader());
+ contributingPluginKeyToClassLoader.put("java", newClassLoader());
+ modelFinder = new TechnicalDebtModelRepository(contributingPluginKeyToClassLoader, TEST_XML_PREFIX_PATH);
+ }
+
+ private ClassLoader newClassLoader() throws MalformedURLException {
+ ClassLoader loader = mock(ClassLoader.class);
+ when(loader.getResourceAsStream(anyString())).thenAnswer(new Answer<InputStream>() {
+ public InputStream answer(InvocationOnMock invocation) throws Throwable {
+ return new FileInputStream(Resources.getResource((String) invocation.getArguments()[0]).getPath());
+ }
+ });
+ return loader;
+ }
+
+ class FakePlugin extends SonarPlugin {
+ public List getExtensions() {
+ return null;
+ }
+ }
+
+}
SqlSession session;
@Mock
- DebtModelPluginRepository debtModelPluginRepository;
+ TechnicalDebtModelRepository technicalDebtModelRepository;
@Mock
TechnicalDebtRuleCache ruleCache;
defaultModel = new DefaultTechnicalDebtModel();
Reader defaultModelReader = mock(Reader.class);
- when(debtModelPluginRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
+ when(technicalDebtModelRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
when(xmlImporter.importXML(eq(defaultModelReader), any(ValidationMessages.class), eq(ruleCache))).thenReturn(defaultModel);
doAnswer(new Answer() {
}).when(dao).insert(any(CharacteristicDto.class), any(SqlSession.class));
- manager = new TechnicalDebtModelSynchronizer(myBatis, dao, debtModelPluginRepository, xmlImporter);
+ manager = new TechnicalDebtModelSynchronizer(myBatis, dao, technicalDebtModelRepository, xmlImporter);
}
@Test
new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic);
defaultModel.addRootCharacteristic(rootCharacteristic);
- when(debtModelPluginRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
+ when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
when(dao.selectEnabledCharacteristics()).thenReturn(Lists.<CharacteristicDto>newArrayList());
manager.synchronize(ValidationMessages.create(), ruleCache);
Reader javaModelReader = mock(Reader.class);
when(xmlImporter.importXML(eq(javaModelReader), any(ValidationMessages.class), eq(ruleCache))).thenReturn(javaModel);
- when(debtModelPluginRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
- when(debtModelPluginRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
+ when(technicalDebtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
+ when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
manager.synchronize(ValidationMessages.create(), ruleCache);
.setRootCharacteristic(javaRootCharacteristic);
Reader javaModelReader = mock(Reader.class);
- when(debtModelPluginRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
+ when(technicalDebtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
when(xmlImporter.importXML(eq(javaModelReader), any(ValidationMessages.class), eq(ruleCache))).thenReturn(javaModel);
- when(debtModelPluginRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
+ when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
manager.synchronize(ValidationMessages.create(), ruleCache);
javaModel.addRootCharacteristic(javaRootCharacteristic);
Reader javaModelReader = mock(Reader.class);
- when(debtModelPluginRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
+ when(technicalDebtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
when(xmlImporter.importXML(eq(javaModelReader), any(ValidationMessages.class), eq(ruleCache))).thenReturn(javaModel);
- when(debtModelPluginRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
+ when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
manager.synchronize(ValidationMessages.create(), ruleCache);
fail();
servicesContainer.addSingleton(DebtService.class);
servicesContainer.addSingleton(TechnicalDebtModelSynchronizer.class);
servicesContainer.addSingleton(CharacteristicsDebtModelSynchronizer.class);
- servicesContainer.addSingleton(DebtModelPluginRepository.class);
+ servicesContainer.addSingleton(TechnicalDebtModelRepository.class);
servicesContainer.addSingleton(TechnicalDebtXMLImporter.class);
servicesContainer.addSingleton(RulesDebtModelXMLImporter.class);
servicesContainer.addSingleton(CharacteristicsDebtModelXMLImporter.class);
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.check.Cardinality;
import org.sonar.core.i18n.RuleI18nManager;
-import org.sonar.core.technicaldebt.DebtModelPluginRepository;
import org.sonar.core.technicaldebt.RulesDebtModelXMLImporter;
+import org.sonar.core.technicaldebt.TechnicalDebtModelRepository;
import javax.annotation.CheckForNull;
private final RuleI18nManager i18n;
private final RuleRepository[] repositories;
- private final DebtModelPluginRepository languageModelFinder;
+ private final TechnicalDebtModelRepository languageModelFinder;
private final RulesDebtModelXMLImporter importer;
- public DeprecatedRuleDefinitions(RuleI18nManager i18n, RuleRepository[] repositories, DebtModelPluginRepository languageModelFinder, RulesDebtModelXMLImporter importer) {
+ public DeprecatedRuleDefinitions(RuleI18nManager i18n, RuleRepository[] repositories, TechnicalDebtModelRepository languageModelFinder, RulesDebtModelXMLImporter importer) {
this.i18n = i18n;
this.repositories = repositories;
this.languageModelFinder = languageModelFinder;
this.importer = importer;
}
- public DeprecatedRuleDefinitions(RuleI18nManager i18n, DebtModelPluginRepository languageModelFinder, RulesDebtModelXMLImporter importer) {
+ public DeprecatedRuleDefinitions(RuleI18nManager i18n, TechnicalDebtModelRepository languageModelFinder, RulesDebtModelXMLImporter importer) {
this(i18n, new RuleRepository[0], languageModelFinder, importer);
}
private Collection<String> getContributingPluginListWithoutSqale() {
Collection<String> pluginList = newArrayList(languageModelFinder.getContributingPluginList());
- pluginList.remove(DebtModelPluginRepository.DEFAULT_MODEL);
+ pluginList.remove(TechnicalDebtModelRepository.DEFAULT_MODEL);
return pluginList;
}
import org.sonar.api.rules.RuleRepository;
import org.sonar.api.server.rule.RuleDefinitions;
import org.sonar.core.i18n.RuleI18nManager;
-import org.sonar.core.technicaldebt.DebtModelPluginRepository;
import org.sonar.core.technicaldebt.RulesDebtModelXMLImporter;
+import org.sonar.core.technicaldebt.TechnicalDebtModelRepository;
import java.io.Reader;
import java.util.Arrays;
RuleI18nManager i18n;
@Mock
- DebtModelPluginRepository debtModelRepository;
+ TechnicalDebtModelRepository debtModelRepository;
@Mock
RulesDebtModelXMLImporter importer;