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());
}
}
Aggregations