View Javadoc
1   package org.melati.poem.test;
2   
3   
4   import org.melati.poem.PoemDatabaseFactory;
5   import org.melati.poem.Table;
6   import org.melati.poem.TableFactory;
7   import org.melati.poem.test.pojo.ClassWithNoIdAndPublicMembers;
8   import org.melati.poem.test.pojo.ClassWithStringId;
9   
10  /**
11   * @author timp
12   * @since 12 Jun 2007
13   *
14   */
15  public class TableFactoryTest extends PoemTestCase {
16  
17    public TableFactoryTest(String name) {
18      super(name);
19    }
20  
21    protected void setUp() throws Exception {
22      super.setUp();
23    }
24  
25    protected void databaseUnchanged() {
26      assertEquals("Setting changed", 0, getDb().getSettingTable().count());
27      assertEquals("Group changed", 1, getDb().getGroupTable().count());
28      assertEquals("GroupMembership changed", 1, getDb().getGroupMembershipTable().count());
29      assertEquals("Capability changed", 5, getDb().getCapabilityTable().count());
30      assertEquals("GroupCapability changed", 1, getDb().getGroupCapabilityTable().count());
31      assertEquals("TableCategory changed", 3, getDb().getTableCategoryTable().count());
32      assertEquals("User changed", 2, getDb().getUserTable().count());
33      //ColumnInfo newOne = null; 
34      //try{ 
35      //  newOne = (ColumnInfo)getDb().getColumnInfoTable().getObject(69);
36      //} catch (Exception e) {}
37      //if (newOne != null) { 
38      //  System.err.println(newOne.getName() + " " + newOne.getTableinfo().getName());
39      //}
40      //assertEquals("ColumnInfo changed", 69, getDb().getColumnInfoTable().count());
41      //assertEquals("TableInfo changed", 9, getDb().getTableInfoTable().count());
42      //checkTablesAndColumns(9,69);
43    }
44  
45    /** 
46     * {@inheritDoc}
47     * @see org.melati.poem.test.PoemTestCase#tearDown()
48     */
49    protected void tearDown() throws Exception {
50      super.tearDown();
51      getDb().disconnect();
52      PoemDatabaseFactory.removeDatabase(getDatabaseName());
53    }
54  
55    /**
56     * Test method for {@link org.melati.poem.TableFactory#fromInstance(org.melati.poem.Database, java.lang.Object)}.
57     */
58    public void testFromKnownInstance() {
59      assertEquals(getDb().getUserTable(), TableFactory.fromInstance(getDb(), getDb().getUserTable().administratorUser()));
60    }
61    /**
62     * Test method for {@link org.melati.poem.TableFactory#fromInstance(org.melati.poem.Database, java.lang.Object)}.
63     */
64    public void testFromUnKnownInstance() {
65      Table<?>t = TableFactory.fromInstance(getDb(), new ClassWithNoIdAndPublicMembers());
66      assertEquals("ClassWithNoIdAndPublicMembers", t.getName());
67      // FIXME Delete tableinfo and columnInfo
68      //getDb().delete(t);
69      //getDb().dump();
70    }
71    /**
72     * Test exception thrown.
73     */
74    public void testFromObjectWithStringIdField() { 
75      Table<?>table =  TableFactory.fromInstance(getDb(), new ClassWithStringId());
76      assertEquals("ClassWithStringId", table.getName());
77    }
78    /**
79     * Test exception thrown.
80     */
81    public void testFromNonPublicObject() { 
82      try { 
83        TableFactory.fromInstance(getDb(), new NonPublicClass());
84        fail("Should have blown up.");
85      } catch (IllegalArgumentException e) { 
86        e = null;
87      }
88    }
89    /**
90     * Test method for {@link org.melati.poem.TableFactory#fromClass(org.melati.poem.Database, java.lang.Class)}.
91     */
92    public void testFromClassBadInput() {
93      try { 
94        TableFactory.fromClass(getDb(), int.class);
95        fail("Should have blown up");
96      } catch (IllegalArgumentException e) { 
97        e = null;
98      }
99      try { 
100       TableFactory.fromInstance(getDb(), new String[] {});
101       fail("Should have blown up");
102     } catch (IllegalArgumentException e) { 
103       e = null;
104     }
105   }
106   /**
107    * @author timp
108    * @since 14 Jun 2007
109    *
110    */
111   class NonPublicClass {
112     NonPublicClass() {}
113   }
114 }