1   package com.sri.emo;
2   
3   import java.util.Enumeration;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.io.InputStream;
7   
8   import com.jcorporate.expresso.core.controller.Block;
9   import com.jcorporate.expresso.core.controller.ExpressoResponse;
10  import com.jcorporate.expresso.core.dataobjects.DataObject;
11  import com.jcorporate.expresso.core.dataobjects.DataObjectFactory;
12  import com.jcorporate.expresso.core.dbobj.DBObject;
13  import com.jcorporate.expresso.core.dbobj.SchemaFactory;
14  import com.jcorporate.expresso.core.registry.RequestRegistry;
15  import com.jcorporate.expresso.services.controller.DBMaint;
16  import com.jcorporate.expresso.services.test.ControllerTestFixture;
17  import com.jcorporate.expresso.services.test.TestSystemInitializer;
18  import com.sri.emo.test.DatabaseTestFixture;
19  import com.sri.emo.test.EmoTestSuite;
20  import junit.framework.TestCase;
21  
22  /***
23   * Test the schema, in particular, test that DBMaint.LIST works for
24   * all the objects.
25   *
26   * @author Michael Rimov
27   *
28   */
29  public class TestEmoSchema extends TestCase {
30      private EmoSchema emoSchema = null;
31      private DatabaseTestFixture databaseTestFixture;
32      private ControllerTestFixture testFixture = null;
33  
34      protected void setUp() throws Exception {
35          super.setUp();
36          emoSchema = (EmoSchema) SchemaFactory.getInstance()
37                  .getSchema(EmoSchema.class.getName());
38          InputStream stream = EmoTestSuite.class.getResourceAsStream("WizardTestData.xml");
39          if ( stream == null ) {
40              throw new Exception("Cannot find file 'WizardTestData.xml' of test data adjacent to class: " + EmoTestSuite.class.getName());
41          }
42          databaseTestFixture = new DatabaseTestFixture(
43                  TestSystemInitializer.getTestContext(), stream);
44          databaseTestFixture.setUp();
45  
46          testFixture = new ControllerTestFixture();
47          testFixture.setUp();
48  
49      }
50  
51      protected void tearDown() throws Exception {
52          emoSchema = null;
53          databaseTestFixture.tearDown();
54  
55          databaseTestFixture = null;
56          testFixture.tearDown();
57          testFixture = null;
58          super.tearDown();
59      }
60  
61      public void testGetMessageBundlePath() {
62          String expectedReturn = "com/sri/emo";
63          String actualReturn = emoSchema.getMessageBundlePath();
64          assertEquals("return value", expectedReturn, actualReturn);
65  
66          /***
67           * @todo Have properties and load 'em.
68           */
69      }
70  
71      public void testDBMaintList() throws Exception {
72          for (Enumeration e = emoSchema.getMembers(); e.hasMoreElements();) {
73              DBObject oneDBObject = (DBObject) e.nextElement();
74              assertTrue(oneDBObject != null);
75              final String dbobjClassName = oneDBObject.getClass().getName();
76              Class c = Thread.currentThread()
77                      .getContextClassLoader().loadClass(dbobjClassName);
78              DataObject oneObj = DataObjectFactory.createDataObject(c, RequestRegistry.getDataContext()
79                      , RequestRegistry.getUser());
80              Map params = new HashMap();
81              params.put("dbobj", dbobjClassName);
82              ExpressoResponse response = testFixture.invokeFacade(DBMaint.class, params, DBMaint.LIST);
83  
84              assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
85  
86              int numObjects = oneObj.count();
87  
88              //The recordlist should have one subblock per row, limited by
89              //maximum record parameters.
90              Block rows = response.getBlock("recordList");
91              assertTrue(rows != null);
92              final int MAX_ROWS = 50;
93              if (numObjects > MAX_ROWS) {
94                  assertEquals(MAX_ROWS, rows.getNested().size());
95              } else {
96                  assertEquals(numObjects, rows.getNested().size());
97              }
98          }
99  
100     }
101 
102 
103 }