-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ProjectReactorBuilderTest.java
695 lines (584 loc) · 31.6 KB
/
ProjectReactorBuilderTest.java
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
* 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 com.google.common.collect.Maps;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.batch.analysis.AnalysisProperties;
import org.sonar.test.TestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ProjectReactorBuilderTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private AnalysisMode mode;
@Before
public void setUp() {
mode = mock(AnalysisMode.class);
}
@Test
public void shouldDefineSimpleProject() {
ProjectDefinition projectDefinition = loadProjectDefinition("simple-project");
assertThat(projectDefinition.getKey()).isEqualTo("com.foo.project");
assertThat(projectDefinition.getName()).isEqualTo("Foo Project");
assertThat(projectDefinition.getVersion()).isEqualTo("1.0-SNAPSHOT");
assertThat(projectDefinition.getDescription()).isEqualTo("Description of Foo Project");
assertThat(projectDefinition.getSourceDirs()).contains("sources");
assertThat(projectDefinition.getLibraries()).contains(TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath(),
TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath());
}
@Test
public void shouldFailIfUnexistingSourceDirectory() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("The folder 'unexisting-source-dir' does not exist for 'com.foo.project' (base directory = "
+ TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-source-dir") + ")");
loadProjectDefinition("simple-project-with-unexisting-source-dir");
}
@Test
public void fail_if_sources_not_set() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("You must define the following mandatory properties for 'com.foo.project': sonar.sources");
loadProjectDefinition("simple-project-with-missing-source-dir");
}
@Test
public void shouldNotFailIfBlankSourceDirectory() {
loadProjectDefinition("simple-project-with-blank-source-dir");
}
@Test
public void modulesDuplicateIds() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Two modules have the same id: 'module1'. Each module must have a unique id.");
loadProjectDefinition("multi-module-duplicate-id");
}
@Test
public void modulesRepeatedIds() {
ProjectDefinition rootProject = loadProjectDefinition("multi-module-repeated-id");
List<ProjectDefinition> modules = rootProject.getSubProjects();
assertThat(modules.size()).isEqualTo(1);
// Module 1
ProjectDefinition module1 = modules.get(0);
assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
assertThat(module1.getName()).isEqualTo("Foo Module 1");
// Module 1 -> Module 1
ProjectDefinition module1_module1 = module1.getSubProjects().get(0);
assertThat(module1_module1.getKey()).isEqualTo("com.foo.project:module1:module1");
assertThat(module1_module1.getName()).isEqualTo("Foo Sub Module 1");
}
@Test
public void shouldDefineMultiModuleProjectWithDefinitionsAllInRootProject() throws IOException {
ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-all-in-root");
// CHECK ROOT
assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
assertThat(rootProject.getName()).isEqualTo("Foo Project");
assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
// root project must not contain some properties - even if they are defined in the root properties file
assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
// and module properties must have been cleaned
assertThat(rootProject.properties().get("module1.sonar.projectKey")).isNull();
assertThat(rootProject.properties().get("module2.sonar.projectKey")).isNull();
// Check baseDir and workDir
assertThat(rootProject.getBaseDir().getCanonicalFile())
.isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"));
assertThat(rootProject.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar"));
// CHECK MODULES
List<ProjectDefinition> modules = rootProject.getSubProjects();
assertThat(modules.size()).isEqualTo(2);
// Module 1
ProjectDefinition module1 = modules.get(0);
assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1"));
assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
assertThat(module1.getName()).isEqualTo("module1");
assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
// Description should not be inherited from parent if not set
assertThat(module1.getDescription()).isNull();
assertThat(module1.getSourceDirs()).contains("sources");
assertThat(module1.getTestDirs()).contains("tests");
assertThat(module1.getBinaries()).contains("target/classes");
// and module properties must have been cleaned
assertThat(module1.properties().get("module1.sonar.projectKey")).isNull();
assertThat(module1.properties().get("module2.sonar.projectKey")).isNull();
// Check baseDir and workDir
assertThat(module1.getBaseDir().getCanonicalFile())
.isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1"));
assertThat(module1.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_module1"));
// Module 2
ProjectDefinition module2 = modules.get(1);
assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2"));
assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
assertThat(module2.getName()).isEqualTo("Foo Module 2");
assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
assertThat(module2.getSourceDirs()).contains("src");
assertThat(module2.getTestDirs()).contains("tests");
assertThat(module2.getBinaries()).contains("target/classes");
// and module properties must have been cleaned
assertThat(module2.properties().get("module1.sonar.projectKey")).isNull();
assertThat(module2.properties().get("module2.sonar.projectKey")).isNull();
// Check baseDir and workDir
assertThat(module2.getBaseDir().getCanonicalFile())
.isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2"));
assertThat(module2.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_com.foo.project.module2"));
}
// SONAR-4876
@Test
public void shouldDefineMultiModuleProjectWithModuleKey() {
ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-moduleKey");
// CHECK ROOT
// module properties must have been cleaned
assertThat(rootProject.properties().get("module1.sonar.moduleKey")).isNull();
assertThat(rootProject.properties().get("module2.sonar.moduleKey")).isNull();
// CHECK MODULES
List<ProjectDefinition> modules = rootProject.getSubProjects();
assertThat(modules.size()).isEqualTo(2);
// Module 2
ProjectDefinition module2 = modules.get(1);
assertThat(module2.getKey()).isEqualTo("com.foo.project.module2");
}
// SONARPLUGINS-2421
@Test
public void shouldDefineMultiLanguageProjectWithDefinitionsAllInRootProject() throws IOException {
ProjectDefinition rootProject = loadProjectDefinition("multi-language-definitions-all-in-root");
// CHECK ROOT
assertThat(rootProject.getKey()).isEqualTo("example");
assertThat(rootProject.getName()).isEqualTo("Example");
assertThat(rootProject.getVersion()).isEqualTo("1.0");
// CHECK MODULES
List<ProjectDefinition> modules = rootProject.getSubProjects();
assertThat(modules.size()).isEqualTo(2);
// Module 1
ProjectDefinition module1 = modules.get(0);
assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"));
assertThat(module1.getSourceDirs()).contains("src/main/java");
// and module properties must have been cleaned
assertThat(module1.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_java-module"));
// Module 2
ProjectDefinition module2 = modules.get(1);
assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"));
assertThat(module2.getSourceDirs()).contains("src/main/groovy");
// and module properties must have been cleaned
assertThat(module2.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_groovy-module"));
}
@Test
public void shouldDefineMultiModuleProjectWithBaseDir() {
ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-basedir");
List<ProjectDefinition> modules = rootProject.getSubProjects();
assertThat(modules.size()).isEqualTo(1);
assertThat(modules.get(0).getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
}
@Test
public void shouldFailIfUnexistingModuleBaseDir() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("The base directory of the module 'module1' does not exist: "
+ TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-basedir").getAbsolutePath() + File.separator + "module1");
loadProjectDefinition("multi-module-with-unexisting-basedir");
}
@Test
public void shouldFailIfUnexistingSourceFolderInheritedInMultimodule() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("The folder 'unexisting-source-dir' does not exist for 'com.foo.project:module1' (base directory = "
+ TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-source-dir").getAbsolutePath() + File.separator + "module1)");
loadProjectDefinition("multi-module-with-unexisting-source-dir");
}
@Test
public void shouldNotFailIfUnexistingTestBinLibFolderInheritedInMultimodule() {
loadProjectDefinition("multi-module-with-unexisting-test-bin-lib-dir");
}
@Test
public void shouldFailIfExplicitUnexistingTestFolder() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("The folder 'tests' does not exist for 'com.foo.project' (base directory = "
+ TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-test-dir").getAbsolutePath());
loadProjectDefinition("simple-project-with-unexisting-test-dir");
}
@Test
public void shouldFailIfExplicitUnexistingBinaryFolder() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("The folder 'bin' does not exist for 'com.foo.project' (base directory = "
+ TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-binary").getAbsolutePath());
loadProjectDefinition("simple-project-with-unexisting-binary");
}
@Test
public void shouldFailIfExplicitUnmatchingLibFolder() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("No files nor directories matching 'libs/*.txt' in directory "
+ TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-lib").getAbsolutePath());
loadProjectDefinition("simple-project-with-unexisting-lib");
}
@Test
public void shouldGetLibDirectory() {
ProjectDefinition def = loadProjectDefinition("simple-project-with-lib-dir");
assertThat(def.getLibraries()).hasSize(1);
File libDir = new File(def.getLibraries().get(0));
assertThat(libDir).isDirectory().exists();
assertThat(libDir.getName()).isEqualTo("lib");
}
@Test
public void shouldFailIfExplicitUnexistingTestFolderOnModule() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("The folder 'tests' does not exist for 'module1' (base directory = "
+ TestUtils.getResource(this.getClass(), "multi-module-with-explicit-unexisting-test-dir").getAbsolutePath() + File.separator + "module1)");
loadProjectDefinition("multi-module-with-explicit-unexisting-test-dir");
}
@Test
public void shouldFailIfExplicitUnexistingBinaryFolderOnModule() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("The folder 'bin' does not exist for 'module1' (base directory = "
+ TestUtils.getResource(this.getClass(), "multi-module-with-explicit-unexisting-binary-dir").getAbsolutePath() + File.separator + "module1)");
loadProjectDefinition("multi-module-with-explicit-unexisting-binary-dir");
}
@Test
public void shouldFailIfExplicitUnmatchingLibFolderOnModule() {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("No files nor directories matching 'lib/*.jar' in directory "
+ TestUtils.getResource(this.getClass(), "multi-module-with-explicit-unexisting-lib").getAbsolutePath() + File.separator + "module1");
loadProjectDefinition("multi-module-with-explicit-unexisting-lib");
}
@Test
public void multiModuleProperties() {
ProjectDefinition projectDefinition = loadProjectDefinition("big-multi-module-definitions-all-in-root");
assertThat(projectDefinition.properties().get("module11.property")).isNull();
assertThat(projectDefinition.properties().get("sonar.profile")).isEqualTo("Foo");
ProjectDefinition module1 = null;
ProjectDefinition module2 = null;
for (ProjectDefinition prj : projectDefinition.getSubProjects()) {
if (prj.getKey().equals("com.foo.project:module1")) {
module1 = prj;
} else if (prj.getKey().equals("com.foo.project:module2")) {
module2 = prj;
}
}
assertThat(module1.properties().get("module11.property")).isNull();
assertThat(module1.properties().get("property")).isNull();
assertThat(module1.properties().get("sonar.profile")).isEqualTo("Foo");
assertThat(module2.properties().get("module11.property")).isNull();
assertThat(module2.properties().get("property")).isNull();
assertThat(module2.properties().get("sonar.profile")).isEqualTo("Foo");
ProjectDefinition module11 = null;
ProjectDefinition module12 = null;
for (ProjectDefinition prj : module1.getSubProjects()) {
if (prj.getKey().equals("com.foo.project:module1:module11")) {
module11 = prj;
} else if (prj.getKey().equals("com.foo.project:module1:module12")) {
module12 = prj;
}
}
assertThat(module11.properties().get("module1.module11.property")).isNull();
assertThat(module11.properties().get("module11.property")).isNull();
assertThat(module11.properties().get("property")).isEqualTo("My module11 property");
assertThat(module11.properties().get("sonar.profile")).isEqualTo("Foo");
assertThat(module12.properties().get("module11.property")).isNull();
assertThat(module12.properties().get("property")).isNull();
assertThat(module12.properties().get("sonar.profile")).isEqualTo("Foo");
}
@Test
public void shouldRemoveModulePropertiesFromTaskProperties() {
Map<String, String> props = loadProps("big-multi-module-definitions-all-in-root");
AnalysisProperties taskProperties = new AnalysisProperties(props, null);
assertThat(taskProperties.property("module1.module11.property")).isEqualTo("My module11 property");
new ProjectReactorBuilder(taskProperties, mode).execute();
assertThat(taskProperties.property("module1.module11.property")).isNull();
}
@Test
public void shouldFailIfMandatoryPropertiesAreNotPresent() {
Map<String, String> props = new HashMap<>();
props.put("foo1", "bla");
props.put("foo4", "bla");
thrown.expect(IllegalStateException.class);
thrown.expectMessage("You must define the following mandatory properties for 'Unknown': foo2, foo3");
ProjectReactorBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
}
@Test
public void shouldFailIfMandatoryPropertiesAreNotPresentButWithProjectKey() {
Map<String, String> props = new HashMap<>();
props.put("foo1", "bla");
props.put("sonar.projectKey", "my-project");
thrown.expect(IllegalStateException.class);
thrown.expectMessage("You must define the following mandatory properties for 'my-project': foo2, foo3");
ProjectReactorBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
}
@Test
public void shouldNotFailIfMandatoryPropertiesArePresent() {
Map<String, String> props = new HashMap<>();
props.put("foo1", "bla");
props.put("foo4", "bla");
ProjectReactorBuilder.checkMandatoryProperties(props, new String[] {"foo1"});
// No exception should be thrown
}
@Test
public void shouldFilterFiles() {
File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
assertThat(ProjectReactorBuilder.getLibraries(baseDir, "in*.txt")).hasSize(1);
assertThat(ProjectReactorBuilder.getLibraries(baseDir, "*.txt")).hasSize(2);
assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt")).hasSize(1);
assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt")).hasSize(2);
}
@Test
public void shouldWorkWithAbsolutePath() {
File baseDir = new File("not-exists");
String absolutePattern = TestUtils.getResource(this.getClass(), "shouldFilterFiles").getAbsolutePath() + "/in*.txt";
assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), absolutePattern)).hasSize(1);
}
@Test
public void shouldGetRelativeFile() {
assertThat(ProjectReactorBuilder.resolvePath(TestUtils.getResource(this.getClass(), "/"), "shouldGetFile/foo.properties"))
.isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties"));
}
@Test
public void shouldGetAbsoluteFile() {
File file = TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties");
assertThat(ProjectReactorBuilder.resolvePath(TestUtils.getResource(this.getClass(), "/"), file.getAbsolutePath()))
.isEqualTo(file);
}
@Test
public void shouldMergeParentProperties() {
// Use a random value to avoid VM optimization that would create constant String and make s1 and s2 the same object
int i = (int) Math.random() * 10;
String s1 = "value" + i;
String s2 = "value" + i;
Map<String, String> parentProps = new HashMap<>();
parentProps.put("toBeMergeProps", "fooParent");
parentProps.put("existingChildProp", "barParent");
parentProps.put("duplicatedProp", s1);
parentProps.put("sonar.projectDescription", "Desc from Parent");
Map<String, String> childProps = new HashMap<>();
childProps.put("existingChildProp", "barChild");
childProps.put("otherProp", "tutuChild");
childProps.put("duplicatedProp", s2);
ProjectReactorBuilder.mergeParentProperties(childProps, parentProps);
assertThat(childProps).hasSize(4);
assertThat(childProps.get("toBeMergeProps")).isEqualTo("fooParent");
assertThat(childProps.get("existingChildProp")).isEqualTo("barChild");
assertThat(childProps.get("otherProp")).isEqualTo("tutuChild");
assertThat(childProps.get("sonar.projectDescription")).isNull();
assertThat(childProps.get("duplicatedProp")).isSameAs(parentProps.get("duplicatedProp"));
}
@Test
public void shouldInitRootWorkDir() {
ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(Maps.<String, String>newHashMap(), null), mode);
File baseDir = new File("target/tmp/baseDir");
File workDir = builder.initRootProjectWorkDir(baseDir, Maps.<String, String>newHashMap());
assertThat(workDir).isEqualTo(new File(baseDir, ".sonar"));
}
@Test
public void nonAssociatedMode() {
when(mode.isIssues()).thenReturn(true);
ProjectDefinition project = loadProjectDefinition("multi-module-with-basedir-not-associated");
assertThat(project.getKey()).isEqualTo("project");
}
@Test
public void shouldInitWorkDirWithCustomRelativeFolder() {
Map<String, String> props = Maps.<String, String>newHashMap();
props.put("sonar.working.directory", ".foo");
ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(props, null), mode);
File baseDir = new File("target/tmp/baseDir");
File workDir = builder.initRootProjectWorkDir(baseDir, props);
assertThat(workDir).isEqualTo(new File(baseDir, ".foo"));
}
@Test
public void shouldInitRootWorkDirWithCustomAbsoluteFolder() {
Map<String, String> props = Maps.<String, String>newHashMap();
props.put("sonar.working.directory", new File("src").getAbsolutePath());
ProjectReactorBuilder builder = new ProjectReactorBuilder(new AnalysisProperties(props, null), mode);
File baseDir = new File("target/tmp/baseDir");
File workDir = builder.initRootProjectWorkDir(baseDir, props);
assertThat(workDir).isEqualTo(new File("src").getAbsoluteFile());
}
@Test
public void shouldFailIf2ModulesWithSameKey() {
Map<String, String> props = new HashMap<>();
props.put("sonar.projectKey", "root");
ProjectDefinition root = ProjectDefinition.create().setProperties(props);
Map<String, String> props1 = new HashMap<>();
props1.put("sonar.projectKey", "mod1");
root.addSubProject(ProjectDefinition.create().setProperties(props1));
// Check uniqueness of a new module: OK
Map<String, String> props2 = new HashMap<>();
props2.put("sonar.projectKey", "mod2");
ProjectDefinition mod2 = ProjectDefinition.create().setProperties(props2);
ProjectReactorBuilder.checkUniquenessOfChildKey(mod2, root);
// Now, add it and check again
root.addSubProject(mod2);
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Project 'root' can't have 2 modules with the following key: mod2");
ProjectReactorBuilder.checkUniquenessOfChildKey(mod2, root);
}
@Test
public void shouldSetModuleKeyIfNotPresent() {
Map<String, String> props = new HashMap<>();
props.put("sonar.projectVersion", "1.0");
// should be set
ProjectReactorBuilder.setModuleKeyAndNameIfNotDefined(props, "foo", "parent");
assertThat(props.get("sonar.moduleKey")).isEqualTo("parent:foo");
assertThat(props.get("sonar.projectName")).isEqualTo("foo");
// but not this 2nd time
ProjectReactorBuilder.setModuleKeyAndNameIfNotDefined(props, "bar", "parent");
assertThat(props.get("sonar.moduleKey")).isEqualTo("parent:foo");
assertThat(props.get("sonar.projectName")).isEqualTo("foo");
}
private ProjectDefinition loadProjectDefinition(String projectFolder) {
Map<String, String> props = loadProps(projectFolder);
AnalysisProperties bootstrapProps = new AnalysisProperties(props, null);
ProjectReactor projectReactor = new ProjectReactorBuilder(bootstrapProps, mode).execute();
return projectReactor.getRoot();
}
protected static Properties toProperties(File propertyFile) {
Properties propsFromFile = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(propertyFile)) {
propsFromFile.load(fileInputStream);
} catch (IOException e) {
throw new IllegalStateException("Impossible to read the property file: " + propertyFile.getAbsolutePath(), e);
}
// Trim properties
for (String propKey : propsFromFile.stringPropertyNames()) {
propsFromFile.setProperty(propKey, StringUtils.trim(propsFromFile.getProperty(propKey)));
}
return propsFromFile;
}
private Map<String, String> loadProps(String projectFolder) {
Map<String, String> props = Maps.<String, String>newHashMap();
Properties runnerProps = toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
for (final String name : runnerProps.stringPropertyNames()) {
props.put(name, runnerProps.getProperty(name));
}
props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
return props;
}
public Map<String, String> toMap(Properties props) {
Map<String, String> result = new HashMap<>();
for (Map.Entry<Object, Object> entry : props.entrySet()) {
result.put(entry.getKey().toString(), entry.getValue().toString());
}
return result;
}
@Test
public void shouldGetList() {
Map<String, String> props = new HashMap<>();
props.put("prop", " foo , bar , \n\ntoto,tutu");
assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
}
@Test
public void shouldGetListFromFile() throws IOException {
String filePath = "shouldGetList/foo.properties";
Map<String, String> props = loadPropsFromFile(filePath);
assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
}
@Test
public void shouldDefineProjectWithBuildDir() {
ProjectDefinition rootProject = loadProjectDefinition("simple-project-with-build-dir");
File buildDir = rootProject.getBuildDir();
assertThat(buildDir).isDirectory().exists();
assertThat(new File(buildDir, "report.txt")).isFile().exists();
assertThat(buildDir.getName()).isEqualTo("build");
}
@Test
public void doNotMixPropertiesWhenModuleKeyIsPrefixOfAnother() throws IOException {
ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-same-prefix");
// CHECK ROOT
assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
assertThat(rootProject.getName()).isEqualTo("Foo Project");
assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
// root project must not contain some properties - even if they are defined in the root properties file
assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
// and module properties must have been cleaned
assertThat(rootProject.properties().get("module1.sonar.projectKey")).isNull();
assertThat(rootProject.properties().get("module2.sonar.projectKey")).isNull();
// Check baseDir and workDir
assertThat(rootProject.getBaseDir().getCanonicalFile())
.isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"));
assertThat(rootProject.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar"));
// CHECK MODULES
List<ProjectDefinition> modules = rootProject.getSubProjects();
assertThat(modules.size()).isEqualTo(2);
// Module 1
ProjectDefinition module1 = modules.get(0);
assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1"));
assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
assertThat(module1.getName()).isEqualTo("module1");
assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
// Description should not be inherited from parent if not set
assertThat(module1.getDescription()).isNull();
assertThat(module1.getSourceDirs()).contains("sources");
assertThat(module1.getTestDirs()).contains("tests");
assertThat(module1.getBinaries()).contains("target/classes");
// and module properties must have been cleaned
assertThat(module1.properties().get("module1.sonar.projectKey")).isNull();
assertThat(module1.properties().get("module2.sonar.projectKey")).isNull();
// Check baseDir and workDir
assertThat(module1.getBaseDir().getCanonicalFile())
.isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1"));
assertThat(module1.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_module1"));
// Module 1 Feature
ProjectDefinition module1Feature = modules.get(1);
assertThat(module1Feature.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1.feature"));
assertThat(module1Feature.getKey()).isEqualTo("com.foo.project:com.foo.project.module1.feature");
assertThat(module1Feature.getName()).isEqualTo("Foo Module 1 Feature");
assertThat(module1Feature.getVersion()).isEqualTo("1.0-SNAPSHOT");
assertThat(module1Feature.getDescription()).isEqualTo("Description of Module 1 Feature");
assertThat(module1Feature.getSourceDirs()).contains("src");
assertThat(module1Feature.getTestDirs()).contains("tests");
assertThat(module1Feature.getBinaries()).contains("target/classes");
// and module properties must have been cleaned
assertThat(module1Feature.properties().get("module1.sonar.projectKey")).isNull();
assertThat(module1Feature.properties().get("module2.sonar.projectKey")).isNull();
// Check baseDir and workDir
assertThat(module1Feature.getBaseDir().getCanonicalFile())
.isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix/module1.feature"));
assertThat(module1Feature.getWorkDir().getCanonicalFile())
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-same-prefix"), ".sonar/com.foo.project_com.foo.project.module1.feature"));
}
private Map<String, String> loadPropsFromFile(String filePath) throws IOException {
Properties props = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath))) {
props.load(fileInputStream);
}
Map<String, String> result = new HashMap<>();
for (Map.Entry<Object, Object> entry : props.entrySet()) {
result.put(entry.getKey().toString(), entry.getValue().toString());
}
return result;
}
}