DomContentHandlerTests.java 3.5 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2017 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed 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 org.springframework.util.xml;

19 20 21 22
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

A
Arjen Poutsma 已提交
23 24 25 26 27 28
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
29

30 31
import static org.junit.Assert.*;
import static org.xmlunit.matchers.CompareMatcher.*;
32

33 34 35 36
/**
 * Unit tests for {@link DomContentHandler}.
 */
public class DomContentHandlerTests {
A
Arjen Poutsma 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49

	private static final String XML_1 =
			"<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" + "<root xmlns='namespace'>" +
					"<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
					"</root>";

	private static final String XML_2_EXPECTED =
			"<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" + "<child xmlns='namespace2' />" +
					"</root>";

	private static final String XML_2_SNIPPET =
			"<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";

50

A
Arjen Poutsma 已提交
51 52 53 54 55 56 57 58 59 60
	private Document expected;

	private DomContentHandler handler;

	private Document result;

	private XMLReader xmlReader;

	private DocumentBuilder documentBuilder;

61

A
Arjen Poutsma 已提交
62
	@Before
63
	@SuppressWarnings("deprecation")  // on JDK 9
A
Arjen Poutsma 已提交
64 65 66 67 68
	public void setUp() throws Exception {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		documentBuilderFactory.setNamespaceAware(true);
		documentBuilder = documentBuilderFactory.newDocumentBuilder();
		result = documentBuilder.newDocument();
69
		xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
A
Arjen Poutsma 已提交
70 71
	}

72

A
Arjen Poutsma 已提交
73 74
	@Test
	public void contentHandlerDocumentNamespacePrefixes() throws Exception {
S
Spring Operator 已提交
75
		xmlReader.setFeature("http://www.xml.org/sax/features/namespace-prefixes", true);
A
Arjen Poutsma 已提交
76 77 78 79
		handler = new DomContentHandler(result);
		expected = documentBuilder.parse(new InputSource(new StringReader(XML_1)));
		xmlReader.setContentHandler(handler);
		xmlReader.parse(new InputSource(new StringReader(XML_1)));
80
		assertThat("Invalid result", result, isSimilarTo(expected));
A
Arjen Poutsma 已提交
81 82 83 84 85 86 87 88
	}

	@Test
	public void contentHandlerDocumentNoNamespacePrefixes() throws Exception {
		handler = new DomContentHandler(result);
		expected = documentBuilder.parse(new InputSource(new StringReader(XML_1)));
		xmlReader.setContentHandler(handler);
		xmlReader.parse(new InputSource(new StringReader(XML_1)));
89
		assertThat("Invalid result", result, isSimilarTo(expected));
A
Arjen Poutsma 已提交
90 91 92 93 94 95 96 97 98 99
	}

	@Test
	public void contentHandlerElement() throws Exception {
		Element rootElement = result.createElementNS("namespace", "root");
		result.appendChild(rootElement);
		handler = new DomContentHandler(rootElement);
		expected = documentBuilder.parse(new InputSource(new StringReader(XML_2_EXPECTED)));
		xmlReader.setContentHandler(handler);
		xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
100
		assertThat("Invalid result", result, isSimilarTo(expected));
A
Arjen Poutsma 已提交
101
	}
102

103
}