1   /*
2    * $Id: DAOFactoryTest.java,v 1.1 2004/12/15 14:18:20 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.dao;
10  
11  import junit.framework.TestCase;
12  import bexee.util.Constants;
13  
14  /***
15   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:20 $
16   * @author Patric Fornasier
17   */
18  public class DAOFactoryTest extends TestCase {
19  
20      private static final String FACTORY_IMPL = "bexee.dao.DAOFactoryTest$MockDAOFactory";
21  
22      protected void tearDown() throws Exception {
23          super.tearDown();
24  
25          // make sure to remove that system property again
26          System.getProperties().remove(Constants.OPT_DAO_FACTORY);
27      }
28  
29      /***
30       * Test if the factory returns the correct default implementation if no
31       * system property is set.
32       */
33      public void testGetInstance() {
34          DAOFactory factory = DAOFactory.getInstance();
35          assertTrue(factory instanceof MemoryDAOFactory);
36      }
37  
38      /***
39       * Test whether the factory returns the right implementation based on a
40       * system property.
41       */
42      public void testGetInstanceBySystemProperty() {
43  
44          // set a different implementation
45          System.setProperty(Constants.OPT_DAO_FACTORY, FACTORY_IMPL);
46  
47          DAOFactory factory = DAOFactory.getInstance();
48          assertTrue(factory instanceof DAOFactoryTest.MockDAOFactory);
49      }
50      
51      /***
52       * Test whether the factory returns the default implementation if an invalid
53       * class name is provided.
54       */
55      public void testWrongClassName()
56      {
57          // set an invalid class name
58          System.setProperty(Constants.OPT_DAO_FACTORY, "nonExistingClass");
59          
60          // check if the default implementation is created
61          DAOFactory factory = DAOFactory.getInstance();
62          assertTrue(factory instanceof MemoryDAOFactory);
63      }
64  
65      /***
66       * Mock class extending the <code>DAOFactory</code> for testing.
67       * 
68       * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:20 $
69       * @author Patric Fornasier
70       */
71      public static class MockDAOFactory extends DAOFactory {
72  
73          /***
74           * Doesn't do anything
75           */
76          public BPELProcessDAO createBPELProcessDAO() {
77              return null;
78          }
79  
80          /***
81           * Doesn't do anything
82           */
83          public ProcessContextDAO createProcessContextDAO() {
84              return null;
85          }
86  
87      }
88  }