2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.source.index;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.ClassRule;
25 import org.junit.Test;
26 import org.sonar.core.persistence.TestDatabase;
27 import org.sonar.server.db.DbClient;
29 import java.sql.Connection;
30 import java.sql.PreparedStatement;
32 import static org.fest.assertions.Assertions.assertThat;
33 import static org.fest.assertions.Fail.fail;
35 public class SourceLineResultSetIteratorTest {
38 public static TestDatabase db = new TestDatabase().schema(SourceLineResultSetIteratorTest.class, "schema.sql");
42 Connection connection;
45 public void setUp() throws Exception {
46 dbClient = new DbClient(db.database(), db.myBatis());
47 connection = db.openConnection();
51 public void after() throws Exception {
56 public void should_generate_source_line_documents() throws Exception {
57 db.prepareDbUnit(getClass(), "shared.xml");
58 PreparedStatement stmt = connection.prepareStatement("UPDATE file_sources SET data = ? WHERE id=1");
59 stmt.setString(1, "aef12a,alice,2014-04-25T12:34:56+0100,1,0,0,2,0,0,3,0,0,polop,palap,,class Foo {\r\n" +
60 "abe465,bob,2014-07-25T12:34:56+0100,,,,,,,,,,,,, // Empty\r\n" +
61 "afb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,,}\r\n" +
62 "afb789,carol,2014-03-23T12:34:56+0100,,,,,,,,,,,,,\r\n");
65 SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
66 assertThat(iterator.hasNext()).isTrue();
67 SourceLineResultSetIterator.SourceFile file = iterator.next();
68 assertThat(file.getLines()).hasSize(4);
69 SourceLineDoc firstLine = file.getLines().get(0);
70 assertThat(firstLine.projectUuid()).isEqualTo("uuid-MyProject");
71 assertThat(firstLine.fileUuid()).isEqualTo("uuid-MyFile.xoo");
72 assertThat(firstLine.line()).isEqualTo(1);
73 assertThat(firstLine.scmRevision()).isEqualTo("aef12a");
74 assertThat(firstLine.scmAuthor()).isEqualTo("alice");
75 // TODO Sanitize usage of fscking dates
76 // assertThat(firstLine.scmDate()).isEqualTo(DateUtils.parseDateTime("2014-04-25T12:34:56+0100"));
77 assertThat(firstLine.highlighting()).isEqualTo("polop");
78 assertThat(firstLine.symbols()).isEqualTo("palap");
79 assertThat(firstLine.source()).isEqualTo("class Foo {");
80 assertThat(firstLine.utLineHits()).isEqualTo(1);
81 assertThat(firstLine.utConditions()).isEqualTo(0);
82 assertThat(firstLine.utCoveredConditions()).isEqualTo(0);
83 assertThat(firstLine.itLineHits()).isEqualTo(2);
84 assertThat(firstLine.itConditions()).isEqualTo(0);
85 assertThat(firstLine.itCoveredConditions()).isEqualTo(0);
86 assertThat(firstLine.overallLineHits()).isEqualTo(3);
87 assertThat(firstLine.overallConditions()).isEqualTo(0);
88 assertThat(firstLine.overallCoveredConditions()).isEqualTo(0);
93 public void should_ignore_lines_already_handled() throws Exception {
94 db.prepareDbUnit(getClass(), "shared.xml");
96 SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 2000000000000L);
97 assertThat(iterator.hasNext()).isFalse();
102 public void parse_empty_file() throws Exception {
103 db.prepareDbUnit(getClass(), "empty-file.xml");
105 SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
106 assertThat(iterator.hasNext()).isTrue();
107 SourceLineResultSetIterator.SourceFile file = iterator.next();
108 assertThat(file.getFileUuid()).isEqualTo("uuid-MyFile.xoo");
109 assertThat(file.getLines()).isEmpty();
114 public void should_fail_on_bad_csv() throws Exception {
115 db.prepareDbUnit(getClass(), "shared.xml");
116 PreparedStatement stmt = connection.prepareStatement("UPDATE file_sources SET data = ? WHERE id=1");
117 stmt.setString(1, "plouf");
118 stmt.executeUpdate();
121 SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
123 assertThat(iterator.hasNext()).isTrue();
126 } catch (IllegalStateException e) {