Java tutorial
/* * The DecidR Development Team 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 de.decidr.model; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Set; import javax.xml.bind.JAXBElement; import javax.xml.transform.OutputKeys; import org.apache.commons.io.IOUtils; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import de.decidr.model.schema.decidrtypes.DWDLSimpleVariableType; import de.decidr.model.schema.decidrtypes.ObjectFactory; import de.decidr.model.schema.decidrtypes.TActor; import de.decidr.model.schema.decidrtypes.TAssignment; import de.decidr.model.schema.decidrtypes.TConfiguration; import de.decidr.model.schema.decidrtypes.THumanTaskData; import de.decidr.model.schema.decidrtypes.TInformation; import de.decidr.model.schema.decidrtypes.TParameter; import de.decidr.model.schema.decidrtypes.TParameters; import de.decidr.model.schema.decidrtypes.TRole; import de.decidr.model.schema.decidrtypes.TRoles; import de.decidr.model.schema.decidrtypes.TTaskItem; /** * @author Daniel Huss * @version 0.1 */ public class XmlToolsTest { private final Document EMPTY_DOC; private final Document SAMPLE_DOC; private final static String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"; private final static String SAMPLE_XML = "<rootElement attribute=\"example\"><subElement/>" + "<namespaceElement xmlns=\"http://example.invalid\"/></rootElement>"; private final TConfiguration SAMPLE_CONFIGURATION; private final THumanTaskData SAMPLE_TASK; private final byte[] SAMPLE_CONFIGURATION_RESOURCE; public XmlToolsTest() { EMPTY_DOC = XmlTools.createDocument(); SAMPLE_DOC = XmlTools.createDocument(); SAMPLE_CONFIGURATION = new TConfiguration(); SAMPLE_TASK = new THumanTaskData(); SAMPLE_CONFIGURATION_RESOURCE = getResourceAsByteArray("sample_configuration.xml"); fillSampleDocument(); fillSampleConfiguration(); fillSampleHumanTask(); } private void fillSampleDocument() { Element root = SAMPLE_DOC.createElement("rootElement"); root.appendChild(SAMPLE_DOC.createElement("subElement")); root.appendChild(SAMPLE_DOC.createElementNS("http://example.invalid", "namespaceElement")); root.setAttribute("attribute", "example"); SAMPLE_DOC.appendChild(root); } private void fillSampleHumanTask() { SAMPLE_TASK.setParameters(new TParameters()); SAMPLE_TASK.getParameters().getParameter().add(new TParameter()); SAMPLE_TASK.getTaskItemOrInformation().add(new TInformation()); SAMPLE_TASK.getTaskItemOrInformation().add(new TTaskItem()); TTaskItem fileId = new TTaskItem(); fileId.setType(DWDLSimpleVariableType.ANY_URI); fileId.setValue("42"); SAMPLE_TASK.getTaskItemOrInformation().add(fileId); } private void fillSampleConfiguration() { TAssignment[] assigns = new TAssignment[3]; assigns[0] = new TAssignment(); assigns[0].setKey("inttest"); assigns[0].setValueType(DWDLSimpleVariableType.INTEGER.toString()); assigns[0].getValue().add("42"); assigns[1] = new TAssignment(); assigns[1].setKey("fileA"); assigns[1].setValueType(DWDLSimpleVariableType.ANY_URI.toString()); assigns[1].getValue().add("42"); assigns[2] = new TAssignment(); assigns[2].setKey("fileB"); assigns[2].setValueType(DWDLSimpleVariableType.ANY_URI.toString()); assigns[2].getValue().add("23"); for (TAssignment a : assigns) { SAMPLE_CONFIGURATION.getAssignment().add(a); } SAMPLE_CONFIGURATION.setRoles(new TRoles()); SAMPLE_CONFIGURATION.getRoles().getActor().add(new TActor()); SAMPLE_CONFIGURATION.getRoles().getActor().add(new TActor()); SAMPLE_CONFIGURATION.getRoles().getRole().add(new TRole()); } @Test public void testCreateDocument() { Document doc = XmlTools.createDocument(); long start = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { doc = XmlTools.createDocument(); /* * Is the new document empty? */ assertEquals(0, doc.getChildNodes().getLength()); } System.out.println("Tested 100 documents in " + (System.currentTimeMillis() - start) + " milliseconds"); } @Test public void testGetFileIdsTConfiguration() { assertEquals(0, XmlTools.getFileIds(new TConfiguration()).size()); Set<Long> fileIds = XmlTools.getFileIds(SAMPLE_CONFIGURATION); assertEquals(2, fileIds.size()); } @Test public void testGetFileIdsTHumanTaskData() { Set<Long> fileIds = XmlTools.getFileIds(SAMPLE_TASK); assertEquals(1, fileIds.size()); assertEquals(42, (long) fileIds.iterator().next()); } @Test public void testGetXmlStringDocument() throws Exception { String result = XmlTools.getXmlString(EMPTY_DOC); assertNotNull(result); assertTrue(result.startsWith("<?xml")); result = XmlTools.getXmlString(SAMPLE_DOC); assertEquals(XML_DECLARATION + SAMPLE_XML, result); } @Test public void testGetXmlStringDocumentProperties() { Properties properties = new Properties(); properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); String result = XmlTools.getXmlString(SAMPLE_DOC, properties); assertEquals(SAMPLE_XML, result); } private void equalsConfiguration(TConfiguration a, TConfiguration b) { assertEquals(a.getAssignment().size(), b.getAssignment().size()); for (int i = 0; i < a.getAssignment().size(); i++) { assertEquals(a.getAssignment().get(i).getValue(), b.getAssignment().get(i).getValue()); } assertEquals(a.getRoles().getActor().size(), b.getRoles().getActor().size()); assertEquals(a.getRoles().getRole().size(), b.getRoles().getRole().size()); } @Test public void testUnmarshalJaxbEntityByteArrayCharsetClassOfT() throws IOException { TConfiguration result = XmlTools.unmarshalJaxbEntity(SAMPLE_CONFIGURATION_RESOURCE, TConfiguration.class); equalsConfiguration(SAMPLE_CONFIGURATION, result); } @Test public void testUnmarshalJaxbEntityStringClassOfT() { TConfiguration result = XmlTools.unmarshalJaxbEntity(new String(SAMPLE_CONFIGURATION_RESOURCE), TConfiguration.class); equalsConfiguration(SAMPLE_CONFIGURATION, result); } @Test public void testMarshalJaxbEntityIntoByteArray() { JAXBElement<TConfiguration> foo = new ObjectFactory().createConfigurations(SAMPLE_CONFIGURATION); String result = new String(XmlTools.marshalJaxbEntityIntoByteArray(foo)); String expected = new String(SAMPLE_CONFIGURATION_RESOURCE); assertEquals(expected.trim(), result.trim()); } @Test public void testMarshalJaxbEntityIntoString() { JAXBElement<TConfiguration> foo = new ObjectFactory().createConfigurations(SAMPLE_CONFIGURATION); String result = XmlTools.marshalJaxbEntityIntoString(foo); String expected = new String(SAMPLE_CONFIGURATION_RESOURCE); assertEquals(expected.trim(), result.trim()); } /** * Uses the thread context classloader to load the specified resource into a * byte array. Returns <code>null</code> if the resource isn't found. */ private byte[] getResourceAsByteArray(String name) { InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); try { try { if (stream == null) { return null; } ByteArrayOutputStream result = new ByteArrayOutputStream(stream.available()); IOUtils.copy(stream, result); return result.toByteArray(); } finally { if (stream instanceof InputStream) { stream.close(); } } } catch (IOException e) { throw new RuntimeException(e); } } }