View Javadoc
1   package org.melati.poem.test;
2   
3   import junit.framework.TestCase;
4   
5   import org.melati.poem.Database;
6   import org.melati.poem.DatabaseInitialisationPoemException;
7   import org.melati.poem.PoemDatabaseFactory;
8   
9   
10  /**
11   * @author timp
12   * @since 2 Feb 2007
13   *
14   */
15  public class PoemDatabaseFactoryTest extends TestCase {
16  
17    /** Default db name */
18    private String databaseName = "melatijunit";  // change to poemtest
19  
20    public PoemDatabaseFactoryTest(String name) {
21      super(name);
22    }
23  
24    protected void setUp() throws Exception {
25      super.setUp();
26    }
27  
28    protected void tearDown() throws Exception {
29      super.tearDown();
30    }
31  
32    /**
33     * Test method for {@link org.melati.poem.PoemDatabaseFactory#getDatabase(String)}.
34     * @throws Exception 
35     */
36    public void testGetDatabase() throws Exception {
37      try { 
38        PoemDatabaseFactory.getDatabase(null);
39        fail("Should have blown up");
40      } catch (NullPointerException e) {
41        e = null;
42      }
43      assertNull(PoemDatabaseFactory.getDatabase("unknown"));
44    }
45  
46    /**
47     * Test method for {@link org.melati.poem.PoemDatabaseFactory
48     * #getDatabase(String, String, String, String, String, String, boolean, boolean, boolean, int)}.
49     * @throws Exception 
50     */
51    public void testGetDatabaseStringStringStringStringStringBooleanBooleanBooleanInt() throws Exception {
52      try { 
53        PoemDatabaseFactory.getDatabase(null);
54        fail("Should have blown up");
55      } catch (NullPointerException e) {
56        e = null;
57      }
58      
59      assertNull(PoemDatabaseFactory.getDatabase("unknown"));
60      try { 
61        PoemDatabaseFactory.getDatabase("badclassname",
62              "jdbc:hsqldb:mem:" + getDatabaseName(),
63              "sa",
64              "","org.melati.poem.PoemDatabaseNOT",
65              "org.melati.poem.dbms.Hsqldb",false,false,false,8);
66      } catch (DatabaseInitialisationPoemException e) { 
67        assertTrue(e.innermostException() instanceof ClassNotFoundException);
68      }
69      try { 
70        PoemDatabaseFactory.getDatabase("badclassname",
71              "jdbc:hsqldb:mem:" + getDatabaseName(),
72              "sa",
73              "","java.lang.Exception",
74              "org.melati.poem.dbms.Hsqldb",false,false,false,8);
75      } catch (DatabaseInitialisationPoemException e) { 
76        assertTrue(e.innermostException() instanceof ClassCastException);
77      }
78    }
79  
80  
81    
82    /**
83     * Test method for {@link org.melati.poem.PoemDatabaseFactory#initialisedDatabases()}.
84     */
85    public void testInitialisedDatabases() {
86      int initialSize = PoemDatabaseFactory.initialisedDatabases().size();
87      getPoemDatabase();
88      getEverythingDatabase();
89      if (initialSize == 0)
90        assertEquals(initialSize + 2, PoemDatabaseFactory.initialisedDatabases().size());
91      else 
92        assertEquals(2, PoemDatabaseFactory.initialisedDatabases().size());
93          
94    }
95  
96    /**
97     * Test method for {@link org.melati.poem.PoemDatabaseFactory#getInitialisedDatabaseNames()}.
98     */
99    public void testGetInitialisedDatabaseNames() {
100     assertTrue(PoemDatabaseFactory.
101         getInitialisedDatabaseNames().
102         toString().
103         indexOf(EverythingTestCase.databaseName) != -1);
104     
105   }
106 
107   /**
108    * Test method for {@link org.melati.poem.PoemDatabaseFactory#removeDatabase(String)}.
109    */
110   public void testRemoveDatabaseString() {
111     
112   }
113   
114   /**
115    * Test method for {@link org.melati.poem.PoemDatabaseFactory#getPoemShutdownThread()}.
116    */
117   public void testGetPoemShutdownThread() {
118     Thread t = PoemDatabaseFactory.getPoemShutdownThread();
119     assertEquals("PoemShutdownThread", t.getName());
120   }
121   /**
122    * @return the minial poem db
123    */
124   public Database getPoemDatabase() { 
125     return PoemDatabaseFactory.getDatabase(getDatabaseName(),
126             "jdbc:hsqldb:mem:" + getDatabaseName(),
127             "sa",
128             "","org.melati.poem.PoemDatabase",
129             "org.melati.poem.dbms.Hsqldb",true,true,false,8);
130   }
131   /**
132    * @return the everything db
133    */
134   public static Database getEverythingDatabase() { 
135     return PoemDatabaseFactory.getDatabase(EverythingTestCase.databaseName,
136             "jdbc:hsqldb:mem:" + EverythingTestCase.databaseName,
137             "sa",
138             "","org.melati.poem.test.EverythingDatabase",
139             "org.melati.poem.dbms.Hsqldb",false,false,false,8);
140   }
141 
142   /**
143    * @return the db name
144    */
145   public String getDatabaseName() {
146     return databaseName;
147   }
148 
149   /**
150    * @param databaseName the db name to set
151    */
152   public void setDatabaseName(String databaseName) {
153     this.databaseName = databaseName;
154   }
155 
156 }