]> source.dussan.org Git - sonarqube.git/blob
ccb2584b7d473c834e4a44f918b15be60ae8308f
[sonarqube.git] /
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.source.index;
21
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;
28
29 import java.sql.Connection;
30 import java.sql.PreparedStatement;
31
32 import static org.fest.assertions.Assertions.assertThat;
33 import static org.fest.assertions.Fail.fail;
34
35 public class SourceLineResultSetIteratorTest {
36
37   @ClassRule
38   public static TestDatabase db = new TestDatabase().schema(SourceLineResultSetIteratorTest.class, "schema.sql");
39
40   DbClient dbClient;
41
42   Connection connection;
43
44   @Before
45   public void setUp() throws Exception {
46     dbClient = new DbClient(db.database(), db.myBatis());
47     connection = db.openConnection();
48   }
49
50   @After
51   public void after() throws Exception {
52     connection.close();
53   }
54
55   @Test
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");
63     stmt.executeUpdate();
64
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);
89     iterator.close();
90   }
91
92   @Test
93   public void should_ignore_lines_already_handled() throws Exception {
94     db.prepareDbUnit(getClass(), "shared.xml");
95
96     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 2000000000000L);
97     assertThat(iterator.hasNext()).isFalse();
98     iterator.close();
99   }
100
101   @Test
102   public void parse_empty_file() throws Exception {
103     db.prepareDbUnit(getClass(), "empty-file.xml");
104
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();
110     iterator.close();
111   }
112
113   @Test
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();
119     stmt.close();
120
121     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
122     try {
123       assertThat(iterator.hasNext()).isTrue();
124       iterator.next();
125       fail();
126     } catch (IllegalStateException e) {
127       // ok
128     }
129     iterator.close();
130   }
131 }