You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2013/06/27 23:19:42 UTC
svn commit: r1497567 - in /sis/branches/JDK7/core:
sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/
sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/
sis-metadata/src/test/java/org/apache/sis/test/suite/
sis-utility/src/main/jav...
Author: desruisseaux
Date: Thu Jun 27 21:19:41 2013
New Revision: 1497567
URL: http://svn.apache.org/r1497567
Log:
Added more tests.
Added:
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/CodeListMarshallingTest.java (with props)
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/LanguageMarshallingTest.java (with props)
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/DataIdentificationMock.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-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/IdentifiedObjectMock.java
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/package-info.java
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
Added: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/CodeListMarshallingTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/CodeListMarshallingTest.java?rev=1497567&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/CodeListMarshallingTest.java (added)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/CodeListMarshallingTest.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -0,0 +1,203 @@
+/*
+ * 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.jaxb.code;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Arrays;
+import java.util.Locale;
+import java.util.Collections;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.JAXBException;
+import org.opengis.metadata.citation.Role;
+import org.opengis.metadata.citation.DateType;
+import org.opengis.metadata.citation.CitationDate;
+import org.opengis.metadata.citation.ResponsibleParty;
+import org.opengis.metadata.identification.TopicCategory;
+import org.apache.sis.metadata.iso.identification.DefaultDataIdentification;
+import org.apache.sis.xml.XML;
+import org.apache.sis.xml.Namespaces;
+import org.apache.sis.xml.MarshallerPool;
+import org.apache.sis.util.CharSequences;
+import org.apache.sis.test.XMLTestCase;
+import org.junit.BeforeClass;
+import org.junit.AfterClass;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests the XML marshaling of {@code CodeList}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @author Guilhem Legal (Geomatys)
+ * @since 0.3 (derived from geotk-3.17)
+ * @version 0.3
+ * @module
+ *
+ * @see <a href="http://jira.geotoolkit.org/browse/GEOTK-121">GEOTK-121</a>
+ */
+public final strictfp class CodeListMarshallingTest extends XMLTestCase {
+ /**
+ * A poll of configured {@link Marshaller} and {@link Unmarshaller}, created when first needed.
+ */
+ private static MarshallerPool pool;
+
+ /**
+ * Creates the XML (un)marshaller pool to be shared by all test methods.
+ * The (un)marshallers locale and timezone will be set to fixed values.
+ *
+ * @throws JAXBException If an error occurred while creating the pool.
+ *
+ * @see #disposeMarshallerPool()
+ */
+ @BeforeClass
+ public static void createMarshallerPool() throws JAXBException {
+ final Map<String,Object> properties = new HashMap<>(4);
+ assertNull(properties.put(XML.LOCALE, Locale.FRANCE));
+ assertNull(properties.put(XML.TIMEZONE, "CET"));
+ pool = new MarshallerPool(properties);
+ }
+
+ /**
+ * Invoked by JUnit after the execution of every tests in order to dispose
+ * the {@link MarshallerPool} instance used internally by this class.
+ */
+ @AfterClass
+ public static void disposeMarshallerPool() {
+ pool = null;
+ }
+
+ /**
+ * Returns a XML string to use for testing purpose.
+ *
+ * @param baseURL The base URL of XML schemas.
+ */
+ private static String getResponsiblePartyXML(final String baseURL) {
+ return "<gmd:CI_ResponsibleParty xmlns:gmd=\"" + Namespaces.GMD + "\">\n" +
+ " <gmd:role>\n" +
+ " <gmd:CI_RoleCode codeList=\"" + baseURL + "resources/Codelist/gmxCodelists.xml#CI_RoleCode\"" +
+ " codeListValue=\"principalInvestigator\">" + "Principal investigator</gmd:CI_RoleCode>\n" +
+ " </gmd:role>\n" +
+ "</gmd:CI_ResponsibleParty>";
+ }
+
+ /**
+ * Returns a XML string to use for testing purpose.
+ *
+ * @param baseURL The base URL of XML schemas.
+ */
+ private static String getCitationXML(final String baseURL, final String language, final String value) {
+ return "<gmd:CI_Date xmlns:gmd=\"" + Namespaces.GMD + "\">\n" +
+ " <gmd:dateType>\n" +
+ " <gmd:CI_DateTypeCode codeList=\"" + baseURL + "resources/Codelist/gmxCodelists.xml#CI_DateTypeCode\"" +
+ " codeListValue=\"creation\" codeSpace=\"" + language + "\">" + value + "</gmd:CI_DateTypeCode>\n" +
+ " </gmd:dateType>\n" +
+ "</gmd:CI_Date>";
+ }
+
+ /**
+ * Tests marshaling using the default URL.
+ *
+ * @throws JAXBException If an error occurred while marshaling the XML.
+ */
+ @Test
+ public void testDefaultURL() throws JAXBException {
+ final String expected = getResponsiblePartyXML("http://schemas.opengis.net/iso/19139/20070417/");
+ final ResponsibleParty rp = (ResponsibleParty) XML.unmarshal(expected);
+ assertEquals(Role.PRINCIPAL_INVESTIGATOR, rp.getRole());
+ /*
+ * Use the convenience method in order to avoid the effort of creating
+ * our own MarshallerPool.
+ */
+ final String actual = XML.marshal(rp);
+ assertXmlEquals(expected, actual, "xmlns:*");
+ }
+
+ /**
+ * Tests marshaling using the ISO URL.
+ *
+ * @throws JAXBException If an error occurred while marshaling the XML.
+ */
+ @Test
+ public void testISO_URL() throws JAXBException {
+ final String expected = getResponsiblePartyXML("http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/");
+ final ResponsibleParty rp = (ResponsibleParty) XML.unmarshal(expected);
+ assertEquals(Role.PRINCIPAL_INVESTIGATOR, rp.getRole());
+
+ final Marshaller marshaller = pool.acquireMarshaller();
+ marshaller.setProperty(XML.SCHEMAS, Collections.singletonMap("gmd",
+ "http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas")); // Intentionally omit trailing '/'.
+ final String actual = marshal(marshaller, rp);
+ pool.recycle(marshaller);
+ assertXmlEquals(expected, actual, "xmlns:*");
+ }
+
+ /**
+ * Tests a code list localization.
+ *
+ * @throws JAXBException If an error occurred while marshaling the XML.
+ */
+ @Test
+ public void testLocalization() throws JAXBException {
+ final Marshaller marshaller = pool.acquireMarshaller();
+ /*
+ * First, test using the French locale.
+ */
+ marshaller.setProperty(XML.LOCALE, Locale.FRENCH);
+ String expected = getCitationXML("http://schemas.opengis.net/iso/19139/20070417/", "fra", "Création");
+ CitationDate ci = (CitationDate) XML.unmarshal(expected);
+ assertEquals(DateType.CREATION, ci.getDateType());
+ String actual = marshal(marshaller, ci);
+ assertXmlEquals(expected, actual, "xmlns:*");
+ /*
+ * Tests again using the Englisg locale.
+ */
+ marshaller.setProperty(XML.LOCALE, Locale.ENGLISH);
+ expected = getCitationXML("http://schemas.opengis.net/iso/19139/20070417/", "eng", "Creation");
+ ci = (CitationDate) XML.unmarshal(expected);
+ assertEquals(DateType.CREATION, ci.getDateType());
+ actual = marshal(marshaller, ci);
+ assertXmlEquals(expected, actual, "xmlns:*");
+
+ pool.recycle(marshaller);
+ }
+
+ /**
+ * Tests marshaling of a code list which is not in the list of standard codes.
+ *
+ * @throws JAXBException If an error occurred while marshaling the XML.
+ */
+ @Test
+ public void testExtraCodes() throws JAXBException {
+ final Marshaller marshaller = pool.acquireMarshaller();
+ final DefaultDataIdentification id = new DefaultDataIdentification();
+ id.setTopicCategories(Arrays.asList(
+ TopicCategory.valueOf("oceans"), // New code
+ TopicCategory.valueOf("OCEANS"), // Existing code with UML id="oceans"
+ TopicCategory.valueOf("test"))); // New code
+
+ final String xml = marshal(marshaller, id);
+ pool.recycle(marshaller);
+
+ // "OCEANS" is marshalled as "oceans" because is contains a UML id, which is lower-case.
+ assertEquals(2, CharSequences.count(xml, "<gmd:MD_TopicCategoryCode>oceans</gmd:MD_TopicCategoryCode>"));
+ assertEquals(0, CharSequences.count(xml, "<gmd:MD_TopicCategoryCode>OCEANS</gmd:MD_TopicCategoryCode>"));
+ assertEquals(1, CharSequences.count(xml, "<gmd:MD_TopicCategoryCode>test</gmd:MD_TopicCategoryCode>"));
+ }
+}
Propchange: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/CodeListMarshallingTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/code/CodeListMarshallingTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
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=1497567&r1=1497566&r2=1497567&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] Thu Jun 27 21:19:41 2013
@@ -19,8 +19,6 @@ package org.apache.sis.internal.jaxb.gml
import java.util.Map;
import java.util.HashMap;
import java.util.Locale;
-import java.io.StringReader;
-import java.io.StringWriter;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBContext;
@@ -55,11 +53,6 @@ public final strictfp class TimePeriodTe
private static MarshallerPool pool;
/**
- * A buffer where to marshal.
- */
- private final StringWriter buffer = new StringWriter();
-
- /**
* Set the marshalling context to a fixed locale and timezone before to create the
* JAXB wrappers for temporal objects. Callers shall invoke {@link #clearContext()}
* after this method.
@@ -114,14 +107,13 @@ public final strictfp class TimePeriodTe
final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
final TimeInstant instant = createTimeInstant("1992-01-01 00:00:00");
- marshaller.marshal(instant, buffer);
- final String actual = buffer.toString();
+ final String actual = marshal(marshaller, instant);
assertXmlEquals(
"<gml:TimeInstant>\n" +
" <gml:timePosition>1992-01-01T01:00:00.000+01:00</gml:timePosition>\n" +
"</gml:TimeInstant>\n", actual, "xmlns:*", "xsi:schemaLocation");
- final TimeInstant test = (TimeInstant) unmarshaller.unmarshal(new StringReader(actual));
+ final TimeInstant test = (TimeInstant) unmarshal(unmarshaller, actual);
assertEquals("1992-01-01 00:00:00", format(XmlUtilities.toDate(test.timePosition)));
pool.recycle(marshaller);
@@ -170,10 +162,9 @@ public final strictfp class TimePeriodTe
final TimePeriod period = new TimePeriod();
period.begin = begin;
period.end = end;
- marshaller.marshal(period, buffer);
- final String actual = buffer.toString();
+ final String actual = marshal(marshaller, period);
assertXmlEquals(expected, actual, "xmlns:*", "xsi:schemaLocation");
- final TimePeriod test = (TimePeriod) unmarshaller.unmarshal(new StringReader(actual));
+ final TimePeriod test = (TimePeriod) unmarshal(unmarshaller, actual);
if (verifyValues) {
assertEquals("1992-01-01 00:00:00", format(XmlUtilities.toDate(test.begin.calendar())));
assertEquals("2007-12-31 00:00:00", format(XmlUtilities.toDate(test.end .calendar())));
Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java?rev=1497567&r1=1497566&r2=1497567&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -55,7 +55,9 @@ import org.junit.BeforeClass;
org.apache.sis.metadata.AbstractMetadataTest.class,
org.apache.sis.metadata.iso.AllMetadataTest.class,
- // XML marshalling
+ // XML marshalling.
+ org.apache.sis.internal.jaxb.code.CodeListMarshallingTest.class,
+ org.apache.sis.internal.jaxb.gmd.LanguageMarshallingTest.class,
org.apache.sis.internal.jaxb.gml.TimePeriodTest.class
})
public final strictfp class MetadataTestSuite extends TestSuite {
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java?rev=1497567&r1=1497566&r2=1497567&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -102,17 +102,12 @@ public final class LanguageCode extends
return new LanguageCode(string);
}
}
- String codeSpace = null;
- String value = null;
- if (context != null) {
- final Locale marshalLocale = context.getLocale();
- if (marshalLocale != null) {
- codeSpace = Context.converter(context).toLanguageCode(context, locale);
- value = locale.getDisplayLanguage(marshalLocale);
- if (value.isEmpty()) {
- value = null;
- }
- }
+ final Locale marshalLocale = (context != null) ? context.getLocale() : null;
+ final String codeSpace = Context.converter(context).toLanguageCode(context, locale);
+ String value = (marshalLocale != null) ? locale.getDisplayLanguage(marshalLocale)
+ : locale.getDisplayLanguage();
+ if (value.isEmpty()) {
+ value = null;
}
if (!codeListValue.isEmpty() || value != null) {
return new LanguageCode(context, codeListValue, codeSpace, value);
Added: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/LanguageMarshallingTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/LanguageMarshallingTest.java?rev=1497567&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/LanguageMarshallingTest.java (added)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/LanguageMarshallingTest.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -0,0 +1,176 @@
+/*
+ * 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.jaxb.gmd;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Locale;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import org.opengis.metadata.identification.DataIdentification;
+import org.apache.sis.test.mock.DataIdentificationMock;
+import org.apache.sis.xml.XML;
+import org.apache.sis.xml.Namespaces;
+import org.apache.sis.xml.MarshallerPool;
+import org.apache.sis.test.XMLTestCase;
+import org.junit.BeforeClass;
+import org.junit.AfterClass;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+import static org.apache.sis.test.TestUtilities.getSingleton;
+
+
+/**
+ * Tests the XML marshaling of {@code Locale} when used for a language.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3 (derived from geotk-3.18)
+ * @version 0.3
+ * @module
+ */
+public final strictfp class LanguageMarshallingTest extends XMLTestCase {
+ /**
+ * A poll of configured {@link Marshaller} and {@link Unmarshaller}, created when first needed.
+ */
+ private static MarshallerPool pool;
+
+ /**
+ * Creates the XML (un)marshaller pool to be shared by all test methods.
+ * The (un)marshallers locale and timezone will be set to fixed values.
+ *
+ * @throws JAXBException If an error occurred while creating the pool.
+ *
+ * @see #disposeMarshallerPool()
+ */
+ @BeforeClass
+ public static void createMarshallerPool() throws JAXBException {
+ final Map<String,Object> properties = new HashMap<>(4);
+ assertNull(properties.put(XML.LOCALE, Locale.UK));
+ assertNull(properties.put(XML.TIMEZONE, "UTC"));
+ pool = new MarshallerPool(JAXBContext.newInstance(DataIdentificationMock.class), properties);
+ }
+
+ /**
+ * Invoked by JUnit after the execution of every tests in order to dispose
+ * the {@link MarshallerPool} instance used internally by this class.
+ */
+ @AfterClass
+ public static void disposeMarshallerPool() {
+ pool = null;
+ }
+
+ /**
+ * Returns the XML of a data identification element. This method returns the following string,
+ * where the {@code <gco:CharacterString>} block is replaced by the more complex
+ * {@code <gmd:LanguageCode>} if the {@code languageCode} argument is {@code true}.
+ *
+ * {@preformat xml
+ * <gmd:MD_DataIdentification>
+ * <gmd:language>
+ * <gco:CharacterString>fra</gco:CharacterString>
+ * </gmd:language>
+ * </gmd:MD_DataIdentification>
+ * }
+ *
+ * @param languageCode {@code true} for using the {@code gmd:LanguageCode} construct,
+ * or false for using the {@code gco:CharacterString} construct.
+ */
+ private static String getDataIdentificationXML(final boolean languageCode) {
+ final StringBuilder buffer = new StringBuilder(
+ "<gmd:MD_DataIdentification" +
+ " xmlns:gmd=\"" + Namespaces.GMD + '"' +
+ " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
+ " <gmd:language>\n");
+ if (languageCode) {
+ buffer.append(" <gmd:LanguageCode"
+ + " codeList=\"http://schemas.opengis.net/iso/19139/20070417/resources/Codelist/ML_gmxCodelists.xml#LanguageCode\""
+ + " codeListValue=\"fra\">French</gmd:LanguageCode>\n");
+ } else {
+ buffer.append(" <gco:CharacterString>fra</gco:CharacterString>\n");
+ }
+ buffer.append(
+ " </gmd:language>\n" +
+ "</gmd:MD_DataIdentification>");
+ return buffer.toString();
+ }
+
+ /**
+ * Tests the parsing of an XML using the {@code gmd:LanguageCode} construct.
+ *
+ * @throws JAXBException Should never happen.
+ */
+ @Test
+ public void testLanguageCode() throws JAXBException {
+ final Marshaller marshaller = pool.acquireMarshaller();
+ final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
+
+ final String xml = getDataIdentificationXML(true);
+ final DataIdentification id = (DataIdentification) unmarshal(unmarshaller, xml);
+ assertEquals(Locale.FRENCH, getSingleton(id.getLanguages()));
+ /*
+ * Reformat and test against the original XML.
+ */
+ assertXmlEquals(xml, marshal(marshaller, id), "xmlns:*");
+ pool.recycle(unmarshaller);
+ pool.recycle(marshaller);
+ }
+
+ /**
+ * Tests the parsing of an XML using the {@code gco:CharacterString} construct.
+ *
+ * @throws JAXBException Should never happen.
+ */
+ @Test
+ public void testCharacterString() throws JAXBException {
+ final Marshaller marshaller = pool.acquireMarshaller();
+ final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
+
+ final String xml = getDataIdentificationXML(false);
+ final DataIdentification id = (DataIdentification) unmarshal(unmarshaller, xml);
+ assertEquals(Locale.FRENCH, getSingleton(id.getLanguages()));
+ /*
+ * Reformat and test against the expected XML.
+ */
+ assertXmlEquals(getDataIdentificationXML(true), marshal(marshaller, id), "xmlns:*");
+ pool.recycle(unmarshaller);
+ pool.recycle(marshaller);
+ }
+
+ /**
+ * Tests the formatting of {@code <gco:CharacterString>}, which require explicit configuration.
+ *
+ * @throws JAXBException Should never happen.
+ */
+ @Test
+ public void testCharacterStringFormat() throws JAXBException {
+ final String inspire = getDataIdentificationXML(true);
+ final String simpler = getDataIdentificationXML(false);
+ final DataIdentificationMock id = new DataIdentificationMock(Locale.FRENCH);
+
+ final Marshaller marshaller = pool.acquireMarshaller();
+ assertNull(marshaller.getProperty(XML.STRING_SUBSTITUTES));
+ assertXmlEquals(inspire, marshal(marshaller, id), "xmlns:*");
+
+ marshaller.setProperty(XML.STRING_SUBSTITUTES, new String[] {"dummy","language","foo"});
+ assertArrayEquals(new String[] {"language"}, (String[]) marshaller.getProperty(XML.STRING_SUBSTITUTES));
+ assertXmlEquals(simpler, marshal(marshaller, id), "xmlns:*");
+ pool.recycle(marshaller);
+ }
+}
Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/LanguageMarshallingTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/jaxb/gmd/LanguageMarshallingTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
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=1497567&r1=1497566&r2=1497567&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] Thu Jun 27 21:19:41 2013
@@ -18,7 +18,13 @@ package org.apache.sis.test;
import java.util.Locale;
import java.util.TimeZone;
+import java.io.StringReader;
+import java.io.StringWriter;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.JAXBException;
import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.util.ArgumentChecks;
import org.junit.After;
import static org.junit.Assert.*;
@@ -52,6 +58,11 @@ public abstract strictfp class XMLTestCa
protected Context context;
/**
+ * A buffer for {@link #marshal(Marshaller, Object)}, created only when first needed.
+ */
+ private StringWriter buffer;
+
+ /**
* Creates a new test case.
*/
protected XMLTestCase() {
@@ -84,4 +95,35 @@ public abstract strictfp class XMLTestCa
context = null;
}
}
+
+ /**
+ * Marshals the given object using the given marshaler.
+ *
+ * @param marshaller The marshaller to use.
+ * @param object The object to marshal, or {@code null}.
+ * @return The marshalled object, or {@code null} if and only if the given {@code object} was null.
+ * @throws JAXBException If an error occurred while marshalling the object.
+ */
+ protected final String marshal(final Marshaller marshaller, final Object object) throws JAXBException {
+ ArgumentChecks.ensureNonNull("marshaller", marshaller);
+ if (buffer == null) {
+ buffer = new StringWriter();
+ }
+ buffer.getBuffer().setLength(0);
+ marshaller.marshal(object, buffer);
+ return buffer.toString();
+ }
+
+ /**
+ * Unmarshals the given XML using the given unmarshaler.
+ *
+ * @param unmarshaller The unmarshaller to use.
+ * @param xml The XML representation of the object to unmarshal, or {@code null}.
+ * @return The unmarshalled object, or {@code null} if and only if the given {@code xml} was null.
+ * @throws JAXBException If an error occurred while unmarshalling the XML.
+ */
+ protected final Object unmarshal(final Unmarshaller unmarshaller, final String xml) throws JAXBException {
+ ArgumentChecks.ensureNonNull("unmarshaller", unmarshaller);
+ return unmarshaller.unmarshal(new StringReader(xml));
+ }
}
Added: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/DataIdentificationMock.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/DataIdentificationMock.java?rev=1497567&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/DataIdentificationMock.java (added)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/DataIdentificationMock.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -0,0 +1,271 @@
+/*
+ * 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.mock;
+
+import java.util.Locale;
+import java.util.Collection;
+import java.util.Collections;
+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.citation.Citation;
+import org.opengis.metadata.citation.ResponsibleParty;
+import org.opengis.metadata.constraint.Constraints;
+import org.opengis.metadata.distribution.Format;
+import org.opengis.metadata.extent.Extent;
+import org.opengis.metadata.identification.AggregateInformation;
+import org.opengis.metadata.identification.BrowseGraphic;
+import org.opengis.metadata.identification.CharacterSet;
+import org.opengis.metadata.identification.DataIdentification;
+import org.opengis.metadata.identification.Keywords;
+import org.opengis.metadata.identification.Progress;
+import org.opengis.metadata.identification.Resolution;
+import org.opengis.metadata.identification.TopicCategory;
+import org.opengis.metadata.identification.Usage;
+import org.opengis.metadata.maintenance.MaintenanceInformation;
+import org.opengis.metadata.spatial.SpatialRepresentationType;
+import org.opengis.util.InternationalString;
+import org.apache.sis.xml.Namespaces;
+import org.apache.sis.internal.jaxb.gmd.LocaleAdapter;
+
+
+/**
+ * A dummy implementation of {@link DataIdentification} with minimal XML (un)marshalling capability.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+@XmlType(name = "MD_DataIdentification_Type")
+@XmlRootElement(name = "MD_DataIdentification", namespace = Namespaces.GMD)
+public final strictfp class DataIdentificationMock implements DataIdentification {
+ /**
+ * The locale to (un)marshal as a language.
+ */
+ @XmlElement(namespace = Namespaces.GMD)
+ @XmlJavaTypeAdapter(LocaleAdapter.class)
+ public Locale language;
+
+ /**
+ * Creates an initially empty metadata.
+ * This constructor is required by JAXB.
+ */
+ public DataIdentificationMock() {
+ }
+
+ /**
+ * Creates an initially empty metadata with the given language.
+ * Callers are free to assign new value to the {@link #language} field directly.
+ *
+ * @param language The initial {@link #language} value (can be {@code null}).
+ */
+ public DataIdentificationMock(final Locale language) {
+ this.language = language;
+ }
+
+ /**
+ * Returns {@link #language} in an unmodifiable collection.
+ *
+ * @return {@link #language} singleton.
+ */
+ @Override
+ public Collection<Locale> getLanguages() {
+ return (language != null) ? Collections.singleton(language) : Collections.<Locale>emptySet();
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<SpatialRepresentationType> getSpatialRepresentationTypes() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends Resolution> getSpatialResolutions() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<CharacterSet> getCharacterSets() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<TopicCategory> getTopicCategories() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public InternationalString getEnvironmentDescription() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends Extent> getExtents() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public InternationalString getSupplementalInformation() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Citation getCitation() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public InternationalString getAbstract() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public InternationalString getPurpose() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<String> getCredits() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<Progress> getStatus() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends ResponsibleParty> getPointOfContacts() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends MaintenanceInformation> getResourceMaintenances() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends BrowseGraphic> getGraphicOverviews() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends Format> getResourceFormats() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends Keywords> getDescriptiveKeywords() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends Usage> getResourceSpecificUsages() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends Constraints> getResourceConstraints() {
+ return null;
+ }
+
+ /**
+ * Undefined property.
+ * @return {@code null}.
+ */
+ @Override
+ public Collection<? extends AggregateInformation> getAggregationInfo() {
+ return null;
+ }
+}
Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/DataIdentificationMock.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/DataIdentificationMock.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/IdentifiedObjectMock.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/IdentifiedObjectMock.java?rev=1497567&r1=1497566&r2=1497567&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/IdentifiedObjectMock.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/IdentifiedObjectMock.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -82,7 +82,7 @@ public final strictfp class IdentifiedOb
*/
@Override
public Collection<GenericName> getAlias() {
- return Collections.singleton(alias);
+ return (alias != null) ? Collections.singleton(alias) : Collections.<GenericName>emptySet();
}
/**
Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/package-info.java?rev=1497567&r1=1497566&r2=1497567&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/package-info.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/mock/package-info.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -29,6 +29,7 @@
*/
@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED, namespace = Namespaces.GML, xmlns = {
@XmlNs(prefix = "gml", namespaceURI = Namespaces.GML),
+ @XmlNs(prefix = "gmd", namespaceURI = Namespaces.GMD),
@XmlNs(prefix = "gco", namespaceURI = Namespaces.GCO)
})
@XmlAccessorType(XmlAccessType.NONE)
Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1497567&r1=1497566&r2=1497567&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -99,7 +99,7 @@ import org.junit.BeforeClass;
org.apache.sis.util.collection.TreeTableFormatTest.class,
org.apache.sis.util.collection.RangeSetTest.class,
- // Converters
+ // Converters.
org.apache.sis.internal.converter.AngleConverterTest.class,
org.apache.sis.internal.converter.StringConverterTest.class,
org.apache.sis.internal.converter.PathConverterTest.class,
Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java?rev=1497567&r1=1497566&r2=1497567&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java [UTF-8] Thu Jun 27 21:19:41 2013
@@ -16,8 +16,6 @@
*/
package org.apache.sis.util.iso;
-import java.io.StringReader;
-import java.io.StringWriter;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBContext;
@@ -59,24 +57,22 @@ public final strictfp class NameMarshall
* Returns the XML representation of the given name, wrapped
* in a mock {@code <gml:IO_IdentifiedObject>} element.
*/
- private static String marshall(final GenericName name) throws JAXBException {
+ private String marshal(final GenericName name) throws JAXBException {
if (pool == null) {
pool = new MarshallerPool(JAXBContext.newInstance(IdentifiedObjectMock.class), null);
}
final Marshaller marshaller = pool.acquireMarshaller();
- final StringWriter out = new StringWriter();
- marshaller.marshal(new IdentifiedObjectMock(name), out);
+ final String xml = marshal(marshaller, new IdentifiedObjectMock(name));
pool.recycle(marshaller);
- return out.toString();
+ return xml;
}
/**
- * Converse of {@link #marshall(GenericName)}.
+ * Converse of {@link #marshal(GenericName)}.
*/
- private static GenericName unmarshall(final String xml) throws JAXBException {
+ private GenericName unmarshall(final String xml) throws JAXBException {
final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
- final StringReader in = new StringReader(xml);
- final Object value = unmarshaller.unmarshal(in);
+ final Object value = unmarshal(unmarshaller, xml);
pool.recycle(unmarshaller);
return ((IdentifiedObjectMock) value).alias;
}
@@ -97,7 +93,7 @@ public final strictfp class NameMarshall
" <gco:LocalName>An ordinary local name</gco:LocalName>\n" +
" </gml:alias>\n" +
"</gml:IO_IdentifiedObject>\n";
- final String actual = marshall(name);
+ final String actual = marshal(name);
assertXmlEquals(expected, actual, "xmlns:*", "xsi:schemaLocation");
assertEquals(name, unmarshall(actual));
}
@@ -119,7 +115,7 @@ public final strictfp class NameMarshall
" <gco:LocalName>A name with & and > and <.</gco:LocalName>\n" +
" </gml:alias>\n" +
"</gml:IO_IdentifiedObject>\n";
- final String actual = marshall(name);
+ final String actual = marshal(name);
assertXmlEquals(expected, actual, "xmlns:*", "xsi:schemaLocation");
assertEquals(name, unmarshall(actual));
}
@@ -144,7 +140,7 @@ public final strictfp class NameMarshall
" </gco:TypeName>\n" +
" </gml:alias>\n" +
"</gml:IO_IdentifiedObject>\n";
- final String actual = marshall(name);
+ final String actual = marshal(name);
assertXmlEquals(expected, actual, "xmlns:*", "xsi:schemaLocation");
assertEquals(name, unmarshall(actual));
}
@@ -165,7 +161,7 @@ public final strictfp class NameMarshall
" <gco:ScopedName>myScope:myName</gco:ScopedName>\n" +
" </gml:alias>\n" +
"</gml:IO_IdentifiedObject>\n";
- final String actual = marshall(name);
+ final String actual = marshal(name);
assertXmlEquals(expected, actual, "xmlns:*", "xsi:schemaLocation");
assertEquals(name, unmarshall(actual));
}