1   /**
2    * 
3    */
4   package org.melati.test;
5   
6   import java.util.Properties;
7   import java.util.Vector;
8   
9   import org.melati.LogicalDatabase;
10  import org.melati.poem.Database;
11  import org.melati.poem.DatabaseInitialisationPoemException;
12  import org.melati.poem.test.PoemTestCase;
13  import org.melati.util.DatabaseInitException;
14  import org.melati.util.NoSuchPropertyException;
15  
16  /**
17   * @author timp
18   * @since 15 Jan 2007
19   *
20   */
21  public class LogicalDatabaseTest extends PoemTestCase {
22  
23    /**
24     * Constructor.
25     * @param name
26     */
27    public LogicalDatabaseTest(String name) {
28      super(name);
29    }
30  
31    /**
32     * {@inheritDoc}
33     * @see junit.framework.TestCase#setUp()
34     */
35    protected void setUp() throws Exception {
36      super.setUp();
37    }
38  
39    /**
40     * {@inheritDoc}
41     * @see junit.framework.TestCase#tearDown()
42     */
43    protected void tearDown() throws Exception {
44      super.tearDown();
45    }
46  
47    /**
48     * When run in eclipse only one is know, when run through a suite 
49     * then all dbs are found.
50     * 
51     * Test method for {@link org.melati.LogicalDatabase#initialisedDatabases()}.
52     */
53    public void testInitialisedDatabases() {
54      Vector them = LogicalDatabase.initialisedDatabases();
55      assertTrue(them.size()> 0);
56    }
57  
58    /**
59     * This fails under crap4j, so this is a difference between crap4j/eclipse and maven.
60     * 
61     * Test method for {@link org.melati.LogicalDatabase#getInitialisedDatabaseNames()}.
62     */
63    public void testGetInitialisedDatabaseNames() {
64      Vector them = LogicalDatabase.getInitialisedDatabaseNames();
65      assertTrue(them.size() > 0);
66      boolean found = false;
67      for (int i = 0; i < them.size(); i++)
68        if (((String)them.get(i)).equals("melatijunit"))
69                found = true;
70      assertTrue(found);
71      
72    }
73  
74    /**
75     * Test method for {@link org.melati.LogicalDatabase#getDatabase(java.lang.String)}.
76     * @throws Exception 
77     */
78    public void testGetDatabase() throws Exception {
79      try { 
80        LogicalDatabase.getDatabase(null);
81        fail("Should have blown up");
82      } catch (NullPointerException e) {
83        e = null;
84      }
85      try { 
86        LogicalDatabase.getDatabase("bad");
87        fail("Should have blown up");
88      } catch (DatabaseInitialisationPoemException e) {
89        e = null;
90      }
91      try { 
92        LogicalDatabase.getDatabase("unknown");
93        fail("Should have blown up");
94      } catch (DatabaseInitException e) {
95        e = null;
96      }
97    }
98  
99    /**
100    * Test method for {@link org.melati.LogicalDatabase#setDatabaseDefs(java.util.Properties)}.
101    */
102   public void testPropertiesFileNotFound() throws Exception {
103    LogicalDatabase.setDatabaseDefs(null);
104    try { 
105      LogicalDatabase.getDatabase("unknown");
106      fail("Should have blown up");
107    } catch (DatabaseInitException e) {
108      //e.printStackTrace();
109      e = null;
110    }
111    LogicalDatabase.setDatabaseDefs(null);
112   }
113 
114   /**
115    * Test method for {@link org.melati.LogicalDatabase#setDatabaseDefs(java.util.Properties)}.
116    */
117   public void testSetDatabaseDefs() {
118    Properties empty = new Properties(); 
119    LogicalDatabase.setDatabaseDefs(empty);
120    try { 
121      Database ld = LogicalDatabase.getDatabase("unknown");
122      fail("Should have blown up but LD = " + ld);
123    } catch (DatabaseInitException e) {
124      assertTrue(e.subException instanceof NoSuchPropertyException);
125      e = null;
126    }
127    LogicalDatabase.setDatabaseDefs(null);
128   }
129 
130   
131   /**
132    * Test method for {@link org.melati.LogicalDatabase#getPropertiesName()}.
133    */
134   public void testGetDefaultPropertiesName() {
135     assertEquals("org.melati.LogicalDatabase.properties", LogicalDatabase.getPropertiesName());
136   }
137 
138 }