]> source.dussan.org Git - sonarqube.git/blob
aaa10a7ae739c5a4336bb60298d103242f5dce99
[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.junit.experimental.categories.Category;
27 import org.sonar.core.persistence.DbTester;
28 import org.sonar.server.db.DbClient;
29 import org.sonar.server.source.db.FileSourceTesting;
30 import org.sonar.test.DbTests;
31
32 import java.sql.Connection;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.junit.Assert.fail;
36
37 @Category(DbTests.class)
38 public class SourceLineResultSetIteratorTest {
39
40   @ClassRule
41   public static DbTester db = new DbTester().schema(SourceLineResultSetIteratorTest.class, "schema.sql");
42
43   DbClient dbClient;
44
45   Connection connection;
46
47   @Before
48   public void setUp() throws Exception {
49     dbClient = new DbClient(db.database(), db.myBatis());
50     connection = db.openConnection();
51   }
52
53   @After
54   public void after() throws Exception {
55     connection.close();
56   }
57
58   @Test
59   public void parse_db_and_generate_source_line_documents() throws Exception {
60     db.prepareDbUnit(getClass(), "shared.xml");
61     FileSourceTesting.updateDataColumn(connection, "FILE_UUID", FileSourceTesting.newFakeData(3).build());
62
63     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
64     assertThat(iterator.hasNext()).isTrue();
65     SourceLineResultSetIterator.SourceFile file = iterator.next();
66     assertThat(file.getLines()).hasSize(3);
67     SourceLineDoc firstLine = file.getLines().get(0);
68     assertThat(firstLine.projectUuid()).isEqualTo("PROJECT_UUID");
69     assertThat(firstLine.fileUuid()).isEqualTo("FILE_UUID");
70     assertThat(firstLine.line()).isEqualTo(1);
71     assertThat(firstLine.scmRevision()).isEqualTo("REVISION_1");
72     assertThat(firstLine.scmAuthor()).isEqualTo("AUTHOR_1");
73     assertThat(firstLine.highlighting()).isEqualTo("HIGHLIGHTING_1");
74     assertThat(firstLine.symbols()).isEqualTo("SYMBOLS_1");
75     assertThat(firstLine.source()).isEqualTo("SOURCE_1");
76     assertThat(firstLine.utLineHits()).isEqualTo(1);
77     assertThat(firstLine.utConditions()).isEqualTo(2);
78     assertThat(firstLine.utCoveredConditions()).isEqualTo(3);
79     assertThat(firstLine.itLineHits()).isEqualTo(4);
80     assertThat(firstLine.itConditions()).isEqualTo(5);
81     assertThat(firstLine.itCoveredConditions()).isEqualTo(6);
82     assertThat(firstLine.overallLineHits()).isEqualTo(7);
83     assertThat(firstLine.overallConditions()).isEqualTo(8);
84     assertThat(firstLine.overallCoveredConditions()).isEqualTo(9);
85     iterator.close();
86   }
87
88   @Test
89   public void should_ignore_lines_already_handled() throws Exception {
90     db.prepareDbUnit(getClass(), "shared.xml");
91
92     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 2000000000000L);
93     assertThat(iterator.hasNext()).isFalse();
94     iterator.close();
95   }
96
97   @Test
98   public void should_fail_on_bad_data_format() throws Exception {
99     db.prepareDbUnit(getClass(), "shared.xml");
100
101     FileSourceTesting.updateDataColumn(connection, "FILE_UUID", "THIS_IS_NOT_PROTOBUF".getBytes());
102
103     SourceLineResultSetIterator iterator = SourceLineResultSetIterator.create(dbClient, connection, 0L);
104     try {
105       assertThat(iterator.hasNext()).isTrue();
106       iterator.next();
107       fail();
108     } catch (IllegalStateException e) {
109       // ok
110     }
111     iterator.close();
112   }
113 }