View Javadoc
1   /*
2    * Copyright (c) 2013-2014, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, 
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
6    */
7   package nl.mineleni.cbsviewer.servlet.wms;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertTrue;
11  import static org.junit.Assert.fail;
12  import static org.junit.Assume.assumeNotNull;
13  
14  import java.io.File;
15  
16  import javax.xml.transform.Source;
17  import javax.xml.transform.stream.StreamSource;
18  
19  import org.custommonkey.xmlunit.XMLUnit;
20  import org.custommonkey.xmlunit.exceptions.ConfigurationException;
21  import org.custommonkey.xmlunit.exceptions.XMLUnitRuntimeException;
22  import org.custommonkey.xmlunit.jaxp13.Validator;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  /**
27   * Test case voor {@link AttributeValuesFilter}.
28   * 
29   * @author prinsmc
30   */
31  public class AttributeValuesFilterTest {
32  	/** test subject. */
33  	private AttributeValuesFilter filter;
34  
35  	/**
36  	 * set up.
37  	 * 
38  	 * @throws Exception
39  	 *             the exception
40  	 */
41  	@Before
42  	public void setUp() throws Exception {
43  		XMLUnit.setIgnoreWhitespace(true);
44  		XMLUnit.setIgnoreComments(true);
45  		this.filter = new AttributeValuesFilter();
46  	}
47  
48  	/**
49  	 * Test voor {@link AttributeValuesFilter#hasFilters()} .
50  	 */
51  	@Test
52  	public void testHasFilters() {
53  		assertTrue(this.filter.hasFilters());
54  	}
55  
56  	/**
57  	 * Test voor {@link AttributeValuesFilter#filterValue(Object)} .
58  	 */
59  	@Test
60  	public void testFilterValue() {
61  		assertEquals("expected", this.filter.filterValue("expected"));
62  		assertEquals("geheim", this.filter.filterValue(-99997));
63  	}
64  
65  	/**
66  	 * XML testcase voor deployment {@code validAttributeValuesFilter.xml} en
67  	 * {@code invalidAttributeValuesFilter.xml}.
68  	 */
69  	@Test
70  	public void testAttributeValuesFilterXML() {
71  		// valideer document
72  		final Validator v = new Validator();
73  		final Source schema = new StreamSource(this.getClass().getClassLoader()
74  				.getResourceAsStream("AttributeValuesFilter.xsd"));
75  		v.addSchemaSource(schema);
76  
77  		// test resources
78  		final Source validdoc = new StreamSource(this.getClass()
79  				.getClassLoader()
80  				.getResourceAsStream("validAttributeValuesFilter.xml"));
81  		assertTrue(v.isInstanceValid(validdoc));
82  
83  		final Source invaliddoc = new StreamSource(this.getClass()
84  				.getClassLoader()
85  				.getResourceAsStream("invalidAttributeValuesFilter.xml"));
86  		try {
87  			v.isInstanceValid(invaliddoc);
88  			fail("Expected excepion did not occur.");
89  		} catch (final XMLUnitRuntimeException x) {
90  			assertEquals("Schema is invalid", x.getMessage());
91  		}
92  	}
93  
94  	/**
95  	 * XML testcase voor {@code AttributeValuesFilter.xsd}.
96  	 */
97  	@Test
98  	public void testAttributeValuesFilterXSD() {
99  		try {
100 			// valideer xsd
101 			final Source schema = new StreamSource(this.getClass()
102 					.getClassLoader()
103 					.getResourceAsStream("AttributeValuesFilter.xsd"));
104 			final Validator v = new Validator();
105 			v.addSchemaSource(schema);
106 			assertTrue(v.isSchemaValid());
107 		} catch (final ConfigurationException e) {
108 			fail(e.getMessage());
109 		}
110 	}
111 
112 	/**
113 	 * XML testcase voor deployment {@code AttributeValuesFilter.xml}.
114 	 */
115 	@Test
116 	public void testDeploymentAttributeValuesFilterXML() {
117 		// valideer document
118 		final Validator v = new Validator();
119 		final Source schema = new StreamSource(this.getClass().getClassLoader()
120 				.getResourceAsStream("AttributeValuesFilter.xsd"));
121 		v.addSchemaSource(schema);
122 		// deployment resource
123 		// final Source doc = new StreamSource(Thread.currentThread()
124 		// .getContextClassLoader()
125 		// .getResourceAsStream("../classes/AttributeValuesFilter.xml"));
126 		final Source doc = new StreamSource(new File(
127 				"target/classes/AttributeValuesFilter.xml"));
128 		assumeNotNull(doc);
129 		assertTrue(v.isInstanceValid(doc));
130 
131 	}
132 }