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