1   package com.sri.emo.test;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   import com.jcorporate.expresso.core.ExpressoSchema;
7   import com.jcorporate.expresso.core.db.DBException;
8   import com.jcorporate.expresso.core.dbobj.Schema;
9   import com.jcorporate.expresso.core.dbobj.SchemaFactory;
10  import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
11  import com.jcorporate.expresso.core.registry.RequestRegistry;
12  import com.jcorporate.expresso.core.security.SuperUser;
13  import com.jcorporate.expresso.kernel.ConsoleInstallLog;
14  import com.jcorporate.expresso.services.test.SchemaCreator;
15  import com.jcorporate.expresso.services.test.TestSystemInitializer;
16  import com.sri.emo.EmoSchema;
17  import com.sri.emo.dbobj.NodeType;
18  import org.jmock.MockObjectTestCase;
19  
20  /***
21   * Base Class Test Case that provides the common database setup-teardown needs
22   * of a wizard test case.
23   * @author Michael Rimov
24   * @version 1.0
25   */
26  public class EmoWizardDatabaseTestCase extends MockObjectTestCase {
27  
28      boolean tablesCreated = false;
29  
30      public EmoWizardDatabaseTestCase() {
31          super();
32  
33      }
34  
35      public EmoWizardDatabaseTestCase(String name) {
36          super(name);
37  
38      }
39  
40  
41      private void setupExpressoUnderpinnings() {
42          if (!tablesCreated) {
43              try {
44                  TestSystemInitializer.setUp();
45                  new MutableRequestRegistry(TestSystemInitializer.getTestContext(), SuperUser.INSTANCE);
46  
47                  NodeType testTable = new NodeType();
48                  try {
49                      testTable.count();
50                  } catch (DBException ex1) {
51                      System.out.println("Creating System tables for testing....");
52  
53                      List schemasToCreate = new ArrayList();
54                      schemasToCreate.add(ExpressoSchema.class);
55                      schemasToCreate.add(EmoSchema.class);
56                      createDatabaseSetup(schemasToCreate);
57                  }
58  
59              } catch (Exception ex) {
60                  ex.printStackTrace();
61                  throw new RuntimeException("error see console for details.");
62              } finally {
63                  //ONCE!
64                  tablesCreated = true;
65              }
66          }
67      }
68  
69      protected void setUp() throws Exception {
70          super.setUp();
71          setupExpressoUnderpinnings();
72      }
73  
74      protected void createDatabaseSetup(final List schemas) throws Exception {
75          for (Iterator i = schemas.iterator(); i.hasNext();) {
76              Class oneSchemaClass = (Class)i.next();
77              Schema oneSchema = SchemaFactory.getInstance().getSchema(oneSchemaClass.getName());
78              oneSchema.setDataContext(RequestRegistry.getDataContext());
79              SchemaCreator.ensureSchemaExists(oneSchema, new ConsoleInstallLog());
80          }
81  
82      }
83  
84  }
85