WHISKER-7 fixed corner case uncovered by integration test

git-svn-id: https://svn.apache.org/repos/asf/creadur/whisker/trunk@1409385 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/apache-whisker-plugin4maven/src/it/example-copyright-notices/verify.groovy b/apache-whisker-plugin4maven/src/it/example-copyright-notices/verify.groovy
index c913c6f..31501ad 100644
--- a/apache-whisker-plugin4maven/src/it/example-copyright-notices/verify.groovy
+++ b/apache-whisker-plugin4maven/src/it/example-copyright-notices/verify.groovy
@@ -40,7 +40,7 @@
      license.expectThat(noLineContains("Copyright (c) 9595 The Example Project"))
      license.expectThat(noLineContains("This product includes software developed at"))
      license.expectThat(noLineContains("The Example Foundation (http://example.org/)."))
-     //license.expectThat(aLineContainsResource("apache.txt"))
+     license.expectThat(aLineContainsResource("apache.txt"))
      notice = noticeIn(basedir)
      notice.expectThat(aLineContains("Copyright (c) 9595 The Example Project"))
      notice.expectThat(aLineContains("This product includes software developed at"))
diff --git a/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/RenderingHelper.java b/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/RenderingHelper.java
new file mode 100644
index 0000000..9ae62cd
--- /dev/null
+++ b/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/RenderingHelper.java
@@ -0,0 +1,89 @@
+/**
+ * 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 org.apache.creadur.whisker.model.ByOrganisation;
+import org.apache.creadur.whisker.model.Descriptor;
+import org.apache.creadur.whisker.model.WithLicense;
+
+/**
+ * Factors out rendering logic from template.
+ */
+public class RenderingHelper {
+
+    /**
+     * The work being rendered, not null
+     */
+    private final Descriptor work;
+
+    /**
+     * Constructs a helper for the given work.
+     * @param work not null
+     */
+    public RenderingHelper(final Descriptor work) {
+        super();
+        this.work = work;
+    }
+
+    /**
+     * Should resources with the given license by
+     * the given organisation be rendered?
+     * @param organisation not null
+     * @param license not null
+     * @return true when resources should be rendered,
+     * false otherwise
+     */
+    public boolean renderResources(final ByOrganisation organisation,
+            final WithLicense license) {
+        return isNot(
+                primary(organisation)) ||
+                isNot(primary(license)) ||
+                license.hasCopyrightNotice();
+    }
+
+    /**
+     * Is this license the primary license for the work?
+     * @param license not null
+     * @return true when this is the primary license
+     * for the work, false otherwise
+     */
+    private boolean primary(final WithLicense license) {
+        return work.isPrimary(license.getLicense());
+    }
+
+    /**
+     * Is this the primary organisation?
+     * @param organisation true when this is the primary license,
+     * false otherwise
+     * @return
+     */
+    private boolean primary(final ByOrganisation organisation) {
+        return work.isPrimary(organisation);
+    }
+
+    /**
+     * Negates claim.
+     * @param claim to be negated
+     * @return true when claim is false,
+     * false when true
+     */
+    public boolean isNot(boolean claim) {
+        return !claim;
+    }
+}
\ No newline at end of file
diff --git a/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityReports.java b/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityReports.java
index 471784d..4548b12 100644
--- a/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityReports.java
+++ b/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityReports.java
@@ -224,6 +224,7 @@
         final VelocityContext context = new VelocityContext();
         context.put("work", work);
         context.put("indent", new Indentation());
+        context.put("helper", new RenderingHelper(work));
         return context;
     }
 
diff --git a/apache-whisker-velocity/src/main/resources/org/apache/creadur/whisker/template/velocity/license.vm b/apache-whisker-velocity/src/main/resources/org/apache/creadur/whisker/template/velocity/license.vm
index 24dfe1c..cf0cc05 100644
--- a/apache-whisker-velocity/src/main/resources/org/apache/creadur/whisker/template/velocity/license.vm
+++ b/apache-whisker-velocity/src/main/resources/org/apache/creadur/whisker/template/velocity/license.vm
@@ -52,7 +52,7 @@
 $indent.indent(12,$license.CopyrightNotice) $indent.indent(12, $license.Text)
 #end
 #foreach( $organisation in $license.Organisations)
-#if ( !$work.isPrimary( $organisation ) || $license.hasCopyrightNotice() )
+#if ( $helper.renderResources( $organisation , $license  ) )
         from $organisation.Name #if ($organisation.URL) $organisation.URL #end
 
 #foreach( $resource in $organisation.Resources)
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
index d8715c2..89958bf 100644
--- 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
@@ -46,13 +46,12 @@
     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 secondaryCopyright = null;
     String resourceName;
     String primaryCopyrightNotice = null;
 
     public DescriptorBuilderForTesting() {
         resourceName = "resource";
-        secondaryCopyright = "Copyright (c) this is secondary";
         primaryLicense.storeIn(licenses);
     }
 
@@ -148,4 +147,13 @@
         return this;
     }
 
+    public DescriptorBuilderForTesting withSecondaryCopyrightNotice() {
+        return withSecondaryCopyrightNotice("Copyright (c) this is secondary");
+    }
+
+    public DescriptorBuilderForTesting withSecondaryCopyrightNotice(final String secondaryCopyright) {
+        this.secondaryCopyright = secondaryCopyright;
+        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 09aab48..7358bf0 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
@@ -70,9 +70,11 @@
     }
 
     public void testSecondaryCopyrightNoticeForPrimaryLicense() throws Exception {
-        Descriptor work = builder.withPrimaryCopyrightNotice().withPrimaryLicenseAndOrgInDirectory("lib").build();
-
-        subject.generate(work, writerFactory);
+        subject.generate(
+                builder
+                .withSecondaryCopyrightNotice()
+                .withPrimaryCopyrightNotice()
+                .withPrimaryLicenseAndOrgInDirectory("lib").build(), writerFactory);
 
         assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
         assertTrue("Expect secondary copyright to be presented: " + writerFactory.firstOutputFor(Result.LICENSE),
@@ -88,12 +90,41 @@
                         builder.getResourceName()));
     }
 
+    private void verifyThatResourceNameIsNotWritten() {
+        assertFalse("Expect resource to be indicated: " + writerFactory.firstOutputFor(Result.LICENSE),
+                StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
+                        builder.getResourceName()));
+    }
+
     public void testPrimaryOrganisationSecondaryLicense() throws Exception {
         subject.generate(
                 builder.withSecondaryLicensePrimaryOrganisationInDirectory("lib").build(),
                 writerFactory);
         assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
+
         verifyThatResourceNameIsWritten();
+
+    }
+
+    public void testIgnorePrimaryOrganisationPrimaryLicense() throws Exception {
+        subject.generate(
+                builder.withPrimaryLicenseAndOrgInDirectory("lib").build(),
+                writerFactory);
+        assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
+        verifyThatResourceNameIsNotWritten();
+
+    }
+
+    public void testIgnorePrimaryOrganisationPrimaryLicensePrimaryCopyrightNotice() throws Exception {
+        subject.generate(
+                builder
+                    .withPrimaryCopyrightNotice()
+                    .withPrimaryLicenseAndOrgInDirectory("lib")
+                    .build(),
+                writerFactory);
+        assertEquals("Only one request for LICENSE writer", 1, writerFactory.requestsFor(Result.LICENSE));
+        verifyThatResourceNameIsNotWritten();
+
     }
 
 }