1   /* ===================================================================
2    * Copyright 2002-05 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.completion.model;
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.ByteArrayOutputStream;
14  import java.io.IOException;
15  import java.io.ObjectInputStream;
16  import java.io.ObjectOutputStream;
17  
18  import junit.framework.TestCase;
19  
20  import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
21  import com.jcorporate.expresso.core.security.SuperUser;
22  import com.jcorporate.expresso.services.test.TestSystemInitializer;
23  import com.sri.emo.test.DatabaseTestFixture;
24  import com.sri.emo.test.EmoTestSuite;
25  
26  /***
27   *
28   * @author Michael Rimov
29   * @version 1.0
30   */
31  public class TestCompletionBean extends TestCase {
32      DatabaseTestFixture testFixture;
33  
34      public TestCompletionBean() throws Exception {
35          super();
36          try {
37              TestSystemInitializer.setUp();
38              new MutableRequestRegistry(TestSystemInitializer.getTestContext(), SuperUser.INSTANCE);
39          } catch (Exception ex) {
40              throw new RuntimeException("Error setting things up",ex);
41          }
42      }
43  
44      protected void setUp() throws Exception {
45          super.setUp();
46          testFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
47              EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
48          testFixture.setUp();
49      }
50  
51      protected void tearDown() throws Exception {
52          testFixture.tearDown();
53          testFixture = null;
54      }
55  
56  
57      public void testEmptyCompletionBeanIsSerializable() {
58          runThroughSerializer(new CompletionBean());
59      }
60  
61      /***
62       * runThroughSerializer
63       *
64       * @param completionBean CompletionBean
65       */
66      private void runThroughSerializer(CompletionBean completionBean) {
67          try {
68              ByteArrayOutputStream bos = new ByteArrayOutputStream();
69              ObjectOutputStream oos = new ObjectOutputStream(bos);
70              oos.writeObject(completionBean);
71              ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
72              ObjectInputStream ois = new ObjectInputStream(bis);
73              CompletionBean bean = (CompletionBean) ois.readObject();
74              assertNotNull(bean);
75          } catch (ClassNotFoundException ex) {
76              ex.printStackTrace();
77              fail("Class not found exception: " + ex.getMessage());
78          } catch (IOException ex) {
79              ex.printStackTrace();            
80              fail("Exception " + ex.toString() + " writing/reading completion bean: " + ex.getMessage());
81          }
82  
83      }
84  
85      public void testEqualsOnEmptyBeans() {
86          CompletionBean bean1 = new CompletionBean();
87          CompletionBean bean2 = new CompletionBean();
88          assertTrue(bean1.equals(bean2));
89      }
90  
91      public void testHashCodeOnEmptyBeans() {
92          CompletionBean bean1 = new CompletionBean();
93          CompletionBean bean2 = new CompletionBean();
94          assertEquals(bean1.hashCode(), bean1.hashCode());
95          assertEquals(bean1.hashCode(), bean2.hashCode());
96      }
97  
98      public void testLoadNodeId() throws Exception {
99          //Loads the node
100         CompletionWizardTestBean testBean = new CompletionWizardTestBean();
101         testBean.setUp();
102 
103         assertEquals(CompletionWizardTestBean.TITLE, testBean.getConstructedBean().getWizardTitle());
104         assertEquals(CompletionWizardTestBean.SUMMARY, testBean.getConstructedBean().getSummary());
105 
106         //Wizard id at this point should be zero since it has never been added.
107         assertNull(testBean.getConstructedBean().getWizardId());
108         testBean.tearDown();
109     }
110 
111     public void testLoadedIdShouldStillBeSerializable() throws Exception {
112         CompletionWizardTestBean testBean = new CompletionWizardTestBean();
113         testBean.setUp();
114         runThroughSerializer(testBean.getConstructedBean());
115         testBean.tearDown();
116     }
117 
118     public void testEqualsOnPopulatedBeans() throws Exception {
119         CompletionWizardTestBean testBean1 = new CompletionWizardTestBean();
120         testBean1.setUp();
121         CompletionWizardTestBean testBean2 = new CompletionWizardTestBean();
122         testBean2.setUp();
123         assertEquals(testBean1.getConstructedBean(), testBean1.getConstructedBean());
124         assertEquals(testBean1.getConstructedBean(), testBean2.getConstructedBean());
125     }
126 
127     public void testHashCodeOnPopulatedBeans() throws Exception {
128         CompletionWizardTestBean testBean1 = new CompletionWizardTestBean();
129         testBean1.setUp();
130         CompletionWizardTestBean testBean2 = new CompletionWizardTestBean();
131         testBean2.setUp();
132         assertEquals(testBean1.getConstructedBean().hashCode(), testBean1.getConstructedBean().hashCode());
133         assertEquals(testBean1.getConstructedBean().hashCode(), testBean2.getConstructedBean().hashCode());
134     }
135 
136 }