1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package javax.jdo.schema;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.FilenameFilter;
22  
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import javax.jdo.JDOFatalException;
27  import javax.jdo.util.AbstractTest;
28  import javax.jdo.util.BatchTestRunner;
29  import javax.jdo.util.XMLTestUtil;
30  
31  import javax.xml.parsers.*;
32  import org.w3c.dom.Document;
33  import org.xml.sax.*;
34  import org.xml.sax.helpers.*;
35  
36  /***
37   * Tests schema files.
38   * <p>
39   */
40  public class XMLTest extends AbstractTest {
41  
42      /*** */
43      protected static String BASEDIR = System.getProperty("basedir", ".");
44  
45      /*** File prefix */
46      protected static final String FILE_PREFIX = BASEDIR + "/test/schema/";
47  
48      /*** */
49      protected static final File JDO_XSD_FILE = 
50          new File(BASEDIR + "/target/classes/javax/jdo/jdo.xsd");
51  
52      /*** */
53      protected static final File ORM_XSD_FILE = 
54          new File(BASEDIR + "/target/classes/javax/jdo/orm.xsd");
55  
56      /*** */
57      protected static final File JDOQUERY_XSD_FILE = 
58          new File(BASEDIR + "/target/classes/javax/jdo/jdoquery.xsd");
59  
60      /*** .xsd files */
61      protected static final File[] XSD_FILES = 
62          new File[] {JDO_XSD_FILE, ORM_XSD_FILE, JDOQUERY_XSD_FILE};
63      
64      /*** XSD metadata files. */
65      protected static File[] positiveXSDJDO = getFiles("Positive", "-xsd.jdo");
66      protected static File[] negativeXSDJDO = getFiles("Negative", "-xsd.jdo");
67      protected static File[] positiveXSDORM = getFiles("Positive", "-xsd.orm");
68      protected static File[] negativeXSDORM = getFiles("Negative", "-xsd.orm");
69      protected static File[] positiveXSDJDOQUERY = getFiles("Positive", "-xsd.jdoquery");
70      protected static File[] negativeXSDJDOQUERY = getFiles("Negative", "-xsd.jdoquery");
71      
72      /*** DTD metadata files. */
73      protected static File[] positiveDTDJDO = getFiles("Positive", "-dtd.jdo");
74      protected static File[] negativeDTDJDO = getFiles("Negative", "-dtd.jdo");
75      protected static File[] positiveDTDORM = getFiles("Positive", "-dtd.orm");
76      protected static File[] negativeDTDORM = getFiles("Negative", "-dtd.orm");
77      protected static File[] positiveDTDJDOQUERY = getFiles("Positive", "-dtd.jdoquery");
78      protected static File[] negativeDTDJDOQUERY = getFiles("Negative", "-dtd.jdoquery");
79      
80      /*** Returns array of files of matching file names. */
81      protected static File[] getFiles(final String prefix, final String suffix) {
82          FilenameFilter filter = new FilenameFilter () {
83              public boolean accept(File file, String name) {
84                  return (name.startsWith(prefix) && name.endsWith(suffix));
85              }
86          };
87          File dir = new File(FILE_PREFIX);
88          return dir.listFiles(filter);
89      }
90  
91      /*** */
92      public static void main(String args[]) {
93          BatchTestRunner.run(XMLTest.class);
94      }
95  
96      /*** Test XSD files jdo.xsd, orm.xsd, and jdoquery.xsd. */
97      public void testXSD() {
98          XMLTestUtil util = new XMLTestUtil();
99          appendMessage(util.checkXMLNonValidating(XSD_FILES));
100         failOnError();
101     }
102 
103     /*** Test XSD based .jdo, .orm and .jdoquery files. */
104     public void testXSDBased() {
105         XMLTestUtil util = new XMLTestUtil();
106         appendMessage(util.checkXML(positiveXSDJDO, true));
107         appendMessage(util.checkXML(negativeXSDJDO, false));
108         appendMessage(util.checkXML(positiveXSDORM, true));
109         appendMessage(util.checkXML(negativeXSDORM, false));
110         appendMessage(util.checkXML(positiveXSDJDOQUERY, true));
111         appendMessage(util.checkXML(negativeXSDJDOQUERY, false));
112         failOnError();
113     }
114 
115     /*** Test DTD based .jdo, .orm and .jdoquery files. */
116     public void testDTDBased() {
117         XMLTestUtil util = new XMLTestUtil();
118         appendMessage(util.checkXML(positiveDTDJDO, true));
119         appendMessage(util.checkXML(negativeDTDJDO, false));
120         appendMessage(util.checkXML(positiveDTDORM, true));
121         appendMessage(util.checkXML(negativeDTDORM, false));
122         appendMessage(util.checkXML(positiveDTDJDOQUERY, true));
123         appendMessage(util.checkXML(negativeDTDJDOQUERY, false));
124         failOnError();
125     }
126 
127 }
128