View Javadoc
1   package org.codehaus.mojo.jaxb2.schemageneration.postprocessing;
2   
3   import java.io.File;
4   import java.io.StringReader;
5   import java.net.URL;
6   import java.util.ArrayList;
7   import java.util.List;
8   import java.util.Map;
9   
10  import javax.xml.transform.TransformerFactory;
11  
12  import org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper;
13  import org.codehaus.mojo.jaxb2.schemageneration.postprocessing.schemaenhancement.ChangeNamespacePrefixProcessor;
14  import org.codehaus.mojo.jaxb2.schemageneration.postprocessing.schemaenhancement.SimpleNamespaceResolver;
15  import org.codehaus.mojo.jaxb2.schemageneration.postprocessing.schemaenhancement.TransformSchema;
16  import org.junit.Assert;
17  
18  import org.apache.maven.plugin.MojoExecutionException;
19  import org.custommonkey.xmlunit.Diff;
20  import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
21  import org.custommonkey.xmlunit.XMLAssert;
22  import org.custommonkey.xmlunit.XMLUnit;
23  import org.junit.Test;
24  import org.w3c.dom.Document;
25  
26  /**
27   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>
28   */
29  public class XsdGeneratorHelperTest
30  {
31      private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
32  
33      static
34      {
35          // Configure XMLUnit.
36          XMLUnit.setIgnoreWhitespace( true );
37          XMLUnit.setIgnoreAttributeOrder( true );
38  
39          // Configure the TransformerFactory
40          FACTORY.setAttribute( "indent-number", 2 );
41      }
42  
43      @Test( expected = MojoExecutionException.class )
44      public void validateExceptionThrownOnDuplicateURIs()
45          throws MojoExecutionException
46      {
47          // Assemble
48          final TransformSchema transformSchema1 = new TransformSchema( "foo", "foo", "foo" );
49          final TransformSchema transformSchema2 = new TransformSchema( "foo", "bar", "bar" );
50  
51          final List<TransformSchema> transformSchemas = new ArrayList<TransformSchema>();
52          transformSchemas.add( transformSchema1 );
53          transformSchemas.add( transformSchema2 );
54  
55          // Act & Assert
56          XsdGeneratorHelper.validateSchemasInPluginConfiguration(transformSchemas);
57          Assert.fail( "Two schemas with same URIs should yield a MojoExecutionException." );
58      }
59  
60      @Test( expected = MojoExecutionException.class )
61      public void validateExceptionThrownOnDuplicatePrefixes()
62          throws MojoExecutionException
63      {
64  
65          // Assemble
66          final TransformSchema transformSchema1 = new TransformSchema( "foo", "foo", "foo" );
67          final TransformSchema transformSchema2 = new TransformSchema( "bar", "foo", "bar" );
68  
69          final List<TransformSchema> transformSchemas = new ArrayList<TransformSchema>();
70          transformSchemas.add( transformSchema1 );
71          transformSchemas.add( transformSchema2 );
72  
73          // Act & Assert
74          XsdGeneratorHelper.validateSchemasInPluginConfiguration(transformSchemas);
75          Assert.fail( "Two schemas with same Prefixes should yield a MojoExecutionException." );
76      }
77  
78      @Test
79      public void validateNoExceptionThrownOnDuplicateNullPrefixes()
80      {
81          // Assemble
82          final TransformSchema transformSchema1 = new TransformSchema( "foo", null, "foo" );
83          final TransformSchema transformSchema2 = new TransformSchema( "bar", null, "bar" );
84  
85          final List<TransformSchema> transformSchemas = new ArrayList<TransformSchema>();
86          transformSchemas.add( transformSchema1 );
87          transformSchemas.add( transformSchema2 );
88  
89          // Act & Assert
90          try
91          {
92              XsdGeneratorHelper.validateSchemasInPluginConfiguration(transformSchemas);
93          }
94          catch ( MojoExecutionException e )
95          {
96              Assert.fail( "Two schemas with null Prefix should not yield a MojoExecutionException." );
97          }
98      }
99  
100     @Test
101     public void validateExceptionThrownOnDuplicateFiles()
102     {
103         // Assemble
104         final TransformSchema transformSchema1 = new TransformSchema( "foo", "foo", "foo.xsd" );
105         final TransformSchema transformSchema2 = new TransformSchema( "bar", "bar", "foo.xsd" );
106 
107         final List<TransformSchema> transformSchemas = new ArrayList<TransformSchema>();
108         transformSchemas.add( transformSchema1 );
109         transformSchemas.add( transformSchema2 );
110 
111         // Act & Assert
112         try
113         {
114             XsdGeneratorHelper.validateSchemasInPluginConfiguration(transformSchemas);
115             Assert.fail( "Two schemas with same Files should yield a MojoExecutionException." );
116         }
117         catch ( MojoExecutionException e )
118         {
119             // Validate the error message.
120             String expectedMessage = "Misconfiguration detected: Duplicate 'file' property with value [foo.xsd] "
121                 + "found in plugin configuration. Correct schema elements index (0) and (1), "
122                 + "to ensure that all 'file' values are unique.";
123             Assert.assertEquals( expectedMessage, e.getLocalizedMessage() );
124         }
125     }
126 
127     @Test( expected = MojoExecutionException.class )
128     public void validateExceptionThrownOnOnlyUriGiven()
129         throws MojoExecutionException
130     {
131         // Assemble
132         final TransformSchema transformSchema1 = new TransformSchema( "foo", null, "" );
133 
134         final List<TransformSchema> transformSchemas = new ArrayList<TransformSchema>();
135         transformSchemas.add( transformSchema1 );
136 
137         // Act & Assert
138         XsdGeneratorHelper.validateSchemasInPluginConfiguration(transformSchemas);
139         Assert.fail( "A schema definition with no prefix or file should yield a MojoExecutionException." );
140     }
141 
142     @Test( expected = MojoExecutionException.class )
143     public void validateExceptionThrownOnNullUri()
144         throws MojoExecutionException
145     {
146         // Assemble
147         final TransformSchema transformSchema1 = new TransformSchema( null, "foo", "bar" );
148 
149         final List<TransformSchema> transformSchemas = new ArrayList<TransformSchema>();
150         transformSchemas.add( transformSchema1 );
151 
152         // Act & Assert
153         XsdGeneratorHelper.validateSchemasInPluginConfiguration(transformSchemas);
154         Assert.fail( "A schema definition with null URI should yield a MojoExecutionException." );
155     }
156 
157     @Test( expected = MojoExecutionException.class )
158     public void validateExceptionThrownOnEmptyUri()
159         throws MojoExecutionException
160     {
161         // Assemble
162         final TransformSchema transformSchema1 = new TransformSchema( "", "foo", "bar" );
163 
164         final List<TransformSchema> transformSchemas = new ArrayList<TransformSchema>();
165         transformSchemas.add( transformSchema1 );
166 
167         // Act & Assert
168         XsdGeneratorHelper.validateSchemasInPluginConfiguration(transformSchemas);
169         Assert.fail( "A schema definition with empty URI should yield a MojoExecutionException." );
170     }
171 
172     @Test
173     public void validateProcessingNodes()
174     {
175         // Assemble
176         final String newPrefix = "changedFoo";
177         final String oldPrefix = "foo";
178         final String originalXml = getXmlDocumentSample( oldPrefix );
179         final String changedXml = getXmlDocumentSample( newPrefix );
180         final NodeProcessor changeNamespacePrefixProcessor = new ChangeNamespacePrefixProcessor( oldPrefix, newPrefix );
181 
182         // Act
183         final Document processedDocument = XsdGeneratorHelper.parseXmlStream(new StringReader(originalXml));
184         XsdGeneratorHelper.process(processedDocument.getFirstChild(), true, changeNamespacePrefixProcessor);
185 
186         // Assert
187         final Document expectedDocument = XsdGeneratorHelper.parseXmlStream(new StringReader(changedXml));
188         final Diff diff = new Diff( expectedDocument, processedDocument, null, new ElementNameAndAttributeQualifier() );
189         diff.overrideElementQualifier( new ElementNameAndAttributeQualifier() );
190 
191         XMLAssert.assertXMLEqual( processedDocument, expectedDocument );
192     }
193 
194     @Test
195     public void validateAcquiringFilenameToResolverMap()
196         throws MojoExecutionException
197     {
198         // Assemble
199         final String[] expectedFilenames = { "schema1.xsd", "schema2.xsd", "schema3.xsd" };
200         final URL tmpUrl = getClass().getClassLoader().getResource( "generated/schema/schema1.xsd" );
201         final File directory = new File( tmpUrl.getFile() ).getParentFile();
202 
203         // Act
204         final Map<String, SimpleNamespaceResolver> fileNameToResolverMap =
205             XsdGeneratorHelper.getFileNameToResolverMap(directory);
206 
207         // Assert
208         Assert.assertEquals( 3, fileNameToResolverMap.size() );
209         for ( String current : expectedFilenames )
210         {
211             Assert.assertTrue( fileNameToResolverMap.keySet().contains( current ) );
212         }
213 
214         SimpleNamespaceResolver schema1Resolver = fileNameToResolverMap.get( "schema1.xsd" );
215         Assert.assertEquals( "http://yet/another/namespace", schema1Resolver.getLocalNamespaceURI() );
216         Assert.assertEquals( "schema1.xsd", schema1Resolver.getSourceFilename() );
217         final Map<String, String> schema1NamespaceURI2PrefixMap = schema1Resolver.getNamespaceURI2PrefixMap();
218         Assert.assertEquals( 1, schema1NamespaceURI2PrefixMap.size() );
219         Assert.assertEquals( "xs", schema1NamespaceURI2PrefixMap.get( "http://www.w3.org/2001/XMLSchema" ) );
220 
221         SimpleNamespaceResolver schema2Resolver = fileNameToResolverMap.get( "schema2.xsd" );
222         Assert.assertEquals( "http://some/namespace", schema2Resolver.getLocalNamespaceURI() );
223         Assert.assertEquals( "schema2.xsd", schema2Resolver.getSourceFilename() );
224         final Map<String, String> schema2NamespaceURI2PrefixMap = schema2Resolver.getNamespaceURI2PrefixMap();
225         Assert.assertEquals( 2, schema2NamespaceURI2PrefixMap.size() );
226         Assert.assertEquals( "ns1", schema2NamespaceURI2PrefixMap.get( "http://another/namespace" ) );
227         Assert.assertEquals( "xs", schema2NamespaceURI2PrefixMap.get( "http://www.w3.org/2001/XMLSchema" ) );
228 
229         SimpleNamespaceResolver schema3Resolver = fileNameToResolverMap.get( "schema3.xsd" );
230         Assert.assertEquals( "http://another/namespace", schema3Resolver.getLocalNamespaceURI() );
231         Assert.assertEquals( "schema3.xsd", schema3Resolver.getSourceFilename() );
232         final Map<String, String> schema3NamespaceURI2PrefixMap = schema3Resolver.getNamespaceURI2PrefixMap();
233         Assert.assertEquals( 3, schema3NamespaceURI2PrefixMap.size() );
234         Assert.assertEquals( "ns2", schema3NamespaceURI2PrefixMap.get( "http://yet/another/namespace" ) );
235         Assert.assertEquals( "ns1", schema3NamespaceURI2PrefixMap.get( "http://some/namespace" ) );
236         Assert.assertEquals( "xs", schema3NamespaceURI2PrefixMap.get( "http://www.w3.org/2001/XMLSchema" ) );
237     }
238 
239     //
240     // Private helpers
241     //
242 
243     private String getXmlDocumentSample( final String namespace )
244     {
245         return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
246             + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" + "           xmlns:" + namespace
247             + "=\"http://the/foo/namespace\" \n"
248             // + "           targetNamespace=\"http://yet/another/namespace\"\n"
249             + "           version=\"1.0\">\n"
250             + "    <xs:element name=\"aRequiredElementInYetAnotherNamespace\" type=\"xs:string\"/>\n" + "    <"
251             + namespace + ":aBar name=\"aFooElement\" />\n" + "</xs:schema>\n";
252     }
253 }