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