Simply test class by factoring out building logic.
git-svn-id: https://svn.apache.org/repos/asf/creadur/whisker/trunk@1408073 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/DescriptorBuilderForTesting.java b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/DescriptorBuilderForTesting.java
new file mode 100644
index 0000000..291ae4f
--- /dev/null
+++ b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/DescriptorBuilderForTesting.java
@@ -0,0 +1,143 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.creadur.whisker.out.velocity;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.creadur.whisker.model.ByOrganisation;
+import org.apache.creadur.whisker.model.Descriptor;
+import org.apache.creadur.whisker.model.License;
+import org.apache.creadur.whisker.model.Organisation;
+import org.apache.creadur.whisker.model.Resource;
+import org.apache.creadur.whisker.model.WithLicense;
+import org.apache.creadur.whisker.model.WithinDirectory;
+
+public class DescriptorBuilderForTesting {
+
+ private static final String A_PRIMARY_COPYRIGHT_NOTICE = "Copyright (c) this is primary";
+ String primaryLicenseText = "This is the primary license text";
+ Organisation thirdPartyOrg = new Organisation("third-party", "thirdparty.org", "http://thirdparty.org");
+ License primaryLicense = new License(false, primaryLicenseText, Collections.<String> emptyList(), "example.org", "http://example.org", "Example License");
+ String primaryOrgName = "example.org";
+ String primaryNotice = "The primary notice.";
+ Collection<WithinDirectory> contents = new ArrayList<WithinDirectory>();
+ Map<String, License> licenses = new HashMap<String, License>();
+ Map<String, String> notices = new HashMap<String, String>();
+ Map<String, Organisation> organisations = new HashMap<String, Organisation>();
+ String secondaryCopyright;
+ String resourceName;
+ String primaryCopyrightNotice = null;
+
+ public DescriptorBuilderForTesting() {
+ resourceName = "resource";
+ secondaryCopyright = "Copyright (c) this is secondary";
+ primaryLicense.storeIn(licenses);
+ }
+
+ public DescriptorBuilderForTesting withPrimaryLicenseAndOrgInDirectory(String directoryName) {
+ addDirectory(getPrimaryLicense(), primaryOrganisation(), directoryName);
+ return this;
+ }
+
+
+ public DescriptorBuilderForTesting withPrimaryLicenseAndThirdPartyOrgInDirectory(String directoryName) {
+ addDirectory(getPrimaryLicense(), getThirdPartyOrg(), directoryName);
+ return this;
+ }
+
+ public Organisation primaryOrganisation() {
+ return new Organisation(getPrimaryOrgName(), "primary.org", "http://example.org");
+ }
+
+ public Descriptor build() {
+ return new Descriptor(primaryLicense, primaryCopyrightNotice, primaryOrgName,
+ primaryNotice, licenses, notices, organisations, contents);
+ }
+
+ public String getResourceName() {
+ return resourceName;
+ }
+
+ public String getSecondaryCopyright() {
+ return secondaryCopyright;
+ }
+
+ public String getPrimaryOrgName() {
+ return primaryOrgName;
+ }
+
+ public Organisation getThirdPartyOrg() {
+ return thirdPartyOrg;
+ }
+
+ public License getPrimaryLicense() {
+ return primaryLicense;
+ }
+
+ public String getPrimaryLicenseText() {
+ return primaryLicenseText;
+ }
+
+ public void addDirectory(License license, final Organisation org,
+ final String directoryName) {
+ final WithinDirectory withinDirectory = buildDirectory(license, org,
+ directoryName);
+ contents.add(withinDirectory);
+ }
+
+ private Collection<Resource> buildResources() {
+ String noticeId = "notice:id";
+ notices.put(noticeId, "Some notice text");
+ Collection<Resource> resources = new ArrayList<Resource>();
+ String source = "";
+ resources.add(new Resource(resourceName, noticeId, source));
+ return resources;
+ }
+
+ private WithinDirectory buildDirectory(License license,
+ final Organisation org, final String directoryName) {
+ Collection<ByOrganisation> byOrgs = new ArrayList<ByOrganisation>();
+ Collection<Resource> resources = buildResources();
+ byOrgs.add(new ByOrganisation(org, resources));
+
+ Collection<WithLicense> withLicenses = new ArrayList<WithLicense>();
+ Map<String, String> params = Collections.emptyMap();
+ withLicenses.add(new WithLicense(license, secondaryCopyright, params, byOrgs));
+
+ Collection<ByOrganisation> publicDomain = Collections.emptyList();
+
+ final WithinDirectory withinDirectory = new WithinDirectory(directoryName, withLicenses, publicDomain);
+ return withinDirectory;
+ }
+
+ public DescriptorBuilderForTesting withPrimaryCopyrightNotice() {
+ return withPrimaryCopyrightNotice(A_PRIMARY_COPYRIGHT_NOTICE);
+ }
+
+
+ public DescriptorBuilderForTesting withPrimaryCopyrightNotice(String primaryCopyrightNotice) {
+ this.primaryCopyrightNotice = primaryCopyrightNotice;
+ return this;
+ }
+
+}
diff --git a/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestLicenseGeneration.java b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestLicenseGeneration.java
index c742ff3..5b9a4a9 100644
--- a/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestLicenseGeneration.java
+++ b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestLicenseGeneration.java
@@ -40,26 +40,14 @@
StringResultWriterFactory writerFactory;
VelocityEngine subject;
- String primaryLicenseText = "This is the primary license text";
- Organisation thirdPartyOrg = new Organisation("third-party", "thirdparty.org", "http://thirdparty.org");
- License primaryLicense = new License(false, primaryLicenseText, Collections.<String> emptyList(), "example.org", "http://example.org", "Example License");
- String primaryOrg = "example.org";
- String primaryNotice = "The primary notice.";
- Collection<WithinDirectory> contents = new ArrayList<WithinDirectory>();
- Map<String, License> licenses = new HashMap<String, License>();
- Map<String, String> notices = new HashMap<String, String>();
- Map<String, Organisation> organisations = new HashMap<String, Organisation>();
- String secondaryCopyright;
- String resourceName;
+ DescriptorBuilderForTesting builder;
@Override
protected void setUp() throws Exception {
super.setUp();
- resourceName = "resource";
- secondaryCopyright = "Copyright (c) this is secondary";
writerFactory = new StringResultWriterFactory();
subject = new VelocityEngine(new EmptyLog());
- primaryLicense.storeIn(licenses);
+ builder = new DescriptorBuilderForTesting();
}
@Override
@@ -67,58 +55,21 @@
super.tearDown();
}
- private void addDirectory(License license, final Organisation org,
- final String directoryName) {
- final WithinDirectory withinDirectory = buildDirectory(license, org,
- directoryName);
- contents.add(withinDirectory);
- }
-
- private Collection<Resource> buildResources() {
- String noticeId = "notice:id";
- notices.put(noticeId, "Some notice text");
- Collection<Resource> resources = new ArrayList<Resource>();
- String source = "";
- resources.add(new Resource(resourceName, noticeId, source));
- return resources;
- }
-
- private WithinDirectory buildDirectory(License license,
- final Organisation org, final String directoryName) {
- Collection<ByOrganisation> byOrgs = new ArrayList<ByOrganisation>();
- Collection<Resource> resources = buildResources();
- byOrgs.add(new ByOrganisation(org, resources));
-
- Collection<WithLicense> withLicenses = new ArrayList<WithLicense>();
- Map<String, String> params = Collections.emptyMap();
- withLicenses.add(new WithLicense(license, secondaryCopyright, params, byOrgs));
-
- Collection<ByOrganisation> publicDomain = Collections.emptyList();
-
- final WithinDirectory withinDirectory = new WithinDirectory(directoryName, withLicenses, publicDomain);
- return withinDirectory;
- }
public void testThatWhenThereAreNoThirdPartyContentsFooterIsNotShown() throws Exception {
- Descriptor work =
- new Descriptor(primaryLicense, primaryOrg, primaryNotice,
- licenses, notices, organisations, contents);
+ Descriptor work = builder.build();
subject.generate(work, writerFactory);
assertTrue("Check that work is suitable for this test", work.isPrimaryOnly());
assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
assertEquals("When no third party contents, expect that only the license text is output",
- primaryLicenseText,
+ builder.getPrimaryLicenseText(),
writerFactory.firstOutputFor(Result.LICENSE).trim());
}
public void testThatFooterIsShownWhenThereAreThirdPartyContents() throws Exception {
- addDirectory(primaryLicense, thirdPartyOrg, "lib");
-
- Descriptor work =
- new Descriptor(primaryLicense, primaryOrg, primaryNotice,
- licenses, notices, organisations, contents);
+ Descriptor work = builder.withPrimaryLicenseAndThirdPartyOrgInDirectory("lib").build();
assertFalse("Check that work is suitable for this test", work.isPrimaryOnly());
@@ -131,21 +82,18 @@
}
public void testSecondaryCopyrightNoticeForPrimaryLicense() throws Exception {
- final String primaryCopyrightNotice = "Copyright (c) this is primary";
- addDirectory(primaryLicense, new Organisation(primaryOrg, "primary.org", "http://example.org"), "lib");
- Descriptor work =
- new Descriptor(primaryLicense, primaryCopyrightNotice, primaryOrg, primaryNotice,
- licenses, notices, organisations, contents);
+ Descriptor work = builder.withPrimaryCopyrightNotice().withPrimaryLicenseAndOrgInDirectory("lib").build();
subject.generate(work, writerFactory);
assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
assertTrue("Expect secondary copyright to be presented: " + writerFactory.firstOutputFor(Result.LICENSE),
StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
- secondaryCopyright));
+ builder.getSecondaryCopyright()));
assertTrue("Expect resource to be indicated: " + writerFactory.firstOutputFor(Result.LICENSE),
StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
- resourceName));
+ builder.getResourceName()));
}
+
}