]> source.dussan.org Git - sonarqube.git/blob
90ffa33a9e31cda581477630b79e491c01573deb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.component;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.scanner.protocol.output.ScannerReport;
26 import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType;
27
28 import static com.google.common.base.Predicates.in;
29 import static com.google.common.base.Predicates.not;
30 import static com.google.common.collect.FluentIterable.from;
31 import static java.util.Arrays.asList;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.fail;
34 import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.DIRECTORY;
35 import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.FILE;
36 import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.UNRECOGNIZED;
37 import static org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType.UNSET;
38 import static org.sonar.server.computation.task.projectanalysis.component.ComponentImpl.builder;
39
40 public class ComponentImplTest {
41
42   static final String KEY = "KEY";
43   static final String UUID = "UUID";
44
45   @Rule
46   public ExpectedException thrown = ExpectedException.none();
47
48   @Test
49   public void verify_key_and_uuid() throws Exception {
50     ComponentImpl component = builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setKey(KEY).setUuid(UUID).build();
51
52     assertThat(component.getKey()).isEqualTo(KEY);
53     assertThat(component.getUuid()).isEqualTo(UUID);
54   }
55
56   @Test
57   public void builder_throws_NPE_if_component_arg_is_Null() {
58     thrown.expect(NullPointerException.class);
59
60     builder(null);
61   }
62
63   @Test
64   public void set_key_throws_NPE_if_component_arg_is_Null() {
65     thrown.expect(NullPointerException.class);
66
67     builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid(null);
68   }
69
70   @Test
71   public void set_uuid_throws_NPE_if_component_arg_is_Null() {
72     thrown.expect(NullPointerException.class);
73
74     builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setKey(null);
75   }
76
77   @Test
78   public void build_without_key_throws_NPE_if_component_arg_is_Null() {
79     thrown.expect(NullPointerException.class);
80
81     builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid("ABCD").build();
82   }
83
84   @Test
85   public void build_without_uuid_throws_NPE_if_component_arg_is_Null() {
86     thrown.expect(NullPointerException.class);
87
88     builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setKey(KEY).build();
89   }
90
91   @Test
92   public void get_name_from_batch_component() {
93     String name = "project";
94     ComponentImpl component = buildSimpleComponent(ScannerReport.Component.newBuilder().setType(FILE).setName(name).build());
95     assertThat(component.getName()).isEqualTo(name);
96   }
97
98   @Test
99   public void get_version_from_batch_component() {
100     String version = "1.0";
101     ComponentImpl component = buildSimpleComponent(ScannerReport.Component.newBuilder().setType(FILE).setVersion(version).build());
102     assertThat(component.getReportAttributes().getVersion()).isEqualTo(version);
103   }
104
105   @Test
106   public void getFileAttributes_throws_ISE_if_BatchComponent_does_not_have_type_FILE() {
107     for (ComponentType componentType : from(asList(ComponentType.values())).filter(not(in(asList(FILE, UNSET, UNRECOGNIZED))))) {
108       ComponentImpl component = buildSimpleComponent(ScannerReport.Component.newBuilder().setType(componentType).build());
109       try {
110         component.getFileAttributes();
111         fail("A IllegalStateException should have been raised");
112       } catch (IllegalStateException e) {
113         assertThat(e).hasMessage("Only component of type FILE have a FileAttributes object");
114       }
115     }
116   }
117
118   @Test
119   public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() {
120     ComponentImpl component = buildSimpleComponent(ScannerReport.Component.newBuilder().setType(FILE).setIsTest(true).build());
121
122     assertThat(component.getFileAttributes().isUnitTest()).isTrue();
123   }
124
125   @Test
126   public void isUnitTest_returns_value_of_language_of_BatchComponent() {
127     String languageKey = "some language key";
128     ComponentImpl component = buildSimpleComponent(ScannerReport.Component.newBuilder().setType(FILE).setLanguage(languageKey).build());
129
130     assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
131   }
132
133   @Test
134   public void build_with_child() throws Exception {
135     ComponentImpl child = builder(ScannerReport.Component.newBuilder().setType(FILE).build())
136       .setKey("CHILD_KEY")
137       .setUuid("CHILD_UUID")
138       .build();
139     ComponentImpl componentImpl = builder(ScannerReport.Component.newBuilder().setType(DIRECTORY).build())
140       .setKey(KEY)
141       .setUuid(UUID)
142       .addChildren(child)
143       .build();
144
145     assertThat(componentImpl.getChildren()).hasSize(1);
146     Component childReloaded = componentImpl.getChildren().iterator().next();
147     assertThat(childReloaded.getKey()).isEqualTo("CHILD_KEY");
148     assertThat(childReloaded.getUuid()).isEqualTo("CHILD_UUID");
149     assertThat(childReloaded.getType()).isEqualTo(Component.Type.FILE);
150   }
151
152   @Test
153   public void convertType() {
154     for (ComponentType componentType : from(asList(ComponentType.values())).filter(not(in(asList(UNSET, UNRECOGNIZED))))) {
155       assertThat(ComponentImpl.Builder.convertType(componentType)).isEqualTo(Component.Type.valueOf(componentType.name()));
156     }
157   }
158
159   private static ComponentImpl buildSimpleComponent(ScannerReport.Component reportComponent) {
160     return builder(reportComponent).setKey(KEY).setUuid(UUID).build();
161   }
162
163   @Test
164   public void equals_compares_on_uuid_only() {
165     ComponentImpl.Builder builder = builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid(UUID);
166
167     assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("1").build());
168     assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("2").build());
169   }
170
171   @Test
172   public void hashCode_is_hashcode_of_uuid() {
173     ComponentImpl.Builder builder = builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid(UUID);
174
175     assertThat(builder.setKey("1").build().hashCode()).isEqualTo(builder.setKey("1").build().hashCode());
176     assertThat(builder.setKey("1").build().hashCode()).isEqualTo(builder.setKey("2").build().hashCode());
177     assertThat(builder.setKey("1").build().hashCode()).isEqualTo(UUID.hashCode());
178   }
179 }