| 1 | | | /* |
| 2 | | | * $Id: SxpJaxbTest.java.disabled 1 2006-11-25 14:41:52Z amandel $ |
| 3 | | | * |
| 4 | | | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
| 5 | | | * |
| 6 | | | * Redistribution and use in source and binary forms, with or without |
| 7 | | | * modification, are permitted provided that the following conditions are |
| 8 | | | * met: |
| 9 | | | * |
| 10 | | | * * Redistributions of source code must retain the above copyright |
| 11 | | | * notice, this list of conditions and the following disclaimer. |
| 12 | | | * * Redistributions in binary form must reproduce the above |
| 13 | | | * copyright notice, this list of conditions and the following |
| 14 | | | * disclaimer in the documentation and/or other materials |
| 15 | | | * provided with the distribution. |
| 16 | | | * * Neither the name of the jCoderZ.org Project nor the names of |
| 17 | | | * its contributors may be used to endorse or promote products |
| 18 | | | * derived from this software without specific prior written |
| 19 | | | * permission. |
| 20 | | | * |
| 21 | | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
| 22 | | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
| 25 | | | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | | | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | | | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 30 | | | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 31 | | | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | | | */ |
| 33 | | | package org.jcoderz.commons.sxp.jaxb; |
| 34 | | | |
| 35 | | | import java.io.ByteArrayInputStream; |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FileInputStream; |
| 38 | | | import java.io.FilenameFilter; |
| 39 | | | import java.io.IOException; |
| 40 | | | import java.io.InputStream; |
| 41 | | | import java.io.StringWriter; |
| 42 | | | import java.io.UnsupportedEncodingException; |
| 43 | | | import java.util.HashMap; |
| 44 | | | import java.util.Map; |
| 45 | | | |
| 46 | | | import javax.xml.bind.JAXBException; |
| 47 | | | import javax.xml.bind.Marshaller; |
| 48 | | | import javax.xml.bind.PropertyException; |
| 49 | | | import javax.xml.bind.Unmarshaller; |
| 50 | | | import javax.xml.bind.Validator; |
| 51 | | | import javax.xml.parsers.ParserConfigurationException; |
| 52 | | | |
| 53 | | | import junit.framework.Test; |
| 54 | | | import junit.framework.TestSuite; |
| 55 | | | |
| 56 | | | import org.custommonkey.xmlunit.Diff; |
| 57 | | | import org.custommonkey.xmlunit.XMLTestCase; |
| 58 | | | import org.custommonkey.xmlunit.XMLUnit; |
| 59 | | | import org.jcoderz.commons.TestCase; |
| 60 | | | import org.jcoderz.commons.csp.sxp.SxpJaxbContext; |
| 61 | | | import org.jcoderz.commons.csp.sxp.ValidationEventCollector; |
| 62 | | | import org.jcoderz.commons.util.IoUtil; |
| 63 | | | import org.xml.sax.SAXException; |
| 64 | | | |
| 65 | | | |
| 66 | | | /** |
| 67 | | | * Test all supplied xml files to be parsed correctly and that we |
| 68 | | | * are able to marshal them correctly back into xml. |
| 69 | | | * @author Andreas Mandel |
| 70 | | | */ |
| 71 | | | public class SxpJaxbTest |
| 72 | | | extends TestCase |
| 73 | | | { |
| 74 | | | private static final String XML_DIRECTORY |
| 75 | | | = "test/xml/sxp/wellformed"; |
| 76 | | | private static final String XML_DIRECTORY_MALFORMED |
| 77 | | | = "test/xml/sxp/malformed"; |
| 78 | | | |
| 79 | | | /** A map containing the expected error messages for the negative tests. */ |
| 80 | | | private static final Map ERROR_MESSAGES = new HashMap(); |
| 81 | | | |
| 82 | | | static |
| 83 | | | { |
| 84 | | | ERROR_MESSAGES.put( |
| 85 | | | "WrongNamespace.xml", |
| 86 | | | new String[] {".*namespace.*"}); |
| 87 | | | } |
| 88 | | | |
| 89 | | | /** |
| 90 | | | * Generating this test suite. |
| 91 | | | * @return this test suite. |
| 92 | | | */ |
| 93 | | | public static Test suite () |
| 94 | | | { |
| 95 | | | final File dir = new File(TestCase.getBaseDir(), XML_DIRECTORY); |
| 96 | | | String[] xmlFiles = dir.list(new XmlFilenameFilter()); |
| 97 | | | if (xmlFiles == null) |
| 98 | | | { |
| 99 | | | throw new RuntimeException("[Error] " + "No XML files found at '" |
| 100 | | | + dir + "'!"); |
| 101 | | | } |
| 102 | | | final TestSuite suite = new TestSuite(); |
| 103 | | | for (int i = 0; i < xmlFiles.length; i++) |
| 104 | | | { |
| 105 | | | suite.addTest(new XmlFileValidation(dir, xmlFiles[i], true)); |
| 106 | | | } |
| 107 | | | |
| 108 | | | final File dirMalformed |
| 109 | | | = new File(TestCase.getBaseDir(), XML_DIRECTORY_MALFORMED); |
| 110 | | | xmlFiles = dirMalformed.list(new XmlFilenameFilter()); |
| 111 | | | if (xmlFiles == null) |
| 112 | | | { |
| 113 | | | throw new RuntimeException("[Error] " + "No XML files found at '" |
| 114 | | | + dirMalformed + "'!"); |
| 115 | | | } |
| 116 | | | for (int i = 0; i < xmlFiles.length; i++) |
| 117 | | | { |
| 118 | | | suite.addTest(new XmlFileValidation(dirMalformed, xmlFiles[i], false)); |
| 119 | | | } |
| 120 | | | return suite; |
| 121 | | | } |
| 122 | | | |
| 123 | | | static class XmlFilenameFilter |
| 124 | | | implements FilenameFilter |
| 125 | | | { |
| 126 | | | /** |
| 127 | | | * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String) |
| 128 | | | */ |
| 129 | | | public boolean accept (File dir, String name) |
| 130 | | | { |
| 131 | | | return name.endsWith(".xml"); |
| 132 | | | } |
| 133 | | | } |
| 134 | | | |
| 135 | | | |
| 136 | | | static class XmlFileValidation |
| 137 | | | extends XMLTestCase |
| 138 | | | { |
| 139 | | | private static final SxpJaxbContext JAXB_CONTEXT |
| 140 | | | = new SxpJaxbContext(); |
| 141 | | | private final String mXmlFileName; |
| 142 | | | private final File mDir; |
| 143 | | | private final boolean mIsPositive; |
| 144 | | | |
| 145 | | | public XmlFileValidation (File dir, String xmlFileName, boolean positive) |
| 146 | | | { |
| 147 | | | super(xmlFileName); |
| 148 | | | mXmlFileName = xmlFileName; |
| 149 | | | mDir = dir; |
| 150 | | | mIsPositive = positive; |
| 151 | | | } |
| 152 | | | |
| 153 | | | public void runTest () |
| 154 | | | throws Throwable |
| 155 | | | { |
| 156 | | | testXmlFile(); |
| 157 | | | } |
| 158 | | | |
| 159 | | | public void testXmlFile () |
| 160 | | | throws JAXBException, SAXException, IOException, |
| 161 | | | ParserConfigurationException |
| 162 | | | { |
| 163 | | | final InputStream in |
| 164 | | | = new FileInputStream(new File(mDir, mXmlFileName)); |
| 165 | | | final byte[] data; |
| 166 | | | try |
| 167 | | | { |
| 168 | | | data = IoUtil.readFully(in); |
| 169 | | | } |
| 170 | | | finally |
| 171 | | | { |
| 172 | | | IoUtil.close(in); |
| 173 | | | } |
| 174 | | | |
| 175 | | | JAXB_CONTEXT.getValidationEventCollector().reset(); |
| 176 | | | |
| 177 | | | Object result = null; |
| 178 | | | final Unmarshaller unmarshaller = JAXB_CONTEXT.getUnmarshaller(); |
| 179 | | | try |
| 180 | | | { |
| 181 | | | result = unmarshaller.unmarshal(new ByteArrayInputStream(data)); |
| 182 | | | } |
| 183 | | | catch (Exception ex) |
| 184 | | | { |
| 185 | | | assertTrue("Exception but no event." + ex, |
| 186 | | | JAXB_CONTEXT.getValidationEventCollector().hasEvents()); |
| 187 | | | } |
| 188 | | | if (mIsPositive) |
| 189 | | | { |
| 190 | | | assertPositiveResult(data, result); |
| 191 | | | } |
| 192 | | | else |
| 193 | | | { |
| 194 | | | assertNegativeResult(data, result); |
| 195 | | | } |
| 196 | | | } |
| 197 | | | |
| 198 | | | private void assertNegativeResult (final byte[] data, Object result) |
| 199 | | | throws UnsupportedEncodingException |
| 200 | | | { |
| 201 | | | final ValidationEventCollector |
| 202 | | | eventCollector = JAXB_CONTEXT.getValidationEventCollector(); |
| 203 | | | assertTrue("Expected errors for document: '" |
| 204 | | | + new String(data, "UTF-8") + "'", |
| 205 | | | eventCollector.hasEvents()); |
| 206 | | | final String message = eventCollector.toString(); |
| 207 | | | final String[] expectedMessage |
| 208 | | | = (String[]) ERROR_MESSAGES.get(mXmlFileName); |
| 209 | | | if (expectedMessage == null) |
| 210 | | | { |
| 211 | | | fail("No expected message defined for negative test. " |
| 212 | | | + " Document name " + mXmlFileName |
| 213 | | | + " was " + (result == null ? "null" : "not null") |
| 214 | | | + ". The error message was:\n" + message); |
| 215 | | | } |
| 216 | | | for (int i = 0; i < expectedMessage.length; i++) |
| 217 | | | { |
| 218 | | | if (!message.matches(expectedMessage[i])) |
| 219 | | | { |
| 220 | | | fail("Expected message part '" + expectedMessage[i] |
| 221 | | | + "' not found in message. " |
| 222 | | | + " Document name " + mXmlFileName |
| 223 | | | + " was " + (result == null ? "null" : "not null") |
| 224 | | | + ". The error message was:\n" + message); |
| 225 | | | } |
| 226 | | | } |
| 227 | | | } |
| 228 | | | |
| 229 | | | private void assertPositiveResult (final byte[] data, Object result) |
| 230 | | | throws JAXBException, PropertyException, SAXException, IOException, |
| 231 | | | ParserConfigurationException, UnsupportedEncodingException |
| 232 | | | { |
| 233 | | | final ValidationEventCollector |
| 234 | | | eventCollector = JAXB_CONTEXT.getValidationEventCollector(); |
| 235 | | | assertFalse("Unmarshal got Events: '" + eventCollector.toString(), |
| 236 | | | eventCollector.hasEvents()); |
| 237 | | | |
| 238 | | | final Validator validator = JAXB_CONTEXT.getValidator(); |
| 239 | | | validator.validate(result); |
| 240 | | | assertFalse("Validate got Events:" + eventCollector.toString(), |
| 241 | | | eventCollector.hasEvents()); |
| 242 | | | |
| 243 | | | final Marshaller marshaller = JAXB_CONTEXT.getMarshaller(); |
| 244 | | | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); |
| 245 | | | final StringWriter sw = new StringWriter(); |
| 246 | | | marshaller.marshal(result, sw); |
| 247 | | | assertFalse("Marshal got Events: '" + eventCollector.toString(), |
| 248 | | | eventCollector.hasEvents()); |
| 249 | | | XMLUnit.setIgnoreWhitespace(true); |
| 250 | | | final Diff diff = new Diff(new String(data, "UTF-8"), sw.toString()); |
| 251 | | | if (!diff.similar()) |
| 252 | | | { |
| 253 | | | fail("Not similar (" + diff.toString() |
| 254 | | | + ") found diff between\n=============================\n" |
| 255 | | | + new String(data, "UTF-8") |
| 256 | | | + "\n=======A=N=D===================\n" + sw.toString() |
| 257 | | | + "\n===============================\n\n"); |
| 258 | | | } |
| 259 | | | } |
| 260 | | | } |
| 261 | | | } |