You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by rd...@apache.org on 2011/12/20 22:41:02 UTC

svn commit: r1221496 - in /incubator/rat/whisker/trunk/apache-whisker-xml/src: main/java/org/apache/rat/whisker/fromxml/JDomBuilder.java main/java/org/apache/rat/whisker/out/Work.java test/java/org/apache/rat/whisker/fromxml/JDomBuilderWorkTest.java

Author: rdonkin
Date: Tue Dec 20 21:41:02 2011
New Revision: 1221496

URL: http://svn.apache.org/viewvc?rev=1221496&view=rev
Log:
Moved license mapping to builder, with testing

Modified:
    incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/JDomBuilder.java
    incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/out/Work.java
    incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderWorkTest.java

Modified: incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/JDomBuilder.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/JDomBuilder.java?rev=1221496&r1=1221495&r2=1221496&view=diff
==============================================================================
--- incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/JDomBuilder.java (original)
+++ incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/JDomBuilder.java Tue Dec 20 21:41:02 2011
@@ -302,7 +302,7 @@ public class JDomBuilder {
     }
 
     /**
-     * Collection organisation definitions within document.
+     * Collects organisation definitions within document.
      * @param document, not null
      * @return organisations indexed by id, not null possibly empty
      */
@@ -320,4 +320,23 @@ public class JDomBuilder {
         return Collections.unmodifiableMap(organisationsById);
     }
 
+    /**
+     * Collects license definitions within document.
+     * @param document, not null
+     * @return licenses, indexed by id, not null, possibly empty
+     */
+    public Map<String, License> mapLicenses(Document document) {
+        final Map<String, License> results = new HashMap<String, License>();
+        final Element licensesChild = document.getRootElement().getChild("licenses");
+        if (licensesChild != null) {
+            @SuppressWarnings("unchecked")
+            final List<Element> children = (List<Element>) licensesChild.getChildren();
+            for (final Element element: children) {
+                new JDomBuilder().license(element).storeIn(results);
+            }
+        }
+        return Collections.unmodifiableMap(results);
+
+    }
+
 }

Modified: incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/out/Work.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/out/Work.java?rev=1221496&r1=1221495&r2=1221496&view=diff
==============================================================================
--- incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/out/Work.java (original)
+++ incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/out/Work.java Tue Dec 20 21:41:02 2011
@@ -38,20 +38,6 @@ import org.jdom.Element;
  * 
  */
 public class Work {
-
-
-    /**
-     * @param document
-     * @return
-     */
-    @SuppressWarnings("unchecked")
-    private static Map<String, License> mapLicenses(Document document) {
-        Map<String, License> results = new HashMap<String, License>();
-        for (final Element element: (List<Element>) document.getRootElement().getChild("licenses").getChildren()) {
-            new JDomBuilder().license(element).storeIn(results);
-        }
-        return Collections.unmodifiableMap(results);
-    }
     
     
     /**
@@ -118,7 +104,7 @@ public class Work {
     public Work(Document document) {
         super();
         this.document = document;
-        this.licenses = mapLicenses(document);
+        this.licenses = new JDomBuilder().mapLicenses(document);
         this.notices = mapNotices(document);
         this.primaryLicense = primaryLicense(document, this.licenses);
         this.primaryNotice = primaryNotice(document);

Modified: incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderWorkTest.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderWorkTest.java?rev=1221496&r1=1221495&r2=1221496&view=diff
==============================================================================
--- incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderWorkTest.java (original)
+++ incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderWorkTest.java Tue Dec 20 21:41:02 2011
@@ -18,10 +18,12 @@
  */
 package org.apache.rat.whisker.fromxml;
 
+import java.util.ArrayList;
 import java.util.Map;
 
 import junit.framework.TestCase;
 
+import org.apache.rat.whisker.model.License;
 import org.apache.rat.whisker.model.Organisation;
 import org.jdom.Document;
 import org.jdom.Element;
@@ -44,6 +46,53 @@ public class JDomBuilderWorkTest extends
         super.tearDown();
     }
 
+    public void testMapLicensesIsEmptyWhenDocumentHasNoLicenses() throws Exception {
+        final Map<String, License> results = subject.mapLicenses(new Document().setRootElement(new Element("manifest")));
+        assertNotNull("Builder should build something", results);
+        assertTrue("Should be empty when no licenses present", results.isEmpty());
+    }
+    
+    public void testMapLicensesExpectedToBeImmutable() throws Exception {
+        final Map<String, License> results = 
+            subject.mapLicenses(new Document().setRootElement(new Element("manifest")));
+        assertNotNull("Expected builder to build something", results);
+        try {
+            results.put("whatever", new License(true, "", new ArrayList<String>(), "", "", ""));
+            fail("Expected map to be immutable");
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+    }
+    
+    public void testMapLicensesFindsLicensesDefinedInDocument() throws Exception {
+        for (int i=0; i<256; i++) {
+            checkMapLicensesWith(i);
+        }
+    }
+
+    /**
+     * @param numberOfLicenses
+     */
+    private void checkMapLicensesWith(final int numberOfLicenses) {
+        final String baseId = "ID";
+        final String baseName = "Name";
+        final Document in = new Document();
+        final Element licenses = new Element("licenses");
+        in.setRootElement(new Element("manifest").addContent(licenses));
+        for (int i=0;i<numberOfLicenses;i++) {
+            licenses.addContent(
+                    new Element("license").setAttribute("id", combine(baseId, i)).setAttribute("name", combine(baseName, i)));
+        }
+        final Map<String, License> results = subject.mapLicenses(in);
+        assertEquals("One license in map for each in the document", numberOfLicenses, results.size());
+        for (int i=0;i<numberOfLicenses;i++) {
+            final License next = results.get(combine(baseId, i));
+            assertNotNull("Expected organisation to be stored", next);
+            assertEquals("Expected correct organisation to be stored", next.getName(), combine(baseName, i));
+        }
+    }
+
+    
     public void testMapOrganisationsIsEmptyWhenDocumentHasNoOrganisations() throws Exception {
         final Map<String, Organisation> results = 
             subject.mapOrganisations(new Document().setRootElement(new Element("manifest")));