1   /*
2    * $Id: MemoryProcessContextDAOTest.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.core.ProcessContext;
13  
14  /***
15   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:20 $
16   * @author Patric Fornasier
17   * @author Pawel Kowalski
18   */
19  public class MemoryProcessContextDAOTest extends TestCase {
20  
21      private ProcessContextDAO dao;
22  
23      private ProcessContext ctx;
24  
25      private ProcessContext ctxInit;
26      
27      private String firstString = "1-string-1";
28      private String secondString = "2-string-2";
29  
30      protected void setUp() throws Exception {
31          super.setUp();
32  
33          dao = new MemoryProcessContextDAO();
34  
35          // create process context with firstString as result
36          ctx = new ProcessContext();
37          ctx.setResult(firstString);
38  
39          // create process context with secondString as result
40          ctxInit = new ProcessContext();
41          ctxInit.setResult(secondString);
42      }
43  
44      /***
45       * Test if we can insert a <code>ProcessContext</code> and find it again.
46       */
47      public void testInsertAndFind() throws Exception {
48  
49          // store the context
50          String id = dao.insert(ctx);
51          String idInit = dao.insert(ctxInit);
52  
53          // explicitely nullify dao and context objects
54          dao = null;
55          ctx = null;
56          ctxInit = null;
57  
58          // create a new dao and load context with primary key
59          dao = new MemoryProcessContextDAO();
60          ctx = dao.find(id);
61          ctxInit = dao.find(idInit);
62  
63          // check if we retrieved the right objects
64          assertEquals(firstString, ctx.getResult());
65          assertEquals(secondString, ctxInit.getResult());
66      }
67  
68      /***
69       * Test if we can delete a <code>ProcessContext</code>.
70       */
71      public void testDelete() throws Exception {
72  
73          // store the context
74          String id = dao.insert(ctx);
75  
76          // check if the context is gone after calling the delete() method
77          assertNotNull(dao.find(id));
78          dao.delete(id);
79          assertNull(dao.find(id));
80      }
81  
82      /***
83       * Test if we can update a <code>ProcessContext</code>.
84       */
85      public void testUpdate() throws Exception {
86  
87          try {
88              dao.update(ctx);
89              fail("Calling update on a non-existing object should not work!");
90          } catch (DAOException e) {
91              // nothing to do here
92          }
93  
94          // store the context
95          String id = dao.insert(ctx);
96  
97          // modify the context and update
98          String str = "humba-rumba";
99          ctx.setResult(str);
100         dao.update(ctx);
101 
102         // check if the process context got updated
103         ctx = null;
104         ctx = dao.find(id);
105         assertEquals(str, ctx.getResult());
106     }
107 }