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/22 21:43:25 UTC

svn commit: r1222442 - 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: Thu Dec 22 20:43:25 2011
New Revision: 1222442

URL: http://svn.apache.org/viewvc?rev=1222442&view=rev
Log:
Move collect contents to builder

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=1222442&r1=1222441&r2=1222442&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 Thu Dec 22 20:43:25 2011
@@ -367,7 +367,7 @@ public class JDomBuilder {
     /**
      * Collects notices in the given documents.
      * @param document, not null
-     * @return notices indexed by id, not null possibly empty
+     * @return notices indexed by id, immutable, not null, possibly empty
      */
     public Map<String, String> mapNotices(Document document) {
         final Map<String, String> results = new HashMap<String, String>();
@@ -416,5 +416,31 @@ public class JDomBuilder {
             result = primaryOrganisationElement.getAttributeValue("id");
         }
         return result;
+    }    
+
+    private WithinDirectory directory(final Element element, final Map<String, License> licenses,
+            final Map<String, Organisation> organisations) {
+        return new JDomBuilder().withinDirectory(element, licenses, organisations);
+    }
+
+    /**
+     * Collects contents of the document.
+     * @param document not null
+     * @return not null, possibly empty
+     * @throws DuplicateElementException when dir names are not unique
+     */
+    public Collection<WithinDirectory> collectContents(final Document document, final Map<String, License> licenses,
+            final Map<String, Organisation> organisations) throws DuplicateElementException {
+        final Collection<WithinDirectory> results = new TreeSet<WithinDirectory>();
+        @SuppressWarnings("unchecked")
+        final List<Element> children = document.getRootElement().getChildren("within");
+        for (Element element: children) {
+            if (results.add(directory(element, licenses, organisations))) {
+                // fine
+            } else {
+                throw new DuplicateElementException("Duplicate parameter '" + element.getAttribute("dir").getValue() + "'");
+            }
+        }
+        return 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=1222442&r1=1222441&r2=1222442&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 Thu Dec 22 20:43:25 2011
@@ -18,18 +18,13 @@
  */
 package org.apache.rat.whisker.out;
 
-import java.util.Collection;
-import java.util.List;
 import java.util.Map;
-import java.util.TreeSet;
 
 import org.apache.rat.whisker.fromxml.JDomBuilder;
 import org.apache.rat.whisker.model.ByOrganisation;
 import org.apache.rat.whisker.model.License;
 import org.apache.rat.whisker.model.Organisation;
-import org.apache.rat.whisker.model.WithinDirectory;
 import org.jdom.Document;
-import org.jdom.Element;
 
 /**
  * 
@@ -37,7 +32,6 @@ import org.jdom.Element;
 public class Work {
 
     
-    private final Document document;
     private final License primaryLicense;
     private final String primaryOrganisationId;
     private final String primaryNotice;
@@ -50,7 +44,6 @@ public class Work {
      */
     public Work(Document document) {
         super();
-        this.document = document;
         this.licenses = new JDomBuilder().mapLicenses(document);
         this.notices = new JDomBuilder().mapNotices(document);
         this.primaryLicense = new JDomBuilder().primaryLicense(document, this.licenses);
@@ -74,20 +67,7 @@ public class Work {
     public License getPrimaryLicense() {
         return primaryLicense;
     }
-    
-    @SuppressWarnings("unchecked")
-    public Collection<WithinDirectory> getContents() {
-        final Collection<WithinDirectory> results = new TreeSet<WithinDirectory>();
-        for (Element element: (List<Element>)document.getRootElement().getChildren("within")) {
-            if (results.add(directory(element))) {
-                // OK System.out.println("Ok");
-            } else {
-                throw new IllegalArgumentException("Duplicate directory " + element.getAttribute("dir"));
-            }
-        }
-        return results;
-    }
-    
+        
     public boolean isPrimary( final License license) {
         return this.primaryLicense.equals(license);
     }
@@ -96,8 +76,4 @@ public class Work {
         return byOrganisation.getId().equals(primaryOrganisationId);
     }
     
-    private WithinDirectory directory(final Element element) {
-        return new JDomBuilder().withinDirectory(element, this.licenses, this.organisations);
-    }
-
 }

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=1222442&r1=1222441&r2=1222442&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 Thu Dec 22 20:43:25 2011
@@ -20,14 +20,17 @@ package org.apache.rat.whisker.fromxml;
 
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 
 import junit.framework.TestCase;
 
 import org.apache.rat.whisker.model.License;
 import org.apache.rat.whisker.model.Organisation;
+import org.apache.rat.whisker.model.WithinDirectory;
 import org.jdom.CDATA;
 import org.jdom.Document;
 import org.jdom.Element;
@@ -276,4 +279,58 @@ public class JDomBuilderWorkTest extends
         assertEquals("When set, builder should find value", idValue, result);
     }
     
+    public void testCollectContentsReturneEmptyWhenDocumentHasNoContents() throws Exception {
+        final Collection<WithinDirectory> results = subject.collectContents(
+                new Document().setRootElement(new Element("manifest")), new HashMap<String, License>(),
+                new HashMap<String, Organisation>());
+        assertNotNull("Builder should build something", results);
+        assertTrue("Collection should be empty when there are no contents", results.isEmpty());
+    }
+    
+    
+    public void testCollectDirectoriesDefinedInDocument() throws Exception {
+        for (int i=1;i<256;i++) {
+            checkCollectDirectoriesWith(i);
+        };
+    }
+
+    /**
+     * @param numberOfDirectories
+     */
+    private void checkCollectDirectoriesWith(final int numberOfDirectories) {
+        final String baseDir = "/dir/path";
+        final Document in = new Document();
+        final Element rootElement = new Element("manifest");
+        in.setRootElement(rootElement);
+        for (int i=0;i<numberOfDirectories;i++) {
+            rootElement.addContent(
+                    new Element("within").setAttribute("dir", combine(baseDir, i)));
+        }
+        final Collection<WithinDirectory> results = subject.collectContents(in, new HashMap<String, License>(),
+                new HashMap<String, Organisation>());
+        assertEquals("One organisation in map for each in the document", numberOfDirectories, results.size());
+        final Collection<String> dirNames = new HashSet<String>();
+        for (final WithinDirectory within:results) {
+            dirNames.add(within.getName());
+        }
+        for (int i=0;i<numberOfDirectories;i++) {
+            assertTrue("", dirNames.contains(combine(baseDir,i)));
+        }
+    }
+    
+    public void testCollectDirectoriesThrowsDuplicateElementExceptionWhenDirAttributeDuplicated() throws Exception {
+        final String dir = "duplicate/path";
+        try {
+            subject.collectContents(
+                    new Document().setRootElement(
+                            new Element("manifest").addContent(
+                                    new Element("within").setAttribute("dir", dir)).addContent(
+                                            new Element("within").setAttribute("dir", dir))), 
+                    new HashMap<String, License>(),
+                    new HashMap<String, Organisation>());
+            fail("Collect should throw a DuplicateElementException when dir names are not unique");
+        } catch (DuplicateElementException e) {
+            // expected
+        }
+    }
 }