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