WHISKER-11 When configured, render source URLs

git-svn-id: https://svn.apache.org/repos/asf/creadur/whisker/trunk@1412678 13f79535-47bb-0310-9956-ffa450edef68
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
index 9ae62cd..9a8966c 100644
--- 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
@@ -18,8 +18,11 @@
  */
 package org.apache.creadur.whisker.out.velocity;
 
+import org.apache.commons.lang.StringUtils;
+import org.apache.creadur.whisker.app.Configuration;
 import org.apache.creadur.whisker.model.ByOrganisation;
 import org.apache.creadur.whisker.model.Descriptor;
+import org.apache.creadur.whisker.model.Resource;
 import org.apache.creadur.whisker.model.WithLicense;
 
 /**
@@ -33,12 +36,18 @@
     private final Descriptor work;
 
     /**
+     * Configuration for the rendering.
+     */
+    private final Configuration configuration;
+
+    /**
      * Constructs a helper for the given work.
      * @param work not null
      */
-    public RenderingHelper(final Descriptor work) {
+    public RenderingHelper(final Descriptor work, final Configuration configuration) {
         super();
         this.work = work;
+        this.configuration = configuration;
     }
 
     /**
@@ -86,4 +95,22 @@
     public boolean isNot(boolean claim) {
         return !claim;
     }
+
+    /**
+     * Renders the resource source URL
+     * based on configuration setting suitable
+     * for the license file.
+     * @param resource not null
+     * @return not null, possible empty string
+     */
+    public String sourceUrl(final Resource resource) {
+        final String result;
+        final String source = resource.getSource();
+        if (StringUtils.isBlank(source) || isNot(configuration.includeSourceURLsInLicense())) {
+            result = "";
+        } else {
+            result = " from " + source;
+        }
+        return result;
+    }
 }
\ No newline at end of file
diff --git a/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityEngine.java b/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityEngine.java
index c9ea91b..4633811 100644
--- a/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityEngine.java
+++ b/apache-whisker-velocity/src/main/java/org/apache/creadur/whisker/out/velocity/VelocityEngine.java
@@ -124,7 +124,7 @@
             final Descriptor work,
             final ResultWriterFactory writerFactory,
             final Configuration configuration) throws Exception {
-        reporter(writerFactory).generate(work);
+        reporter(writerFactory).generate(work, configuration);
         return this;
     }
 }
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 4548b12..5648357 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
@@ -25,6 +25,7 @@
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
+import org.apache.creadur.whisker.app.Configuration;
 import org.apache.creadur.whisker.app.ResultWriterFactory;
 import org.apache.creadur.whisker.app.analysis.LicenseAnalyst;
 import org.apache.creadur.whisker.model.Descriptor;
@@ -167,9 +168,11 @@
     /**
      * Reports on work.
      * @param work not null
+     * @param configuration not null
      * @throws Exception when generation fails
      */
-    public final void generate(final Descriptor work) throws Exception {
+    public final void generate(final Descriptor work,
+            final Configuration configuration) throws Exception {
         final List<Product> products = new ArrayList<Product>();
         for (Product product: PRODUCTS_THAT_GENERATE_LICENSING_MATERIALS) {
             switch (product) {
@@ -184,7 +187,7 @@
 
         }
         final Product[] pruductArray = new Product[products.size()];
-        merge(products.toArray(pruductArray), context(work));
+        merge(products.toArray(pruductArray), context(work, configuration));
     }
 
     /**
@@ -218,13 +221,15 @@
     /**
      * Creates a context, and loads it for descriptor work.
      * @param work not null
+     * @param configuration not null
      * @return not null
      */
-    private VelocityContext context(final Descriptor work) {
+    private VelocityContext context(final Descriptor work,
+            final Configuration configuration) {
         final VelocityContext context = new VelocityContext();
         context.put("work", work);
         context.put("indent", new Indentation());
-        context.put("helper", new RenderingHelper(work));
+        context.put("helper", new RenderingHelper(work, configuration));
         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 cf0cc05..55064a2 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
@@ -37,7 +37,7 @@
         by $organisation.Name #if ($organisation.URL) $organisation.URL #end
 
 #foreach( $resource in $organisation.Resources)
-            $resource.Name
+            $resource.Name $helper.sourceUrl($resource)
 #end
 #end
 #end
@@ -56,7 +56,7 @@
         from $organisation.Name #if ($organisation.URL) $organisation.URL #end
 
 #foreach( $resource in $organisation.Resources)
-            $resource.Name
+            $resource.Name $helper.sourceUrl($resource)
 #end
 #end
 #end
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 89958bf..95a9519 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
@@ -49,6 +49,7 @@
     String secondaryCopyright = null;
     String resourceName;
     String primaryCopyrightNotice = null;
+    public String sourceUrl = "";
 
     public DescriptorBuilderForTesting() {
         resourceName = "resource";
@@ -110,8 +111,7 @@
         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));
+        resources.add(new Resource(resourceName, noticeId, sourceUrl));
         return resources;
     }
 
@@ -156,4 +156,9 @@
         return this;
     }
 
+    public DescriptorBuilderForTesting withSourceURL() {
+        sourceUrl = "http://example.org/bogus";
+        return this;
+    }
+
 }
diff --git a/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestLicenseGenerationSourceURLs.java b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestLicenseGenerationSourceURLs.java
new file mode 100644
index 0000000..674f4ca
--- /dev/null
+++ b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestLicenseGenerationSourceURLs.java
@@ -0,0 +1,70 @@
+/**
+ * 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 static org.apache.creadur.whisker.app.ConfigurationBuilder.aConfiguration;
+import junit.framework.TestCase;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.creadur.whisker.app.Result;
+import org.apache.creadur.whisker.model.Descriptor;
+
+public class TestLicenseGenerationSourceURLs extends TestCase {
+
+    StringResultWriterFactory writerFactory;
+    VelocityEngine subject;
+    DescriptorBuilderForTesting builder;
+    Descriptor work;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        writerFactory = new StringResultWriterFactory();
+        subject = new VelocityEngine(new EmptyLog());
+        builder = new DescriptorBuilderForTesting().withSourceURL();
+        work = builder.withPrimaryLicenseAndThirdPartyOrgInDirectory(".").build();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testNoSourceUrlsConfiguration() throws Exception {
+        subject.generate(work, writerFactory, aConfiguration().noSourceURLsInLicense().build());
+        assertFalse(failureMessage(), outputContainsSourceUrl());
+
+    }
+
+    private boolean outputContainsSourceUrl() {
+        return StringUtils.contains(writerFactory.firstOutputFor(Result.LICENSE),
+                builder.sourceUrl);
+    }
+
+    private String failureMessage() {
+        return "Expect information when third party contents present: " + writerFactory.firstOutputFor(Result.LICENSE);
+    }
+
+    public void testWithSourceUrlsConfiguration() throws Exception {
+        subject.generate(work, writerFactory, aConfiguration().withSourceURLsInLicense().build());
+
+        assertTrue(failureMessage(), outputContainsSourceUrl());
+
+    }
+}
diff --git a/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestRenderingHelper.java b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestRenderingHelper.java
new file mode 100644
index 0000000..d5301d3
--- /dev/null
+++ b/apache-whisker-velocity/src/test/java/org/apache/creadur/whisker/out/velocity/TestRenderingHelper.java
@@ -0,0 +1,70 @@
+/**
+ * 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 static org.apache.creadur.whisker.app.ConfigurationBuilder.*;
+
+import org.apache.creadur.whisker.model.Descriptor;
+import org.apache.creadur.whisker.model.Resource;
+
+import junit.framework.TestCase;
+
+public class TestRenderingHelper extends TestCase {
+
+    private static final String A_SOURCE_URL = "http://example.org/sample";
+
+    RenderingHelper subject;
+
+    Resource resourceWithSourceUrl;
+    Resource resourceNoSourceUrl;
+
+    Descriptor work;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        resourceWithSourceUrl = new Resource("a name", "an:id", A_SOURCE_URL);
+        resourceNoSourceUrl = new Resource("a name", "an:id", null);
+    }
+
+    public void testSourceWithSourceUrlsConfiguration() {
+        subject = new RenderingHelper(work,
+                aConfiguration().withSourceURLsInLicense().build());
+        assertEquals(subject.sourceUrl(resourceWithSourceUrl), " from " + A_SOURCE_URL);
+    }
+
+    public void testSourceNoSourceUrlsConfiguration() {
+        subject = new RenderingHelper(work,
+                aConfiguration().noSourceURLsInLicense().build());
+        assertEquals(subject.sourceUrl(resourceWithSourceUrl), "");
+    }
+
+    public void testNoSourceWithSourceUrlsConfiguration() {
+        subject = new RenderingHelper(work,
+                aConfiguration().withSourceURLsInLicense().build());
+        assertEquals(subject.sourceUrl(resourceNoSourceUrl), "");
+    }
+
+    public void testNoSourceNoSourceUrlsConfiguration() {
+        subject = new RenderingHelper(work,
+                aConfiguration().noSourceURLsInLicense().build());
+        assertEquals(subject.sourceUrl(resourceNoSourceUrl), "");
+    }
+
+}