1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.creadur.whisker.out.velocity;
20
21 import static org.apache.creadur.whisker.app.ConfigurationBuilder.*;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.creadur.whisker.app.Result;
27 import org.apache.creadur.whisker.model.Descriptor;
28
29 public class TestLicenseGeneration extends TestCase {
30
31 StringResultWriterFactory writerFactory;
32 VelocityEngine subject;
33 DescriptorBuilderForTesting builder;
34
35 @Override
36 protected void setUp() throws Exception {
37 super.setUp();
38 writerFactory = new StringResultWriterFactory();
39 subject = new VelocityEngine(new EmptyLog());
40 builder = new DescriptorBuilderForTesting();
41 }
42
43 @Override
44 protected void tearDown() throws Exception {
45 super.tearDown();
46 }
47
48
49 public void testThatWhenThereAreNoThirdPartyContentsFooterIsNotShown() throws Exception {
50 Descriptor work = builder.build();
51
52 subject.generate(work, writerFactory, aConfiguration().build());
53
54 assertTrue("Check that work is suitable for this test", work.isPrimaryOnly());
55 assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
56 assertEquals("When no third party contents, expect that only the license text is output",
57 builder.getPrimaryLicenseText(),
58 writerFactory.firstOutputFor(Result.LICENSE).trim());
59 }
60
61 public void testThatFooterIsShownWhenThereAreThirdPartyContents() throws Exception {
62 Descriptor work = builder.withPrimaryLicenseAndThirdPartyOrgInDirectory("lib").build();
63
64 assertFalse("Check that work is suitable for this test", work.isPrimaryOnly());
65
66 subject.generate(work, writerFactory, aConfiguration().build());
67
68 assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
69 assertTrue("Expect information when third party contents present: " + writerFactory.firstOutputFor(Result.LICENSE),
70 StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
71 "This distribution contains third party resources."));
72 }
73
74 public void testSecondaryCopyrightNoticeForPrimaryLicense() throws Exception {
75 subject.generate(
76 builder
77 .withSecondaryCopyrightNotice()
78 .withPrimaryCopyrightNotice()
79 .withPrimaryLicenseAndOrgInDirectory("lib").build(), writerFactory, aConfiguration().build());
80
81 assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
82 assertTrue("Expect secondary copyright to be presented: " + writerFactory.firstOutputFor(Result.LICENSE),
83 StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
84 builder.getSecondaryCopyright()));
85 verifyThatResourceNameIsWritten();
86
87 }
88
89 private void verifyThatResourceNameIsWritten() {
90 assertTrue("Expect resource to be indicated: " + writerFactory.firstOutputFor(Result.LICENSE),
91 StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
92 builder.getResourceName()));
93 }
94
95 private void verifyThatResourceNameIsNotWritten() {
96 assertFalse("Expect resource to be indicated: " + writerFactory.firstOutputFor(Result.LICENSE),
97 StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
98 builder.getResourceName()));
99 }
100
101 public void testPrimaryOrganisationSecondaryLicense() throws Exception {
102 subject.generate(
103 builder.withSecondaryLicensePrimaryOrganisationInDirectory("lib").build(),
104 writerFactory, aConfiguration().build());
105 assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
106
107 verifyThatResourceNameIsWritten();
108
109 }
110
111 public void testIgnorePrimaryOrganisationPrimaryLicense() throws Exception {
112 subject.generate(
113 builder.withPrimaryLicenseAndOrgInDirectory("lib").build(),
114 writerFactory, aConfiguration().build());
115 assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
116 verifyThatResourceNameIsNotWritten();
117
118 }
119
120 public void testIgnorePrimaryOrganisationPrimaryLicensePrimaryCopyrightNotice() throws Exception {
121 subject.generate(
122 builder
123 .withPrimaryCopyrightNotice()
124 .withPrimaryLicenseAndOrgInDirectory("lib")
125 .build(),
126 writerFactory, aConfiguration().build());
127 assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
128 verifyThatResourceNameIsNotWritten();
129
130 }
131
132 }