sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1499816 - in /sis/branches/JDK7/core/sis-metadata/src: main/java/org/apache/sis/metadata/iso/content/ main/java/org/apache/sis/metadata/iso/identification/ main/java/org/apache/sis/metadata/iso/spatial/ test/java/org/apache/sis/metadata/is...
Date Thu, 04 Jul 2013 17:31:41 GMT
Author: desruisseaux
Date: Thu Jul  4 17:31:40 2013
New Revision: 1499816

URL: http://svn.apache.org/r1499816
Log:
Workaround for an element which is randomly missing at XML marshalling time (JAXB bug?)

Modified:
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/content/package-info.java
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFraction.java
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultResolution.java
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/package-info.java
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/spatial/package-info.java
    sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/content/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/content/package-info.java?rev=1499816&r1=1499815&r2=1499816&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/content/package-info.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/content/package-info.java
[UTF-8] Thu Jul  4 17:31:40 2013
@@ -111,10 +111,9 @@
     @XmlJavaTypeAdapter(InternationalStringAdapter.class),
     @XmlJavaTypeAdapter(GO_GenericName.class),
     @XmlJavaTypeAdapter(GO_RecordType.class),
-    @XmlJavaTypeAdapter(GO_Boolean.class),        @XmlJavaTypeAdapter(type=boolean.class,
value=GO_Boolean.class),
-    @XmlJavaTypeAdapter(GO_Decimal.class),        @XmlJavaTypeAdapter(type=double.class,
 value=GO_Decimal.class),
-    @XmlJavaTypeAdapter(GO_Integer.class),        @XmlJavaTypeAdapter(type=int.class,   
 value=GO_Integer.class),
-    @XmlJavaTypeAdapter(GO_Integer.AsLong.class), @XmlJavaTypeAdapter(type=long.class,  
 value=GO_Integer.AsLong.class)
+    @XmlJavaTypeAdapter(GO_Boolean.class), @XmlJavaTypeAdapter(type=boolean.class, value=GO_Boolean.class),
+    @XmlJavaTypeAdapter(GO_Decimal.class), @XmlJavaTypeAdapter(type=double.class,  value=GO_Decimal.class),
+    @XmlJavaTypeAdapter(GO_Integer.class), @XmlJavaTypeAdapter(type=int.class,     value=GO_Integer.class)
 })
 package org.apache.sis.metadata.iso.content;
 

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFraction.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFraction.java?rev=1499816&r1=1499815&r2=1499816&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFraction.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFraction.java
[UTF-8] Thu Jul  4 17:31:40 2013
@@ -19,7 +19,9 @@ package org.apache.sis.metadata.iso.iden
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 import org.opengis.metadata.identification.RepresentativeFraction;
+import org.apache.sis.internal.jaxb.gco.GO_Integer;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 
@@ -105,6 +107,7 @@ public class DefaultRepresentativeFracti
      * Returns the denominator of this representative fraction.
      */
     @Override
+    @XmlJavaTypeAdapter(value = GO_Integer.AsLong.class, type = long.class)
     @XmlElement(name = "denominator", required = true)
     public long getDenominator() {
         return denominator;

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultResolution.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultResolution.java?rev=1499816&r1=1499815&r2=1499816&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultResolution.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultResolution.java
[UTF-8] Thu Jul  4 17:31:40 2013
@@ -27,6 +27,7 @@ import org.apache.sis.internal.metadata.
 import org.apache.sis.metadata.iso.ISOMetadata;
 import org.apache.sis.measure.ValueRange;
 import org.apache.sis.util.resources.Messages;
+import org.apache.sis.util.Workaround;
 
 
 /**
@@ -168,8 +169,6 @@ public class DefaultResolution extends I
      */
     @Override
     @ValueRange(minimum=0, isMinIncluded=false)
-    @XmlJavaTypeAdapter(GO_Distance.class)
-    @XmlElement(name = "distance")
     public Double getDistance() {
         return isDistance() ? (Double) scaleOrDistance : null;
     }
@@ -189,4 +188,31 @@ public class DefaultResolution extends I
         }
         scaleOrDistance = newValue;
     }
+
+    /**
+     * Workaround for a strange JAXB behavior (bug?). For an unknown reason, we are unable
to annotate the
+     * {@link #getDistance()} method directly. Doing so cause JAXB to randomly ignores the
{@code <gmd:distance>}
+     * property. Annotating a separated method which in turn invokes the real method seems
to work.
+     *
+     * <p>In order to check if this workaround is still needed with more recent JAXB
versions, move the
+     * {@link XmlElement} and {@link XmlJavaTypeAdapter} annotations to the {@link #getDistance()}
method,
+     * then execute the {@link DefaultResolutionTest#testXML()} test at least 10 times (because
the failure
+     * happen randomly). If the test succeeded every time, then the {@code getValue()} and
{@code setValue(Double)}
+     * methods can be completely deleted.</p>
+     *
+     * @see DefaultResolutionTest#testXML()
+     */
+    @XmlElement(name = "distance")
+    @XmlJavaTypeAdapter(GO_Distance.class)
+    @Workaround(library = "JAXB", version = "2.2.4-2")
+    private Double getValue() {
+        return getDistance();
+    }
+
+    /**
+     * The corresponding setter for the {@link #getValue()} workaround.
+     */
+    private void setValue(final Double newValue) {
+        setDistance(newValue);
+    }
 }

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/package-info.java?rev=1499816&r1=1499815&r2=1499816&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/package-info.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/package-info.java
[UTF-8] Thu Jul  4 17:31:40 2013
@@ -122,9 +122,7 @@
     @XmlJavaTypeAdapter(LocaleAdapter.class),
     @XmlJavaTypeAdapter(StringAdapter.class),
     @XmlJavaTypeAdapter(InternationalStringAdapter.class),
-    @XmlJavaTypeAdapter(GO_DateTime.class),
-    @XmlJavaTypeAdapter(GO_Decimal.class),        @XmlJavaTypeAdapter(type=double.class,
value=GO_Decimal.class),
-    @XmlJavaTypeAdapter(GO_Integer.AsLong.class), @XmlJavaTypeAdapter(type=long.class,  
value=GO_Integer.AsLong.class)
+    @XmlJavaTypeAdapter(GO_DateTime.class)
 })
 package org.apache.sis.metadata.iso.identification;
 

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/spatial/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/spatial/package-info.java?rev=1499816&r1=1499815&r2=1499816&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/spatial/package-info.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/spatial/package-info.java
[UTF-8] Thu Jul  4 17:31:40 2013
@@ -111,7 +111,6 @@
 
     // Java types, primitive types and basic OGC types handling
     @XmlJavaTypeAdapter(InternationalStringAdapter.class),
-    @XmlJavaTypeAdapter(GO_Decimal.class), @XmlJavaTypeAdapter(type=double.class,  value=GO_Decimal.class),
     @XmlJavaTypeAdapter(GO_Integer.class), @XmlJavaTypeAdapter(type=int.class,     value=GO_Integer.class),
     @XmlJavaTypeAdapter(GO_Boolean.class), @XmlJavaTypeAdapter(type=boolean.class, value=GO_Boolean.class)
 })

Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java?rev=1499816&r1=1499815&r2=1499816&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java
[UTF-8] Thu Jul  4 17:31:40 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.metadata.iso.identification;
 
+import javax.xml.bind.JAXBException;
+import org.apache.sis.xml.XML;
 import org.apache.sis.metadata.iso.LoggingWatcher;
 import org.apache.sis.test.TestCase;
 import org.junit.Rule;
@@ -77,4 +79,65 @@ public final strictfp class DefaultResol
         assertNull("equivalentScale", metadata.getEquivalentScale());
         assertNull("distance", metadata.getDistance());
     }
+
+    /**
+     * Tests XML (un)marshalling of a resolution element. The main purpose of this method
is to test our
+     * workaround for a strange JAXB behavior (bug?).  For an unknown reason, we are unable
to annotate the
+     * {@link DefaultResolution#getDistance()} method directly. Doing so cause JAXB to randomly
ignores the
+     * {@code <gmd:distance>} property. Annotating a separated method which in turn
invokes the real method
+     * seems to work.
+     *
+     * <p>This test creates a {@link DefaultResolution} instance which is expected
to be marshalled as below
+     * (ignoring namespace declarations):</p>
+     *
+     * {@preformat xml
+     *   <gmd:MD_Resolution>
+     *     <gmd:distance>
+     *       <gco:Distance uom=\"http://schemas.opengis.net/iso/19139/20070417/resources/uom/gmxUom.xml#xpointer(//*[@gml:id='m'])\">1000.0</gco:Distance>
+     *     </gmd:distance>
+     *   </gmd:MD_Resolution>
+     * }
+     *
+     * If we annotate the public {@code getDistance()} directly, JAXB will sometime marshals
the resolution as
+     * expected, or sometime marshals an empty element as below:
+     *
+     * {@preformat xml
+     *   <gmd:MD_Resolution/>
+     * }
+     *
+     * In the later case, debugging shows that the {@code getDistance()} method is simply
never invoked.
+     * Whether the distance is marshaled or not seems totally random: just executing this
test many time
+     * make both cases to occur (however failures occur more often the successes).
+     *
+     * <p>Annotating an other method as a workaround seems to always work. See the
{@link DefaultResolution#getValue()}
+     * javadoc for instructions about how to check if this workaround is still needed with
more recent JAXB versions.</p>
+     *
+     * @throws JAXBException If an error occurred while marshalling the element.
+     *
+     * @see DefaultResolution#getValue()
+     */
+    @Test
+    public void testXML() throws JAXBException {
+        final DefaultResolution resolution = new DefaultResolution();
+        resolution.setDistance(1000.0);
+        final String xml = XML.marshal(resolution);
+        assertTrue("<gmd:distance> element is missing. If this test fails randomly,
"
+                + "see DefaultResolutionTest.testXML() javadoc for more information", xml.contains("distance"));
+        /*
+         * Following test is done as a matter of principle, but should not be a problem.
+         * The real issue is the <gmd:distance> which happen to be randomly missing
for
+         * an unknown reason.
+         */
+        assertXmlEquals(
+                "<gmd:MD_Resolution>\n" +
+                "  <gmd:distance>\n" +
+                "    <gco:Distance uom=\"http://schemas.opengis.net/iso/19139/20070417/resources/uom/gmxUom.xml#xpointer(//*[@gml:id='m'])\">1000.0</gco:Distance>\n"
+
+                "  </gmd:distance>\n" +
+                "</gmd:MD_Resolution>", xml, "xmlns:*");
+        /*
+         * Unmarshal the element back to a Java object, as a safety.
+         * Should not be a problem neither.
+         */
+        assertEquals(resolution, XML.unmarshal(xml));
+    }
 }



Mime
View raw message