You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DebtModelPluginRepositoryTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.debt;
  21. import com.google.common.collect.Lists;
  22. import com.google.common.collect.Maps;
  23. import com.google.common.io.Resources;
  24. import org.apache.commons.io.IOUtils;
  25. import org.junit.Test;
  26. import org.mockito.invocation.InvocationOnMock;
  27. import org.mockito.stubbing.Answer;
  28. import org.sonar.api.SonarPlugin;
  29. import org.sonar.core.platform.PluginInfo;
  30. import org.sonar.core.platform.PluginRepository;
  31. import java.io.FileInputStream;
  32. import java.io.InputStream;
  33. import java.io.Reader;
  34. import java.util.Collection;
  35. import java.util.List;
  36. import java.util.Map;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.junit.Assert.assertNotNull;
  39. import static org.junit.Assert.fail;
  40. import static org.mockito.Matchers.anyString;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.when;
  43. public class DebtModelPluginRepositoryTest {
  44. private static final String TEST_XML_PREFIX_PATH = "org/sonar/server/debt/DebtModelPluginRepositoryTest/";
  45. private DebtModelPluginRepository modelFinder;
  46. @Test
  47. public void test_component_initialization() throws Exception {
  48. // we do have the "csharp-model.xml" file in src/test/resources
  49. PluginInfo csharpPluginMetadata = new PluginInfo("csharp");
  50. // but we don' have the "php-model.xml" one
  51. PluginInfo phpPluginMetadata = new PluginInfo("php");
  52. PluginRepository repository = mock(PluginRepository.class);
  53. when(repository.getPluginInfos()).thenReturn(Lists.newArrayList(csharpPluginMetadata, phpPluginMetadata));
  54. FakePlugin fakePlugin = new FakePlugin();
  55. when(repository.getPluginInstance(anyString())).thenReturn(fakePlugin);
  56. modelFinder = new DebtModelPluginRepository(repository, TEST_XML_PREFIX_PATH);
  57. // when
  58. modelFinder.start();
  59. // assert
  60. Collection<String> contributingPluginList = modelFinder.getContributingPluginList();
  61. assertThat(contributingPluginList.size()).isEqualTo(2);
  62. assertThat(contributingPluginList).containsOnly("technical-debt", "csharp");
  63. }
  64. @Test
  65. public void contributing_plugin_list() {
  66. initModel();
  67. Collection<String> contributingPluginList = modelFinder.getContributingPluginList();
  68. assertThat(contributingPluginList.size()).isEqualTo(2);
  69. assertThat(contributingPluginList).contains("csharp", "java");
  70. }
  71. @Test
  72. public void get_content_for_xml_file() {
  73. initModel();
  74. Reader xmlFileReader = null;
  75. try {
  76. xmlFileReader = modelFinder.createReaderForXMLFile("csharp");
  77. assertNotNull(xmlFileReader);
  78. List<String> lines = IOUtils.readLines(xmlFileReader);
  79. assertThat(lines.size()).isEqualTo(25);
  80. assertThat(lines.get(0)).isEqualTo("<sqale>");
  81. } catch (Exception e) {
  82. fail("Should be able to read the XML file.");
  83. } finally {
  84. IOUtils.closeQuietly(xmlFileReader);
  85. }
  86. }
  87. @Test
  88. public void return_xml_file_path_for_plugin() {
  89. initModel();
  90. assertThat(modelFinder.getXMLFilePath("foo")).isEqualTo(TEST_XML_PREFIX_PATH + "foo-model.xml");
  91. }
  92. @Test
  93. public void contain_default_model() {
  94. modelFinder = new DebtModelPluginRepository(mock(PluginRepository.class));
  95. modelFinder.start();
  96. assertThat(modelFinder.getContributingPluginKeyToClassLoader().keySet()).containsOnly("technical-debt");
  97. }
  98. private void initModel() {
  99. Map<String, ClassLoader> contributingPluginKeyToClassLoader = Maps.newHashMap();
  100. contributingPluginKeyToClassLoader.put("csharp", newClassLoader());
  101. contributingPluginKeyToClassLoader.put("java", newClassLoader());
  102. modelFinder = new DebtModelPluginRepository(contributingPluginKeyToClassLoader, TEST_XML_PREFIX_PATH);
  103. }
  104. private ClassLoader newClassLoader() {
  105. ClassLoader loader = mock(ClassLoader.class);
  106. when(loader.getResourceAsStream(anyString())).thenAnswer(new Answer<InputStream>() {
  107. public InputStream answer(InvocationOnMock invocation) throws Throwable {
  108. return new FileInputStream(Resources.getResource((String) invocation.getArguments()[0]).getPath());
  109. }
  110. });
  111. return loader;
  112. }
  113. class FakePlugin extends SonarPlugin {
  114. public List getExtensions() {
  115. return null;
  116. }
  117. }
  118. }