1 package org.codehaus.mojo.jaxb2.schemageneration.postprocessing.schemaenhancement;
2
3 import org.codehaus.plexus.util.FileUtils;
4 import org.junit.Assert;
5 import org.junit.Test;
6
7 import javax.xml.XMLConstants;
8 import java.io.File;
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13
14
15
16
17 public class SimpleNamespaceResolverTest {
18
19 public static final String SCHEMA_DIR = "/org/codehaus/mojo/jaxb2/helpers/";
20
21 private File getSchemaFile(String resource) {
22 return FileUtils.toFile(this.getClass().getResource(resource));
23 }
24
25 @Test
26 public void validateCollectingSchemaInfoForSingleNamespaceSchemaFile() {
27
28 final String schemaFile = "yetAnotherSchema.xsd";
29 final File resolvedSchemaFile = getSchemaFile(SCHEMA_DIR + schemaFile);
30 final SimpleNamespaceResolver unitUnderTest = new SimpleNamespaceResolver(resolvedSchemaFile);
31
32
33 final Map<String, String> namespaceURI2PrefixMap = unitUnderTest.getNamespaceURI2PrefixMap();
34
35
36 Assert.assertEquals(schemaFile, unitUnderTest.getSourceFilename());
37 Assert.assertEquals("http://yet/another/namespace", unitUnderTest.getLocalNamespaceURI());
38
39 Assert.assertEquals(1, namespaceURI2PrefixMap.size());
40 Assert.assertEquals("xs", namespaceURI2PrefixMap.get(XMLConstants.W3C_XML_SCHEMA_NS_URI));
41
42 Assert.assertEquals(XMLConstants.W3C_XML_SCHEMA_NS_URI, unitUnderTest.getNamespaceURI("xs"));
43 }
44
45 @Test
46 public void validateCollectingSchemaInfoForMultipleNamespaceSchemaFile() {
47
48 final String schemaFile = "anotherSchema.xsd";
49 final SimpleNamespaceResolver unitUnderTest = new SimpleNamespaceResolver(getSchemaFile(SCHEMA_DIR + schemaFile));
50
51
52 final Map<String, String> namespaceURI2PrefixMap = unitUnderTest.getNamespaceURI2PrefixMap();
53
54
55 Assert.assertEquals(schemaFile, unitUnderTest.getSourceFilename());
56 Assert.assertEquals("http://another/namespace", unitUnderTest.getLocalNamespaceURI());
57
58 Assert.assertEquals(3, namespaceURI2PrefixMap.size());
59 Assert.assertEquals("xs", namespaceURI2PrefixMap.get(XMLConstants.W3C_XML_SCHEMA_NS_URI));
60 Assert.assertEquals("yetAnother", namespaceURI2PrefixMap.get("http://yet/another/namespace"));
61 Assert.assertEquals("some", namespaceURI2PrefixMap.get("http://some/namespace"));
62
63 for (String current : namespaceURI2PrefixMap.keySet()) {
64 final String currentPrefix = namespaceURI2PrefixMap.get(current);
65 Assert.assertEquals(currentPrefix, unitUnderTest.getPrefix(current));
66 }
67 }
68
69 @Test(expected = IllegalArgumentException.class)
70 public void validateExceptionOnEmptyRelativePathToXmlFile() {
71
72 final String incorrectEmpty = "";
73
74
75 new SimpleNamespaceResolver(getSchemaFile(incorrectEmpty));
76 Assert.fail(
77 "Creating a SimpleNamespaceResolver with empty argument " + "should yield an IllegalArgumentException.");
78 }
79
80 @Test
81 public void validateExceptionOnNonexistentXmlSchemaFile() {
82
83 final String nonExistentPath = "this/file/does/not/exist.xml";
84 final File nonExistent = new File(nonExistentPath);
85
86
87 try {
88 new SimpleNamespaceResolver(nonExistent);
89 Assert.fail("Creating a SimpleNamespaceResolver connected to a nonexistent file "
90 + "should yield an IllegalArgumentException.");
91 } catch (IllegalArgumentException e) {
92
93 } catch (Exception e) {
94 Assert.fail("Expected IllegalArgumentException, but received [" + e.getClass().getName() + "]");
95 }
96 }
97
98 @Test
99 public void validateJaxbNamespaceResolverComplianceInThrowingExceptionOnNullNamespaceResolverArguments() {
100
101 final String schemaFile = "yetAnotherSchema.xsd";
102 final SimpleNamespaceResolver unitUnderTest = new SimpleNamespaceResolver(getSchemaFile(SCHEMA_DIR + schemaFile));
103 final String incorrectNull = null;
104
105
106 try {
107 unitUnderTest.getPrefix(incorrectNull);
108 Assert.fail("Running getPrefix with a null argument should yield an IllegalArgumentException.");
109 } catch (IllegalArgumentException e) {
110
111 } catch (Exception e) {
112 Assert.fail("Expected IllegalArgumentException, but received [" + e.getClass().getName() + "]");
113 }
114
115 try {
116 unitUnderTest.getNamespaceURI(incorrectNull);
117 Assert.fail("Running getNamespaceURI with a null argument should yield an IllegalArgumentException.");
118 } catch (IllegalArgumentException e) {
119
120 } catch (Exception e) {
121 Assert.fail("Expected IllegalArgumentException, but received [" + e.getClass().getName() + "]");
122 }
123
124 try {
125 unitUnderTest.getPrefixes(incorrectNull);
126 Assert.fail("Running getPrefixes with a null argument should yield an IllegalArgumentException.");
127 } catch (IllegalArgumentException e) {
128
129 } catch (Exception e) {
130 Assert.fail("Expected IllegalArgumentException, but received [" + e.getClass().getName() + "]");
131 }
132 }
133
134 @Test
135 public void validatePrefixesIterator() {
136
137 final String schemaFile = "yetAnotherSchema.xsd";
138 final SimpleNamespaceResolver unitUnderTest = new SimpleNamespaceResolver(getSchemaFile(SCHEMA_DIR + schemaFile));
139
140
141 List<String> prefixesList = new ArrayList<String>();
142 for (Iterator<String> it = unitUnderTest.getPrefixes(XMLConstants.W3C_XML_SCHEMA_NS_URI); it.hasNext(); ) {
143 prefixesList.add(it.next());
144 }
145
146
147 Assert.assertEquals(1, prefixesList.size());
148 Assert.assertEquals("xs", prefixesList.get(0));
149 }
150 }