1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.dbobj;
11  
12  import com.jcorporate.expresso.core.db.DBConnection;
13  import com.jcorporate.expresso.core.db.DBConnectionPool;
14  import com.jcorporate.expresso.core.db.DBException;
15  import com.jcorporate.expresso.core.dbobj.DBObject;
16  import com.jcorporate.expresso.core.dbobj.Schema;
17  import com.jcorporate.expresso.core.utility.DBToolTests;
18  import com.jcorporate.expresso.ext.dbobj.SingleDBUserInfo;
19  import com.jcorporate.expresso.services.dbobj.DefaultUserInfo;
20  import com.jcorporate.expresso.services.test.TestSystemInitializer;
21  import com.sri.emo.EmoSchema;
22  import junit.framework.TestSuite;
23  
24  import java.util.Enumeration;
25  import java.util.TreeSet;
26  
27  
28  /***
29   * This class validates the schema by automatically creating it
30   * Base Class Tests from DBToolTests perform the actual work.
31   */
32  public class TestSchema
33          extends DBToolTests {
34      public TestSchema(String testName)
35              throws Exception {
36          super(testName);
37      }
38  
39      public static junit.framework.Test suite()
40              throws Exception {
41  
42          //Order Matters here
43          TestSuite ts = new TestSuite("Schema Verification Tests");
44          ts.addTest(new TestSchema("testCreate"));
45          ts.addTest(new TestSchema("testAddingSchemas"));
46          ts.addTest(new TestSchema("testSetupSecurity"));
47          ts.addTest(new TestSchema("testPopulateTables"));
48          ts.addTest(new TestSchema("testOtherSetups"));
49          ts.addTest(new TestSchema("testVerify"));
50          return ts;
51      }
52  
53      protected void setUp()
54              throws Exception {
55          super.setUp();
56  
57          //System must be initialized prior to instanatiating the schema instance
58          schemaList.add(new EmoSchema());
59      }
60  
61      public void testVerify() {
62          TreeSet tableNames = new TreeSet();
63          DBConnectionPool myPool = null;
64          DBConnection myConnection = null;
65  
66          try {
67              if (!isFailedOnce()) {
68                  assertTrue("schemaList.size() > 0", schemaList.size() > 0);
69  
70                  Schema thisSchema = null;
71  
72                  for (Enumeration als = schemaList.elements();
73                       als.hasMoreElements();) {
74                      thisSchema = (Schema) als.nextElement();
75                      System.out.println("Verifying Schema " +
76                              thisSchema.getClass().getName());
77  
78                      DBObject oneObject = null;
79  
80                      for (Enumeration dbo = thisSchema.getMembers();
81                           dbo.hasMoreElements();) {
82                          oneObject = (DBObject) dbo.nextElement();
83                          oneObject.setDataContext(TestSystemInitializer.getTestContext());
84  
85                          //Check for unique table name
86                          if (tableNames.contains(oneObject.getJDBCMetaData().getTargetTable())) {
87  
88                              //
89                              //We have a special case for SingleDBUserInfo since it
90                              //is a known duplicate table.  Under Normal Circumstances
91                              //this should not be the case.
92                              //
93                              String objName = oneObject.getClass().getName();
94                              if (objName.equals(DefaultUserInfo.class.getName())
95                                      || objName.equals(SingleDBUserInfo.class.getName())
96                                      || objName.equals(com.sri.common.util.PermGroup.class.getName())) {
97                              } else {
98                                  fail("Table name: " +
99                                          oneObject.getJDBCMetaData().getTargetTable() +
100                                         " already exists due to another DBObject.  Offending DBObject" +
101                                         oneObject.getClass().getName());
102                             }
103                         } else {
104                             tableNames.add(oneObject.getJDBCMetaData().getTargetTable());
105                         }
106                         //oneObject.setConnection(myConnection);
107                         try {
108                             oneObject.verify();
109                         } catch (DBException de) {
110                             myPool.release(myConnection);
111                             System.out.println(de.getMessage());
112                             de.printStackTrace();
113                             fail("Error in object " + oneObject.getJDBCMetaData().getName() +
114                                     ":" + de.getMessage());
115                         }
116                     }
117                     /* for each database object */
118 
119                 }
120             } else { /* for each schema in the list */
121                 fail();
122             }
123         } catch (Exception e) {
124             System.out.println(e.getMessage());
125             e.printStackTrace();
126             setFailedOnce(true);
127 
128             if (myPool != null && myConnection != null) {
129                 myPool.release(myConnection);
130             }
131 
132             fail(e.getMessage());
133         }
134 
135     }
136 
137 
138     public static void main(String[] args)
139             throws Exception {
140 
141         //Set the system properties we need
142         junit.textui.TestRunner.run(suite());
143     }
144 }