3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.computation.task.projectanalysis.component;
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;
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;
40 public class ComponentImplTest {
42 static final String KEY = "KEY";
43 static final String UUID = "UUID";
46 public ExpectedException thrown = ExpectedException.none();
49 public void verify_key_and_uuid() throws Exception {
50 ComponentImpl component = builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setKey(KEY).setUuid(UUID).build();
52 assertThat(component.getKey()).isEqualTo(KEY);
53 assertThat(component.getUuid()).isEqualTo(UUID);
57 public void builder_throws_NPE_if_component_arg_is_Null() {
58 thrown.expect(NullPointerException.class);
64 public void set_key_throws_NPE_if_component_arg_is_Null() {
65 thrown.expect(NullPointerException.class);
67 builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid(null);
71 public void set_uuid_throws_NPE_if_component_arg_is_Null() {
72 thrown.expect(NullPointerException.class);
74 builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setKey(null);
78 public void build_without_key_throws_NPE_if_component_arg_is_Null() {
79 thrown.expect(NullPointerException.class);
81 builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid("ABCD").build();
85 public void build_without_uuid_throws_NPE_if_component_arg_is_Null() {
86 thrown.expect(NullPointerException.class);
88 builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setKey(KEY).build();
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);
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);
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());
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");
119 public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() {
120 ComponentImpl component = buildSimpleComponent(ScannerReport.Component.newBuilder().setType(FILE).setIsTest(true).build());
122 assertThat(component.getFileAttributes().isUnitTest()).isTrue();
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());
130 assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
134 public void build_with_child() throws Exception {
135 ComponentImpl child = builder(ScannerReport.Component.newBuilder().setType(FILE).build())
137 .setUuid("CHILD_UUID")
139 ComponentImpl componentImpl = builder(ScannerReport.Component.newBuilder().setType(DIRECTORY).build())
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);
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()));
159 private static ComponentImpl buildSimpleComponent(ScannerReport.Component reportComponent) {
160 return builder(reportComponent).setKey(KEY).setUuid(UUID).build();
164 public void equals_compares_on_uuid_only() {
165 ComponentImpl.Builder builder = builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid(UUID);
167 assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("1").build());
168 assertThat(builder.setKey("1").build()).isEqualTo(builder.setKey("2").build());
172 public void hashCode_is_hashcode_of_uuid() {
173 ComponentImpl.Builder builder = builder(ScannerReport.Component.newBuilder().setType(FILE).build()).setUuid(UUID);
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());