Author: gbailleul
Date: Tue Jun 18 09:42:11 2013
New Revision: 1494083

URL: http://svn.apache.org/r1494083
Log:
PDFBOX-1540: fix errors on counts attributes
add test on the xml generator

Added:
    
pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestXmlResultParser.java
Modified:
    
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/ValidationResult.java
    
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java

Modified: 
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/ValidationResult.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/ValidationResult.java?rev=1494083&r1=1494082&r2=1494083&view=diff
==============================================================================
--- 
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/ValidationResult.java
 (original)
+++ 
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/ValidationResult.java
 Tue Jun 18 09:42:11 2013
@@ -78,7 +78,7 @@ public class ValidationResult
      * Create a Validation Result object. This constructor force the isValid 
to false and add all the given errors to
      * the list or ValidationErrors.
      * 
-     * @param error
+     * @param errors
      *            if error is null, no error is added to the list.
      */
     public ValidationResult(List<ValidationError> errors)
@@ -333,7 +333,12 @@ public class ValidationResult
         {
             this.isWarning = isWarning;
         }
-        
+
+        @Override
+        public int hashCode() {
+            return errorCode.hashCode();
+        }
+
         public boolean equals (Object o) {
             if (o instanceof ValidationError) {
                 ValidationError ve = (ValidationError)o;
@@ -342,6 +347,8 @@ public class ValidationResult
                     return false;
                 } else if (!errorCode.equals(ve.errorCode)) {
                     return false;
+                }  else if (!details.equals(ve.details)) {
+                    return false;
                 }
                 // check warning
                 return isWarning==ve.isWarning;

Modified: 
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java?rev=1494083&r1=1494082&r2=1494083&view=diff
==============================================================================
--- 
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java
 (original)
+++ 
pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/parser/XmlResultParser.java
 Tue Jun 18 09:42:11 2013
@@ -24,7 +24,6 @@ package org.apache.pdfbox.preflight.pars
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -94,58 +93,54 @@ public class XmlResultParser
         } else {
             Element preflight = generateResponseSkeleton(rdocument, 
source.getName(), after-before);
             // valid ?
-            Element valid = rdocument.createElement("isValid");
-            valid.setAttribute("type", pdfType);
-            valid.setTextContent("false");
-            preflight.appendChild(valid);
-            // errors list
-            List<ValidationError> cleaned = 
cleanErrorList(result.getErrorsList());
-            Element errors = rdocument.createElement("errors");
-            errors.setAttribute("count", String.format("%d", cleaned.size()));
-            preflight.appendChild(errors);
-            Map<ValidationError, Integer> errorsByType = new 
HashMap<ValidationError, Integer>();
-            for (ValidationError validationError : cleaned)
-            {
-                Integer count = errorsByType.get(validationError);
-                if (count==null) {
-                    errorsByType.put(validationError, 1);
-                } else {
-                    errorsByType.put(validationError, count++);
-                }
-            }
-            
-            for (Map.Entry<ValidationError,Integer> entry : 
errorsByType.entrySet())
-            {
-                Element error = rdocument.createElement("error");
-                error.setAttribute("count", 
String.format("%d",entry.getValue().intValue()));
-                Element code = rdocument.createElement("code");
-                code.setTextContent(entry.getKey().getErrorCode());
-                error.appendChild(code);
-                Element detail = rdocument.createElement("details");
-                detail.setTextContent(entry.getKey().getDetails());
-                error.appendChild(detail);
-                errors.appendChild(error);
-            }
+            createResponseWithError(rdocument, pdfType, result, preflight);
             return preflight;
         }
 
     }
 
-    private List<ValidationError> cleanErrorList (List<ValidationError> 
errors) {
-        List<ValidationError> cleaned = new 
ArrayList<ValidationResult.ValidationError>(errors.size());
-        List<String> already = new ArrayList<String>(errors.size());
-        for (ValidationError validationError : errors)
+    protected void createResponseWithError(Document rdocument, String pdfType, 
ValidationResult result, Element preflight) {
+        Element valid = rdocument.createElement("isValid");
+        valid.setAttribute("type", pdfType);
+        valid.setTextContent("false");
+        preflight.appendChild(valid);
+        // errors list
+        Element errors = rdocument.createElement("errors");
+        Map<ValidationError, Integer> cleaned = 
cleanErrorList(result.getErrorsList());
+        preflight.appendChild(errors);
+        int totalCount = 0;
+        for (Map.Entry<ValidationError,Integer> entry : cleaned.entrySet())
         {
-            if (!already.contains(validationError.getErrorCode())) {
-                // first time
-                cleaned.add(validationError);
-                already.add(validationError.getErrorCode());
-            } // else already found
+            Element error = rdocument.createElement("error");
+            int count = entry.getValue().intValue();
+            error.setAttribute("count", String.format("%d",count));
+            totalCount += count;
+            Element code = rdocument.createElement("code");
+            code.setTextContent(entry.getKey().getErrorCode());
+            error.appendChild(code);
+            Element detail = rdocument.createElement("details");
+            detail.setTextContent(entry.getKey().getDetails());
+            error.appendChild(detail);
+            errors.appendChild(error);
+        }
+        errors.setAttribute("count", String.format("%d", totalCount));
+    }
+
+    private Map<ValidationError,Integer> cleanErrorList (List<ValidationError> 
errors) {
+        Map<ValidationError,Integer> cleaned = new HashMap<ValidationError, 
Integer>(errors.size());
+        for (ValidationError ve: errors) {
+            Integer found = cleaned.get(ve);
+            if (found!=null) {
+                cleaned.put(ve,found+1);
+            } else {
+                cleaned.put(ve,1);
+            }
+
         }
-        return cleaned;
+         return cleaned;
     }
 
-    private Element generateFailureResponse (Document rdocument, String 
name,long duration, String pdfType, Exception e) {
+    protected Element generateFailureResponse (Document rdocument, String 
name,long duration, String pdfType, Exception e) {
         Element preflight = generateResponseSkeleton(rdocument, name, 
duration);
         // valid ?
         Element valid = rdocument.createElement("isValid");
@@ -168,7 +163,7 @@ public class XmlResultParser
         return preflight;
     }
 
-    private Element generateResponseSkeleton (Document rdocument, String name, 
long duration) {
+    protected Element generateResponseSkeleton (Document rdocument, String 
name, long duration) {
         Element preflight = rdocument.createElement("preflight");
         preflight.setAttribute("name", name);
         // duration

Added: 
pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestXmlResultParser.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestXmlResultParser.java?rev=1494083&view=auto
==============================================================================
--- 
pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestXmlResultParser.java
 (added)
+++ 
pdfbox/trunk/preflight/src/test/java/org/apache/pdfbox/preflight/parser/TestXmlResultParser.java
 Tue Jun 18 09:42:11 2013
@@ -0,0 +1,117 @@
+/*****************************************************************************
+ *
+ * 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.pdfbox.preflight.parser;
+
+
+import org.apache.pdfbox.preflight.ValidationResult;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+
+public class TestXmlResultParser {
+
+    public static final String ERROR_CODE = "000";
+
+    protected XmlResultParser parser = new XmlResultParser();
+
+    protected Document document;
+
+    protected Element preflight;
+
+    protected XPath xpath;
+
+    @Before
+    public void before () throws Exception {
+        document = 
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+        preflight = parser.generateResponseSkeleton(document, "myname", 14);
+        xpath = XPathFactory.newInstance().newXPath();
+    }
+
+    @Test
+    public void testOneError () throws Exception {
+        ValidationResult result = new ValidationResult(false);
+        result.addError(new ValidationResult.ValidationError("7"));
+        parser.createResponseWithError(document, "pdftype", result, preflight);
+        Assert.assertNotNull(xpath.evaluate("errors[@count='1']", preflight, 
XPathConstants.NODE));
+        NodeList nl = (NodeList)xpath.evaluate("errors/error[@count='1']", 
preflight, XPathConstants.NODESET);
+        Assert.assertEquals(1,nl.getLength());
+    }
+
+    @Test
+    public void testTwoError () throws Exception {
+        ValidationResult result = new ValidationResult(false);
+        result.addError(new ValidationResult.ValidationError("7"));
+        result.addError(new ValidationResult.ValidationError(ERROR_CODE));
+        parser.createResponseWithError(document, "pdftype", result, preflight);
+        Assert.assertNotNull(xpath.evaluate("errors[@count='2']", preflight, 
XPathConstants.NODE));
+        NodeList nl = (NodeList)xpath.evaluate("errors/error[@count='1']", 
preflight, XPathConstants.NODESET);
+        Assert.assertEquals(2,nl.getLength());
+    }
+
+    @Test
+    public void testSameErrorTwice () throws Exception {
+        ValidationResult result = new ValidationResult(false);
+        result.addError(new ValidationResult.ValidationError(ERROR_CODE));
+        result.addError(new ValidationResult.ValidationError(ERROR_CODE));
+        parser.createResponseWithError(document,"pdftype",result,preflight);
+        Assert.assertNotNull(xpath.evaluate("errors[@count='2']", preflight, 
XPathConstants.NODE));
+        Assert.assertNotNull(xpath.evaluate("errors/error[@count='2']", 
preflight, XPathConstants.NODE));
+        Element code = 
(Element)xpath.evaluate("errors/error[@count='2']/code", preflight, 
XPathConstants.NODE);
+        Assert.assertNotNull(code);
+        Assert.assertEquals(ERROR_CODE,code.getTextContent());
+    }
+
+    @Test
+    public void testSameCodeWithDifferentMessages () throws Exception {
+        ValidationResult result = new ValidationResult(false);
+        result.addError(new 
ValidationResult.ValidationError(ERROR_CODE,"message 1"));
+        result.addError(new 
ValidationResult.ValidationError(ERROR_CODE,"message 2"));
+        parser.createResponseWithError(document, "pdftype", result, preflight);
+        Assert.assertNotNull(xpath.evaluate("errors[@count='2']", preflight, 
XPathConstants.NODE));
+        NodeList nl = (NodeList)xpath.evaluate("errors/error[@count='1']", 
preflight, XPathConstants.NODESET);
+        Assert.assertEquals(2,nl.getLength());
+    }
+
+
+//    private void dump (Element element) throws Exception {
+//        Transformer transformer = 
TransformerFactory.newInstance().newTransformer();
+//        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, 
"yes");
+//        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+//        
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount";, "4");
+//        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+//        transformer.transform(new DOMSource(element), new 
StreamResult(System.out));
+//        System.out.flush();
+//    }
+
+}


Reply via email to