|
| 1 | +/* |
| 2 | + * Copyright 2014 Christoph Böhme |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.test; |
| 17 | + |
| 18 | +import javax.xml.parsers.DocumentBuilder; |
| 19 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 20 | +import javax.xml.parsers.ParserConfigurationException; |
| 21 | + |
| 22 | +import org.culturegraph.mf.exceptions.ShouldNeverHappenException; |
| 23 | +import org.junit.Test; |
| 24 | +import org.w3c.dom.Document; |
| 25 | +import org.w3c.dom.Element; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test cases for class {@link TestCase}. |
| 29 | + * |
| 30 | + * @author Christoph Böhme |
| 31 | + * |
| 32 | + */ |
| 33 | +public final class TestCaseTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void shouldSupportFormetaAsInputAndResultFormat() { |
| 37 | + final Document doc = getXmlDocument(); |
| 38 | + |
| 39 | + final Element inputElement = doc.createElement("input"); |
| 40 | + inputElement.setAttribute("type", "text/x-formeta"); |
| 41 | + inputElement.setTextContent("{l: v}"); |
| 42 | + |
| 43 | + final Element resultElement = doc.createElement("result"); |
| 44 | + resultElement.setAttribute("type", "text/x-formeta"); |
| 45 | + resultElement.setTextContent("{l: v}"); |
| 46 | + |
| 47 | + final Element config = doc.createElement("test-case"); |
| 48 | + config.appendChild(inputElement); |
| 49 | + config.appendChild(resultElement); |
| 50 | + |
| 51 | + final TestCase testCase = new TestCase(config); |
| 52 | + testCase.run(); |
| 53 | + |
| 54 | + // The test was successful if run does not throw |
| 55 | + // an exception. |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void shouldSupportCGXmlAsInputAndResultFormat() { |
| 60 | + final Document doc = getXmlDocument(); |
| 61 | + |
| 62 | + final Element inputRecord = doc.createElement("record"); |
| 63 | + inputRecord.setAttribute("id", "1"); |
| 64 | + |
| 65 | + final Element inputElement = doc.createElement("input"); |
| 66 | + inputElement.setAttribute("type", "text/x-cg+xml"); |
| 67 | + inputElement.appendChild(inputRecord); |
| 68 | + |
| 69 | + final Element resultRecord = doc.createElement("record"); |
| 70 | + resultRecord.setAttribute("id", "1"); |
| 71 | + |
| 72 | + final Element resultElement = doc.createElement("result"); |
| 73 | + resultElement.setAttribute("type", "text/x-cg+xml"); |
| 74 | + resultElement.appendChild(resultRecord); |
| 75 | + |
| 76 | + final Element config = doc.createElement("test-case"); |
| 77 | + config.appendChild(inputElement); |
| 78 | + config.appendChild(resultElement); |
| 79 | + |
| 80 | + final TestCase testCase = new TestCase(config); |
| 81 | + testCase.run(); |
| 82 | + |
| 83 | + // The test was successful if run does not throw |
| 84 | + // an exception. |
| 85 | + } |
| 86 | + |
| 87 | + private static Document getXmlDocument() { |
| 88 | + final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 89 | + final DocumentBuilder docBuilder; |
| 90 | + try { |
| 91 | + docBuilder = docBuilderFactory.newDocumentBuilder(); |
| 92 | + } catch(final ParserConfigurationException e) { |
| 93 | + throw new ShouldNeverHappenException(e); |
| 94 | + } |
| 95 | + |
| 96 | + return docBuilder.newDocument(); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments