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/11/29 20:09:02 UTC

svn commit: r1208029 - in /incubator/rat/whisker/trunk/apache-whisker-xml/src: main/java/org/apache/rat/whisker/fromxml/ test/java/org/apache/rat/whisker/fromxml/

Author: rdonkin
Date: Tue Nov 29 19:09:01 2011
New Revision: 1208029

URL: http://svn.apache.org/viewvc?rev=1208029&view=rev
Log:
Redfactor to increase code reuse and switch to more specific exception type.

Added:
    incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/UnexpectedElementException.java   (with props)
    incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderOrganisationTest.java   (with props)
    incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderResourceTest.java
      - copied, changed from r1208019, incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderTest.java
Removed:
    incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderTest.java
Modified:
    incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/JDomBuilder.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=1208029&r1=1208028&r2=1208029&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 Nov 29 19:09:01 2011
@@ -19,6 +19,7 @@
 package org.apache.rat.whisker.fromxml;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.rat.whisker.model.Organisation;
 import org.apache.rat.whisker.model.Resource;
 import org.jdom.Element;
 
@@ -27,19 +28,49 @@ import org.jdom.Element;
  */
 public class JDomBuilder {
     
+    /** Names the element representing an organisation */
+    private static final String ORGANISATION_ELEMENT_NAME = "organisation";
+    /** Names the element representing a resource */
+    private static final String RESOURCE_ELEMENT_NAME = "resource";
+
     /**
      * Builds a resource.
      * @param element not null
      * @return built resource, not null
-     * @throws IllegalArgumentException when element is not named 'resource'
+     * @throws UnexpectedElementException when element is not named 'resource'
      */
-    public Resource resource(Element element) {
-        if ("resource".equals(element.getName())) {
+    public Resource resource(Element element) throws UnexpectedElementException {
+        if (RESOURCE_ELEMENT_NAME.equals(element.getName())) {
             return new Resource(StringUtils.trim(element.getAttributeValue("name")), 
                     StringUtils.trim(element.getAttributeValue("notice")),
                     StringUtils.trim(element.getAttributeValue("source")));
         } else {
-            throw new IllegalArgumentException("Expected resource element but was " + element.getName());
+            throw unexpectedElementException(element, RESOURCE_ELEMENT_NAME);
+        }
+    }
+
+    /**
+     * Builds a suitable exception when the element name is unexpected.
+     * @param element, not null
+     * @param expectedElement, not null
+     * @return a suitable exception, not null
+     */
+    private UnexpectedElementException unexpectedElementException(Element element,
+            final String expectedElement) {
+        return new UnexpectedElementException(expectedElement, element.getName());
+    }
+
+    /**
+     * Builds an organisation model from xml.
+     * @param element, not null
+     * @return {@link Organisation} not null
+     * @throws UnexpectedElementException when element is not named 'organisation'
+     */
+    public Organisation organisation(Element element) throws UnexpectedElementException {
+        if (ORGANISATION_ELEMENT_NAME.equals(element.getName())) {
+            return null;
+        } else {
+            throw unexpectedElementException(element, ORGANISATION_ELEMENT_NAME);
         }
     }
 

Added: incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/UnexpectedElementException.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/UnexpectedElementException.java?rev=1208029&view=auto
==============================================================================
--- incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/UnexpectedElementException.java (added)
+++ incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/UnexpectedElementException.java Tue Nov 29 19:09:01 2011
@@ -0,0 +1,58 @@
+/**
+ * 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.rat.whisker.fromxml;
+
+/**
+ * Indicates that an unexpected element occured in the xml.
+ */
+public class UnexpectedElementException extends IllegalArgumentException {
+
+    /**  */
+    private static final long serialVersionUID = -4801232871203301470L;
+
+    private final String expectedElement;
+    private final String actualElement;
+    
+    /**
+     * Constructs an instance.
+     * @param expectedElement names the element expected not null
+     * @param actualElement names the element that occured not null
+     */
+    public UnexpectedElementException(final String expectedElement, final String actualElement) {
+        super("Expected '"  + expectedElement + "' element but was " + actualElement);
+        this.expectedElement = expectedElement;
+        this.actualElement = actualElement;
+    }
+
+    /**
+     * Gets the name of the element that was expected.
+     * @return not null
+     */
+    public String getExpectedElement() {
+        return expectedElement;
+    }
+
+    /**
+     * Gets the name of the element that occured.
+     * @return not null
+     */
+    public String getActualElement() {
+        return actualElement;
+    }
+}

Propchange: incubator/rat/whisker/trunk/apache-whisker-xml/src/main/java/org/apache/rat/whisker/fromxml/UnexpectedElementException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderOrganisationTest.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderOrganisationTest.java?rev=1208029&view=auto
==============================================================================
--- incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderOrganisationTest.java (added)
+++ incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderOrganisationTest.java Tue Nov 29 19:09:01 2011
@@ -0,0 +1,56 @@
+/**
+ * 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.rat.whisker.fromxml;
+
+import org.jdom.Element;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ */
+public class JDomBuilderOrganisationTest extends TestCase {
+    private JDomBuilder subject;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        subject = new JDomBuilder();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testThrowIllegalArgumentWhenResourceIsNotOrganisation() throws Exception {
+        try {
+            subject.organisation(
+                new Element("bogus")
+                    .setAttribute("name", "name")
+                    .setAttribute("url", "url")
+                    .setAttribute("id", "id"));
+            fail("Expected IllegalArgument throw when elements is not named 'organisation'");  
+        } catch (UnexpectedElementException e) {
+            //expected
+        } catch (Throwable t) {
+            fail("Expected IllegalArgument throw when elements is not named 'organisation' but " + t + " was instead");
+        }
+    }
+}

Propchange: incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderOrganisationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderResourceTest.java (from r1208019, incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderTest.java)
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderResourceTest.java?p2=incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderResourceTest.java&p1=incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderTest.java&r1=1208019&r2=1208029&rev=1208029&view=diff
==============================================================================
--- incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderTest.java (original)
+++ incubator/rat/whisker/trunk/apache-whisker-xml/src/test/java/org/apache/rat/whisker/fromxml/JDomBuilderResourceTest.java Tue Nov 29 19:09:01 2011
@@ -26,7 +26,7 @@ import junit.framework.TestCase;
 /**
  * 
  */
-public class JDomBuilderTest extends TestCase {
+public class JDomBuilderResourceTest extends TestCase {
 
     private JDomBuilder subject;
     
@@ -89,7 +89,7 @@ public class JDomBuilderTest extends Tes
                     .setAttribute("notice", "notice")
                     .setAttribute("source", "source"));
             fail("Expected IllegalArgument throw when elements is not named 'resource'");  
-        } catch (IllegalArgumentException e) {
+        } catch (UnexpectedElementException e) {
             //expected
         } catch (Throwable t) {
             fail("Expected IllegalArgument throw when elements is not named 'resource' but " + t + " was instead");