sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1496964 - in /sis/branches/JDK7/core: sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/ sis-utility/src/test/java/org/apache/sis/internal/test/ sis-utility/src/test/java/org/apache/sis/test/
Date Wed, 26 Jun 2013 15:25:36 GMT
Author: desruisseaux
Date: Wed Jun 26 15:25:36 2013
New Revision: 1496964

URL: http://svn.apache.org/r1496964
Log:
Be tolerant to JAXB implementation that do not insert the "gml:" prefix in element names.

Added:
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLTransformationTest.java
  (with props)
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTransformation.java
  (with props)
Modified:
    sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLComparatorTest.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java

Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java?rev=1496964&r1=1496963&r2=1496964&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
[UTF-8] Wed Jun 26 15:25:36 2013
@@ -25,6 +25,7 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.datatype.DatatypeConfigurationException;
 import org.apache.sis.internal.jaxb.XmlUtilities;
+import org.apache.sis.test.XMLTransformation;
 import org.apache.sis.test.XMLTestCase;
 import org.junit.BeforeClass;
 import org.junit.AfterClass;
@@ -99,13 +100,14 @@ public final strictfp class TimePeriodTe
     @Test
     public void testTimeInstant() throws JAXBException, DatatypeConfigurationException {
         createContext(false, Locale.FRANCE, "CET");
-        final String expected =
+        String expected =
             "<gml:TimeInstant>\n" +
             "  <gml:timePosition>1992-01-01T01:00:00.000+01:00</gml:timePosition>\n"
+
             "</gml:TimeInstant>\n";
         final TimeInstant instant = createTimeInstant("1992-01-01 00:00:00");
         marshaller.marshal(instant, buffer);
         final String actual = buffer.toString();
+        expected = XMLTransformation.GML.optionallyRemovePrefix(expected, actual);
         assertXmlEquals(expected, actual, "xmlns:*", "xsi:schemaLocation");
         final TimeInstant test = (TimeInstant) unmarshaller.unmarshal(new StringReader(actual));
         assertEquals("1992-01-01 00:00:00", format(XmlUtilities.toDate(test.timePosition)));
@@ -144,7 +146,7 @@ public final strictfp class TimePeriodTe
      * @param expected The expected string.
      */
     private void testPeriod(final TimePeriodBound begin, final TimePeriodBound end,
-            final String expected, final boolean verifyValues) throws JAXBException
+            String expected, final boolean verifyValues) throws JAXBException
     {
         createContext(false, Locale.FRANCE, "CET");
         final TimePeriod period = new TimePeriod();
@@ -152,6 +154,7 @@ public final strictfp class TimePeriodTe
         period.end   = end;
         marshaller.marshal(period, buffer);
         final String actual = buffer.toString();
+        expected = XMLTransformation.GML.optionallyRemovePrefix(expected, actual);
         assertXmlEquals(expected, actual, "xmlns:*", "xsi:schemaLocation");
         final TimePeriod test = (TimePeriod) unmarshaller.unmarshal(new StringReader(actual));
         if (verifyValues) {

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLComparatorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLComparatorTest.java?rev=1496964&r1=1496963&r2=1496964&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLComparatorTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLComparatorTest.java
[UTF-8] Wed Jun 26 15:25:36 2013
@@ -33,8 +33,7 @@ import static org.junit.Assert.*;
  */
 public final strictfp class XMLComparatorTest extends TestCase {
     /**
-     * Tests the {@link XMLComparator#ignoredAttributes} and {@link XMLComparator#ignoredNodes}
-     * sets.
+     * Tests the {@link XMLComparator#ignoredAttributes} and {@link XMLComparator#ignoredNodes}
sets.
      *
      * @throws Exception Should never happen.
      */

Added: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLTransformationTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLTransformationTest.java?rev=1496964&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLTransformationTest.java
(added)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLTransformationTest.java
[UTF-8] Wed Jun 26 15:25:36 2013
@@ -0,0 +1,94 @@
+/*
+ * 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.sis.internal.test;
+
+import java.util.regex.Matcher;
+import org.apache.sis.xml.Namespaces;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.apache.sis.test.XMLTransformation.GML;
+
+
+/**
+ * Tests {@link XMLTransformation}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+public final strictfp class XMLTransformationTest extends TestCase {
+    /**
+     * Tests the {@link org.apache.sis.test.XMLTransformation#GML} pattern
+     * with a string containing {@code xmlns:gml="…"}.
+     */
+    @Test
+    public void testPrefixGML() {
+        final Matcher matcher = GML.findPrefix.matcher("<gml:TimeInstant xmlns:gml=\""
+ Namespaces.GML + "\">");
+        assertTrue(matcher.matches());
+        assertEquals(1, matcher.groupCount());
+        assertEquals(":gml", matcher.group(1));
+    }
+
+    /**
+     * Tests the {@link org.apache.sis.test.XMLTransformation#GML} pattern
+     * with a string containing {@code xmlns="…"} (without {@code :gml}).
+     */
+    @Test
+    public void testPrefixMissing() {
+        final Matcher matcher = GML.findPrefix.matcher("<TimeInstant xmlns=\"" + Namespaces.GML
+ "\">");
+        assertTrue(matcher.matches());
+        assertEquals(1, matcher.groupCount());
+        assertNull(matcher.group(1));
+    }
+
+    /**
+     * Tests the {@link org.apache.sis.test.XMLTransformation#GML} pattern
+     * with a string containing {@code xmlns:gmd="…"}. We expect no match.
+     */
+    @Test
+    public void testPrefixMismatch() {
+        final Matcher matcher = GML.findPrefix.matcher("<gml:TimeInstant xmlns:gmd=\""
+ Namespaces.GMD + "\">");
+        assertFalse(matcher.matches());
+    }
+
+    /**
+     * Tests {@link org.apache.sis.test.XMLTransformation#optionallyRemovePrefix(String,
CharSequence)}.
+     */
+    @Test
+    @DependsOnMethod({
+        "testPrefixGML",
+        "testPrefixMissing",
+        "testPrefixMismatch"
+    })
+    public void testOptionallyRemovePrefix() {
+        final String expected =
+            "<gml:TimeInstant xmlns:gml=\"" + Namespaces.GML + "\">\n" +
+            "  <gml:timePosition>1992-01-01T01:00:00.000+01:00</gml:timePosition>\n"
+
+            "</gml:TimeInstant>\n";
+        assertSame(expected, GML.optionallyRemovePrefix(expected, expected));
+
+        final String actual =
+            "<TimeInstant xmlns=\"" + Namespaces.GML + "\">\n" +
+            "  <timePosition>1992-01-01T01:00:00.000+01:00</timePosition>\n"
+
+            "</TimeInstant>\n";
+        assertEquals(actual, GML.optionallyRemovePrefix(expected, actual));
+    }
+}

Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLTransformationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/test/XMLTransformationTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java?rev=1496964&r1=1496963&r2=1496964&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
[UTF-8] Wed Jun 26 15:25:36 2013
@@ -30,7 +30,6 @@ import java.io.IOException;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
-
 import org.w3c.dom.Attr;
 import org.w3c.dom.CDATASection;
 import org.w3c.dom.Comment;
@@ -41,7 +40,6 @@ import org.w3c.dom.NodeList;
 import org.w3c.dom.ProcessingInstruction;
 import org.w3c.dom.Text;
 import org.xml.sax.SAXException;
-
 import org.apache.sis.util.ArgumentChecks;
 
 import static java.lang.StrictMath.*;
@@ -57,11 +55,14 @@ import static org.apache.sis.util.CharSe
  * protected methods defined in this class, which can be overridden.
  *
  * <p>By default, this comparator expects the documents to contain the same elements
and
- * the same attributes (but the order of attributes may be different). However it is
- * possible to:</p>
+ * the same attributes (but the order of attributes may be different).
+ * However it is possible to:</p>
  *
  * <ul>
+ *   <li>Specify whether comments shall be ignored (see {@link #ignoreComments})</li>
  *   <li>Specify attributes to ignore in comparisons (see {@link #ignoredAttributes})</li>
+ *   <li>Specify nodes to ignore, including children (see {@link #ignoredNodes})</li>
+ *   <li>Specify a tolerance threshold for comparisons of numerical values (see {@link
#tolerance})</li>
  * </ul>
  *
  * @author  Johann Sorel (Geomatys)
@@ -69,6 +70,10 @@ import static org.apache.sis.util.CharSe
  * @since   0.3 (derived from geotk-3.17)
  * @version 0.3
  * @module
+ *
+ * @see XMLTestCase
+ * @see XMLTransformation
+ * @see Assert#assertXmlEquals(Object, Object, String[])
  */
 public strictfp class XMLComparator {
     /**

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java?rev=1496964&r1=1496963&r2=1496964&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java
[UTF-8] Wed Jun 26 15:25:36 2013
@@ -37,6 +37,9 @@ import static org.junit.Assert.*;
  * @since   0.3
  * @version 0.3
  * @module
+ *
+ * @see XMLComparator
+ * @see XMLTransformation
  */
 public abstract strictfp class XMLTestCase extends TestCase {
     /**

Added: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTransformation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTransformation.java?rev=1496964&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTransformation.java
(added)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTransformation.java
[UTF-8] Wed Jun 26 15:25:36 2013
@@ -0,0 +1,98 @@
+/*
+ * 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.sis.test;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.sis.xml.Namespaces;
+import org.apache.sis.util.StringBuilders;
+
+
+/**
+ * A predefined set of transformations that can be applied on XML strings before processing.
+ * Those transformations are applied in order to allow to make some tests to be tolerant
to
+ * different JAXB implementations.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+public strictfp enum XMLTransformation {
+    /**
+     * Transformations related to XML in the {@value org.apache.xml.Namespaces#GML} namespace.
+     */
+    GML("gml", "(?s).*xmlns\\s*(:\\s*\\w+)?\\s*=\\s*\"" + Namespaces.GML + "\".*");
+
+    /**
+     * The expected prefix.
+     */
+    private final String standardPrefix;
+
+    /**
+     * The pattern for getting the XML prefix of a given namespace.
+     */
+    public final Pattern findPrefix;
+
+    /**
+     * Creates a new enum for the given pattern.
+     */
+    private XMLTransformation(final String prefix, final String findPrefix) {
+        this.standardPrefix = prefix;
+        this.findPrefix = Pattern.compile(findPrefix);
+    }
+
+    /**
+     * If the given XML does not contain a {@code xmlns} attribute for the namespace represented
by this enum,
+     * removes the prefix which was expected for this namespace. For example if the expected
XML was:
+     *
+     * {@preformat xml
+     *   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+     *   <gml:TimeInstant xmlns:gml="http://www.opengis.net/gml">
+     *     <gml:timePosition>1992-01-01T01:00:00.000+01:00</gml:timePosition>
+     *   </gml:TimeInstant>
+     * }
+     *
+     * but the actual XML is:
+     *
+     * {@preformat xml
+     *   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+     *   <TimeInstant xmlns="http://www.opengis.net/gml">
+     *     <timePosition>1992-01-01T01:00:00.000+01:00</timePosition>
+     *   </TimeInstant>
+     * }
+     *
+     * then this remove will remove all occurrence of {@code "gmd:"} in the expected string.
+     *
+     * @param  expected The expected XML.
+     * @param  actual The actual XML.
+     * @return The new expected XML.
+     */
+    public String optionallyRemovePrefix(String expected, final CharSequence actual) {
+        final Matcher matcher = findPrefix.matcher(actual);
+        if (matcher.matches() && matcher.groupCount() == 1) {
+            final String prefix = matcher.group(1);
+            if (prefix == null) {
+                final StringBuilder buffer = new StringBuilder(expected);
+                StringBuilders.replace(buffer, standardPrefix + ':', "");
+                StringBuilders.replace(buffer, "xmlns:" + standardPrefix, "xmlns");
+                expected = buffer.toString();
+            }
+        }
+        return expected;
+    }
+}

Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTransformation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTransformation.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8



Mime
View raw message