Search in sources :

Example 1 with NamespaceBindingException

use of org.opencastproject.mediapackage.NamespaceBindingException in project opencast by opencast.

the class DublinCoreTest method testNewInstance.

/**
 * Test method for saving the catalog.
 */
@Test
public void testNewInstance() {
    try {
        // Read the sample catalog
        FileInputStream in = new FileInputStream(catalogFile);
        DublinCoreCatalog dcSample = DublinCores.read(in);
        IOUtils.closeQuietly(in);
        // Create a new catalog and fill it with a few fields
        DublinCoreCatalog dcNew = DublinCores.mkOpencastEpisode().getCatalog();
        dcTempFile1 = testFolder.newFile();
        // Add the required fields
        dcNew.add(PROPERTY_IDENTIFIER, dcSample.getFirst(PROPERTY_IDENTIFIER));
        dcNew.add(PROPERTY_TITLE, dcSample.getFirst(PROPERTY_TITLE, DublinCore.LANGUAGE_UNDEFINED), DublinCore.LANGUAGE_UNDEFINED);
        // Add an additional field
        dcNew.add(PROPERTY_PUBLISHER, dcSample.getFirst(PROPERTY_PUBLISHER));
        // Add a null-value field
        try {
            dcNew.add(PROPERTY_CONTRIBUTOR, (String) null);
            fail();
        } catch (IllegalArgumentException ignore) {
        }
        // Add a field with an encoding scheme
        dcNew.add(PROPERTY_LICENSE, DublinCoreValue.mk("http://www.opencastproject.org/license", DublinCore.LANGUAGE_UNDEFINED, ENC_SCHEME_URI));
        // Don't forget to bind the namespace...
        dcNew.addBindings(XmlNamespaceContext.mk("octest", "http://www.opencastproject.org/octest"));
        dcNew.add(OC_PROPERTY_PROMOTED, DublinCoreValue.mk("true", DublinCore.LANGUAGE_UNDEFINED, new EName("http://www.opencastproject.org/octest", "Boolean")));
        try {
            dcNew.add(OC_PROPERTY_PROMOTED, DublinCoreValue.mk("true", DublinCore.LANGUAGE_UNDEFINED, EName.mk("http://www.opencastproject.org/enc-scheme", "Boolean")));
            fail();
        } catch (NamespaceBindingException e) {
        // Ok. This exception is expected to occur
        }
        // Store the catalog
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        FileWriter sw = new FileWriter(dcTempFile1);
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(dcNew.toXml());
        trans.transform(source, result);
        // Re-read the saved catalog and test for its content
        DublinCoreCatalog dcNewFromDisk = DublinCores.read(dcTempFile1.toURI().toURL().openStream());
        assertEquals(dcSample.getFirst(PROPERTY_IDENTIFIER), dcNewFromDisk.getFirst(PROPERTY_IDENTIFIER));
        assertEquals(dcSample.getFirst(PROPERTY_TITLE, "en"), dcNewFromDisk.getFirst(PROPERTY_TITLE, "en"));
        assertEquals(dcSample.getFirst(PROPERTY_PUBLISHER), dcNewFromDisk.getFirst(PROPERTY_PUBLISHER));
    } catch (IOException e) {
        fail("Error creating the catalog: " + e.getMessage());
    } catch (ParserConfigurationException e) {
        fail("Error creating a parser for the catalog: " + e.getMessage());
    } catch (TransformerException e) {
        fail("Error saving the catalog: " + e.getMessage());
    }
}
Also used : EName(org.opencastproject.mediapackage.EName) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileWriter(java.io.FileWriter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) NamespaceBindingException(org.opencastproject.mediapackage.NamespaceBindingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) Test(org.junit.Test)

Aggregations

FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Test (org.junit.Test)1 EName (org.opencastproject.mediapackage.EName)1 NamespaceBindingException (org.opencastproject.mediapackage.NamespaceBindingException)1