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.

TokenizerBridgeTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.duplications.internal.pmd;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.InputStreamReader;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.List;
  25. import net.sourceforge.pmd.cpd.SourceCode;
  26. import net.sourceforge.pmd.cpd.TokenEntry;
  27. import net.sourceforge.pmd.cpd.Tokenizer;
  28. import net.sourceforge.pmd.cpd.Tokens;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. import org.sonar.api.batch.sensor.cpd.internal.TokensLine;
  32. import static org.hamcrest.CoreMatchers.is;
  33. import static org.junit.Assert.assertThat;
  34. public class TokenizerBridgeTest {
  35. private TokenizerBridge bridge;
  36. @Before
  37. public void setUp() {
  38. Tokenizer tokenizer = new Tokenizer() {
  39. public void tokenize(SourceCode tokens, Tokens tokenEntries) {
  40. tokenEntries.add(new TokenEntry("t1", "src", 1));
  41. tokenEntries.add(new TokenEntry("t2", "src", 1));
  42. tokenEntries.add(new TokenEntry("t3", "src", 2));
  43. tokenEntries.add(new TokenEntry("t1", "src", 4));
  44. tokenEntries.add(new TokenEntry("t3", "src", 4));
  45. tokenEntries.add(new TokenEntry("t3", "src", 4));
  46. tokenEntries.add(TokenEntry.getEOF());
  47. }
  48. };
  49. bridge = new TokenizerBridge(tokenizer, 10);
  50. }
  51. @Test
  52. public void shouldClearCacheInTokenEntry() {
  53. bridge.chunk("file.txt", new InputStreamReader(new ByteArrayInputStream(new byte[0]), StandardCharsets.UTF_8));
  54. TokenEntry token = new TokenEntry("image", "srcId", 0);
  55. assertThat(token.getIndex(), is(0));
  56. assertThat(token.getIdentifier(), is(1));
  57. }
  58. @Test
  59. public void test() {
  60. // To be sure that token index will be relative to file - run twice:
  61. bridge.chunk("file.txt", new InputStreamReader(new ByteArrayInputStream(new byte[0]), StandardCharsets.UTF_8));
  62. List<TokensLine> lines = bridge.chunk("file.txt", new InputStreamReader(new ByteArrayInputStream(new byte[0]), StandardCharsets.UTF_8));
  63. assertThat(lines.size(), is(3));
  64. TokensLine line = lines.get(0);
  65. // 2 tokens on 1 line
  66. assertThat(line.getStartUnit(), is(1));
  67. assertThat(line.getEndUnit(), is(2));
  68. assertThat(line.getStartLine(), is(1));
  69. assertThat(line.getEndLine(), is(1));
  70. assertThat(line.getHashCode(), is("t1t2".hashCode()));
  71. line = lines.get(1);
  72. // 1 token on 2 line
  73. assertThat(line.getStartUnit(), is(3));
  74. assertThat(line.getEndUnit(), is(3));
  75. assertThat(line.getStartLine(), is(2));
  76. assertThat(line.getEndLine(), is(2));
  77. assertThat(line.getHashCode(), is("t3".hashCode()));
  78. line = lines.get(2);
  79. // 3 tokens on 4 line
  80. assertThat(line.getStartUnit(), is(4));
  81. assertThat(line.getEndUnit(), is(6));
  82. assertThat(line.getStartLine(), is(4));
  83. assertThat(line.getEndLine(), is(4));
  84. assertThat(line.getHashCode(), is("t1t3t3".hashCode()));
  85. }
  86. }