Search in sources :

Example 1 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class XMLTestCase method unmarshal.

/**
 * Unmarshals the given object using the {@linkplain #getMarshallerPool() test marshaller pool}.
 *
 * @param  <T>   compile-time type of {@code type} argument.
 * @param  type  the expected type of the unmarshalled object.
 * @param  xml   the XML representation of the object to unmarshal.
 * @return the unmarshalled object.
 * @throws JAXBException if an error occurred while unmarshalling the XML.
 *
 * @see #marshal(Object)
 */
protected final <T> T unmarshal(final Class<T> type, final String xml) throws JAXBException {
    final MarshallerPool pool = getMarshallerPool();
    final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
    final Object object = unmarshal(unmarshaller, xml);
    pool.recycle(unmarshaller);
    assertInstanceOf("unmarshal", type, object);
    return type.cast(object);
}
Also used : MarshallerPool(org.apache.sis.xml.MarshallerPool) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class XMLTestCase method marshal.

/**
 * Marshals the given object using the {@linkplain #getMarshallerPool() test marshaller pool}.
 * The default XML schema is used (usually the most recent one).
 *
 * @param  object  the object to marshal.
 * @return the marshalled object.
 * @throws JAXBException if an error occurred while marshalling the object.
 *
 * @see #unmarshal(Class, String)
 */
protected final String marshal(final Object object) throws JAXBException {
    final MarshallerPool pool = getMarshallerPool();
    final Marshaller marshaller = pool.acquireMarshaller();
    final String xml = marshal(marshaller, object);
    pool.recycle(marshaller);
    return xml;
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshallerPool(org.apache.sis.xml.MarshallerPool)

Example 3 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class XMLTestCase method unmarshalFile.

/**
 * Unmarshals the content of the given test file using the {@linkplain #getMarshallerPool() test marshaller pool}.
 * The resource is obtained by a call to {@code getClass().getResource(filename)}, which implies that the file
 * shall be in the same package than the subclass of {@code this}.
 *
 * @param  <T>       compile-time type of {@code type} argument.
 * @param  type      the expected type of the unmarshalled object.
 * @param  filename  the name of the XML file in the package of the final subclass of {@code this}.
 * @return the object unmarshalled from the given file.
 * @throws JAXBException if an error occurred during unmarshalling.
 *
 * @see #assertMarshalEqualsFile(String, Object, String...)
 */
protected final <T> T unmarshalFile(final Class<T> type, final String filename) throws JAXBException {
    final MarshallerPool pool = getMarshallerPool();
    final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
    final Object object = unmarshaller.unmarshal(getResource(filename));
    pool.recycle(unmarshaller);
    assertInstanceOf(filename, type, object);
    return type.cast(object);
}
Also used : MarshallerPool(org.apache.sis.xml.MarshallerPool) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class XMLTestCase method getMarshallerPool.

/**
 * Returns the default XML (un)marshaller pool potentially shared by test methods in all sub-classes.
 * The (un)marshallers locale is set to {@link Locale#UK} (the language of ISO standards) and their
 * timezone is arbitrarily set to CET (<cite>Central European Time</cite>).
 *
 * <div class="note"><b>Note:</b>
 * We intentionally use a timezone different than UTC in order to have an error of one or two hours
 * if a code fails to take timezone offset in account.</div>
 *
 * @return the shared (un)marshaller pool.
 * @throws JAXBException if an error occurred while creating the JAXB marshaller.
 */
protected static synchronized MarshallerPool getMarshallerPool() throws JAXBException {
    if (defaultPool == null) {
        final Map<String, Object> properties = new HashMap<>(4);
        assertNull(properties.put(XML.LOCALE, Locale.UK));
        assertNull(properties.put(XML.TIMEZONE, TIMEZONE));
        defaultPool = new MarshallerPool(properties);
    }
    return defaultPool;
}
Also used : HashMap(java.util.HashMap) MarshallerPool(org.apache.sis.xml.MarshallerPool)

Example 5 with MarshallerPool

use of org.apache.sis.xml.MarshallerPool in project sis by apache.

the class CodeListMarshallingTest method testLocalization.

/**
 * Implementation of {@link #testLocalization()} and {@link #testLocalizationLegacyXML()}.
 */
private void testLocalization(final boolean legacy) throws JAXBException {
    final MarshallerPool pool = getMarshallerPool();
    final Marshaller marshaller = pool.acquireMarshaller();
    if (legacy) {
        marshaller.setProperty(XML.METADATA_VERSION, VERSION_2007);
    }
    /*
         * First, test using the French locale.
         */
    marshaller.setProperty(XML.LOCALE, Locale.FRENCH);
    String expected = getCitationXML("fra", "Création", legacy);
    CitationDate ci = unmarshal(CitationDate.class, expected);
    assertEquals(DateType.CREATION, ci.getDateType());
    String actual = marshal(marshaller, ci);
    assertXmlEquals(expected, actual, "xmlns:*");
    /*
         * Tests again using the English locale.
         */
    marshaller.setProperty(XML.LOCALE, Locale.ENGLISH);
    expected = getCitationXML("eng", "Creation", legacy);
    ci = unmarshal(CitationDate.class, expected);
    assertEquals(DateType.CREATION, ci.getDateType());
    actual = marshal(marshaller, ci);
    assertXmlEquals(expected, actual, "xmlns:*");
    pool.recycle(marshaller);
}
Also used : Marshaller(javax.xml.bind.Marshaller) CitationDate(org.opengis.metadata.citation.CitationDate) MarshallerPool(org.apache.sis.xml.MarshallerPool)

Aggregations

MarshallerPool (org.apache.sis.xml.MarshallerPool)16 Marshaller (javax.xml.bind.Marshaller)9 Test (org.junit.Test)7 Unmarshaller (javax.xml.bind.Unmarshaller)5 HashMap (java.util.HashMap)3 DependsOnMethod (org.apache.sis.test.DependsOnMethod)3 BeforeClass (org.junit.BeforeClass)2 StringWriter (java.io.StringWriter)1 XMLComparator (org.apache.sis.test.XMLComparator)1 IdentifiedObjectMock (org.apache.sis.test.mock.IdentifiedObjectMock)1 Version (org.apache.sis.util.Version)1 CitationDate (org.opengis.metadata.citation.CitationDate)1 ResponsibleParty (org.opengis.metadata.citation.ResponsibleParty)1 BrowseGraphic (org.opengis.metadata.identification.BrowseGraphic)1