1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.batch.scan;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.BatchExtension;
import org.sonar.api.CoreProperties;
import org.sonar.api.ServerExtension;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.bootstrap.ProjectBootstrapper;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.task.TaskExtension;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.TempFolder;
import org.sonar.batch.bootstrap.AnalysisMode;
import org.sonar.batch.bootstrap.BootstrapProperties;
import org.sonar.batch.bootstrap.ExtensionInstaller;
import org.sonar.batch.bootstrap.GlobalSettings;
import org.sonar.batch.bootstrap.TaskProperties;
import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
import org.sonar.batch.protocol.input.GlobalReferentials;
import org.sonar.batch.protocol.input.ProjectReferentials;
import org.sonar.batch.referential.ProjectReferentialsLoader;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
import java.util.Collections;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ProjectScanContainerTest {
private ProjectBootstrapper projectBootstrapper;
private ProjectScanContainer container;
private Settings settings;
private ComponentContainer parentContainer;
private BootstrapProperties bootstrapProperties;
@Before
public void prepare() {
projectBootstrapper = mock(ProjectBootstrapper.class);
bootstrapProperties = new BootstrapProperties(Collections.<String, String>emptyMap());
AnalysisMode analysisMode = new AnalysisMode(bootstrapProperties);
when(projectBootstrapper.bootstrap()).thenReturn(new ProjectReactor(ProjectDefinition.create()));
parentContainer = new ComponentContainer();
parentContainer.add(System2.INSTANCE);
parentContainer.add(bootstrapProperties);
parentContainer.add(analysisMode);
GlobalReferentials globalRef = new GlobalReferentials();
settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), globalRef, analysisMode);
parentContainer.add(settings);
ProjectReferentialsLoader projectReferentialsLoader = new ProjectReferentialsLoader() {
@Override
public ProjectReferentials load(ProjectReactor reactor, TaskProperties taskProperties) {
return new ProjectReferentials();
}
};
parentContainer.add(projectReferentialsLoader);
parentContainer.add(mock(TaskProperties.class));
container = new ProjectScanContainer(parentContainer);
}
@Test
public void should_add_fake_maven_executor_on_non_maven_env() {
container.add(mock(ExtensionInstaller.class), projectBootstrapper);
container.doBeforeStart();
assertThat(container.getComponentByType(MavenPluginExecutor.class)).isNotNull();
}
@Test
public void should_use_maven_executor_provided_by_maven() {
container.add(mock(ExtensionInstaller.class), projectBootstrapper);
MavenPluginExecutor mavenPluginExecutor = mock(MavenPluginExecutor.class);
container.add(mavenPluginExecutor);
container.doBeforeStart();
assertThat(container.getComponentsByType(MavenPluginExecutor.class)).hasSize(1);
assertThat(container.getComponentByType(MavenPluginExecutor.class)).isSameAs(mavenPluginExecutor);
}
@Test
public void should_activate_profiling() {
container.add(mock(ExtensionInstaller.class), projectBootstrapper, mock(TempFolder.class));
container.doBeforeStart();
assertThat(container.getComponentsByType(PhasesSumUpTimeProfiler.class)).hasSize(0);
settings.setProperty(CoreProperties.PROFILING_LOG_PROPERTY, "true");
container = new ProjectScanContainer(parentContainer);
container.add(mock(ExtensionInstaller.class), projectBootstrapper, mock(TempFolder.class));
container.doBeforeStart();
assertThat(container.getComponentsByType(PhasesSumUpTimeProfiler.class)).hasSize(1);
}
@Test
public void should_add_only_batch_extensions() {
ProjectScanContainer.BatchExtensionFilter filter = new ProjectScanContainer.BatchExtensionFilter();
assertThat(filter.accept(new MyBatchExtension())).isTrue();
assertThat(filter.accept(MyBatchExtension.class)).isTrue();
assertThat(filter.accept(new MyProjectExtension())).isFalse();
assertThat(filter.accept(MyProjectExtension.class)).isFalse();
assertThat(filter.accept(new MyServerExtension())).isFalse();
assertThat(filter.accept(MyServerExtension.class)).isFalse();
assertThat(filter.accept(new MyTaskExtension())).isFalse();
assertThat(filter.accept(MyTaskExtension.class)).isFalse();
}
@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
static class MyBatchExtension implements BatchExtension {
}
static class MyProjectExtension implements BatchExtension {
}
static class MyServerExtension implements ServerExtension {
}
static class MyTaskExtension implements TaskExtension {
}
}
|