View Javadoc

1   package tests;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.File;
6   import java.io.FileReader;
7   import java.io.InputStreamReader;
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  import org.apache.ws.commons.schema.XmlSchema;
12  import org.apache.ws.commons.schema.XmlSchemaCollection;
13  import org.custommonkey.xmlunit.DetailedDiff;
14  import org.custommonkey.xmlunit.Diff;
15  import org.custommonkey.xmlunit.Difference;
16  import org.custommonkey.xmlunit.DifferenceConstants;
17  import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
18  import org.custommonkey.xmlunit.XMLTestCase;
19  import org.w3c.dom.Element;
20  
21  public class NISTSchemaTest extends XMLTestCase {
22  
23      private List failed = new ArrayList();
24  
25      private List passed = new ArrayList();
26  
27      private String schemaLocation = "target/xmlschema2002-01-16/nisttest/NISTTestsAll";
28      
29      public NISTSchemaTest(String name) {
30          super(name);
31          
32          schemaLocation = System.getProperty("nistTestLocation", schemaLocation);
33      }
34  
35      public void testReadWrite() throws Exception {
36          traverse(new File(schemaLocation));
37          
38          System.out.println("Passed: " + passed.size() + "/" + (passed.size() + failed.size()));
39          
40          if (failed.size() > 0) {
41              fail("Some schemas didn't write correctly!");
42          }
43          
44          if (passed.size() == 0) {
45              fail("No schemas were located! Make sure the schema location is correct: " 
46                      + schemaLocation);
47          }
48      }
49  
50      public XmlSchema loadSchema(File f) throws Exception {
51          XmlSchemaCollection col = new XmlSchemaCollection();
52          col.setBaseUri(schemaLocation);
53          XmlSchema xmlSchema = col.read(new FileReader(f), null);
54          return xmlSchema;
55      }
56  
57      public void traverse(File f) throws Exception {
58  
59          if (f.isDirectory()) {
60              File[] files = f.listFiles();
61              for (int i = 0; i < files.length; i++) {
62                  traverse(files[i]);
63              }
64          } else if (f.getAbsolutePath().endsWith("xsd")) {
65              XmlSchema schema = loadSchema(f);
66              ByteArrayOutputStream baos = new ByteArrayOutputStream();
67              schema.write(baos);
68  
69              Diff diff = new Diff(new FileReader(f), new InputStreamReader(
70                      new ByteArrayInputStream(baos.toByteArray())));
71  
72              DetailedDiff detaileddiffs = new DetailedDiff(diff);
73              detaileddiffs.overrideDifferenceListener(new SchemaAttrDiff());
74              boolean result = detaileddiffs.similar();
75  
76              if (result) {
77                  passed.add(f.getName());
78              } else {
79                  failed.add(f.getName());
80                  System.out.println("Failed: " + f.getName());
81              }
82          }
83      }
84  
85      static class SchemaAttrDiff extends
86              IgnoreTextAndAttributeValuesDifferenceListener {
87  
88          public int differenceFound(Difference difference) {
89              
90              if (difference.getId() == DifferenceConstants.ELEMENT_NUM_ATTRIBUTES.getId()) {
91                  // control and test have to be elements
92                  // check if they are schema elements .. they only
93                  // seem to have the added attributeFormDefault and
94                  // elementFormDefault attributes
95                  // so shldnt have more than 2 attributes difference
96                  Element actualEl = (Element) difference.getControlNodeDetail().getNode();
97                  Element testEl = (Element) difference.getTestNodeDetail().getNode();
98                  
99                  if (actualEl.getLocalName().equals("schema")) {
100 
101                     int expectedAttrs = Integer.parseInt(difference.getControlNodeDetail().getValue());
102                     int actualAttrs = Integer.parseInt(difference.getTestNodeDetail().getValue());
103                     if (Math.abs(actualAttrs - expectedAttrs) <= 2) {
104                         return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
105                     }
106                 }
107             }
108             else if (difference.getId() == DifferenceConstants.ATTR_NAME_NOT_FOUND_ID) {
109                 // sometimes the serializer throws in a few extra attributes...
110                 Element actualEl = (Element) difference.getControlNodeDetail().getNode();
111                 
112                 if (actualEl.getLocalName().equals("schema")) {
113                     return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
114                 }
115             }
116             
117             return super.differenceFound(difference);
118         }
119     }
120 
121 }