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.

JavaCpdBlockIndexerSensorTest.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.scanner.cpd;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.List;
  25. import org.apache.commons.io.FileUtils;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.mockito.ArgumentCaptor;
  31. import org.mockito.Mock;
  32. import org.mockito.MockitoAnnotations;
  33. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  34. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  35. import org.sonar.api.batch.sensor.internal.SensorContextTester;
  36. import org.sonar.duplications.block.Block;
  37. import org.sonar.scanner.cpd.index.SonarCpdBlockIndex;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.mockito.ArgumentMatchers.eq;
  40. import static org.mockito.Mockito.verify;
  41. import static org.mockito.Mockito.verifyZeroInteractions;
  42. public class JavaCpdBlockIndexerSensorTest {
  43. @Rule
  44. public TemporaryFolder temp = new TemporaryFolder();
  45. private SensorContextTester context;
  46. @Mock
  47. private SonarCpdBlockIndex index;
  48. private ArgumentCaptor<List<Block>> blockCaptor = ArgumentCaptor.forClass(List.class);
  49. private DefaultInputFile file;
  50. @Before
  51. public void prepare() throws IOException {
  52. MockitoAnnotations.initMocks(this);
  53. File baseDir = temp.newFolder();
  54. context = SensorContextTester.create(baseDir);
  55. file = new TestInputFileBuilder("foo", "src/ManyStatements.java")
  56. .setModuleBaseDir(baseDir.toPath())
  57. .setCharset(StandardCharsets.UTF_8)
  58. .setLanguage("java").build();
  59. context.fileSystem().add(file);
  60. File ioFile = file.file();
  61. FileUtils.copyURLToFile(this.getClass().getResource("ManyStatements.java"), ioFile);
  62. }
  63. @Test
  64. public void testExclusions() {
  65. file.setExcludedForDuplication(true);
  66. new JavaCpdBlockIndexerSensor(index).execute(context);
  67. verifyZeroInteractions(index);
  68. }
  69. @Test
  70. public void testJavaIndexing() {
  71. new JavaCpdBlockIndexerSensor(index).execute(context);
  72. verify(index).insert(eq(file), blockCaptor.capture());
  73. List<Block> blockList = blockCaptor.getValue();
  74. assertThat(blockList).hasSize(26);
  75. }
  76. }