1 package com.sri.emo.wizard.selection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import com.jcorporate.expresso.core.db.DBException;
21 import com.jcorporate.expresso.core.dbobj.SchemaFactory;
22 import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
23 import com.jcorporate.expresso.core.security.User;
24 import com.jcorporate.expresso.services.test.TestSystemInitializer;
25 import com.sri.emo.EmoSchema;
26 import com.sri.emo.wizard.Wizard;
27 import com.sri.emo.wizard.WizardException;
28 import com.sri.emo.wizard.WizardFactory;
29 import com.sri.emo.wizard.WizardRepository;
30 import org.jmock.Mock;
31 import org.jmock.MockObjectTestCase;
32
33 public class TestSelectionWizardRepository extends MockObjectTestCase {
34 private WizardRepository wizardRepository = null;
35 private Mock wizardFactoryMock;
36 private Mock wizardMock;
37 private WizardFactory wizardFactory;
38 private Wizard wiz;
39
40 public TestSelectionWizardRepository(String name) {
41 super(name);
42 }
43
44 protected void setUp() throws Exception {
45 super.setUp();
46 try {
47 User u = new User();
48 u.setDataContext(TestSystemInitializer.getTestContext());
49 u.setUid(User.getAdminId(TestSystemInitializer.getTestContext()));
50 u.retrieve();
51 new MutableRequestRegistry(TestSystemInitializer.getTestContext(), u);
52 } catch (DBException ex) {
53 ex.printStackTrace();
54 throw new java.lang.RuntimeException(ex);
55 }
56
57 EmoSchema oneSchema =
58 (EmoSchema) SchemaFactory.getInstance().getSchema(com.sri.emo.EmoSchema.class
59 .getName());
60 wizardRepository =
61 oneSchema.getWizardRepository(TestSystemInitializer.getTestContext());
62 }
63
64 protected void tearDown() throws Exception {
65 wizardRepository = null;
66 wizardFactoryMock = null;
67 wizardMock = null;
68 wiz = null;
69 super.tearDown();
70 }
71
72 /***
73 * Builds a Mock Object Wizard Factory to test the Wizard Repository
74 *
75 * @return WizardFactory instance.
76 */
77 protected WizardFactory buildMockWizardFactory() {
78 wizardMock = new Mock(Wizard.class);
79 wizardMock.expects(never());
80 wiz = (Wizard) wizardMock.proxy();
81
82 wizardFactoryMock = new Mock(WizardFactory.class);
83 wizardFactoryMock.expects(once()).method("buildWizard").withNoArguments().will(returnValue(wiz));
84 return (WizardFactory) wizardFactoryMock.proxy();
85 }
86
87 /***
88 * @throws WizardException
89 */
90 public void testAddFind() throws WizardException {
91 wizardFactory = buildMockWizardFactory();
92 wizardRepository.addFactory("TestWizard", wizardFactory);
93 Wizard result = wizardRepository.find("TestWizard");
94 assertNotNull(result);
95 assertTrue(result == wiz);
96 wizardFactoryMock.verify();
97 wizardMock.verify();
98 wizardRepository.removeWizard("TestWizard");
99 }
100
101 /***
102 * Checks to make sure we can serialize the repository and rebuild it
103 * properly.
104 */
105 public void testRepositorySerialization() {
106 try {
107 ByteArrayOutputStream bos = new ByteArrayOutputStream();
108 ObjectOutputStream oos = new ObjectOutputStream(bos);
109 oos.writeObject(wizardRepository);
110
111 byte[] array = bos.toByteArray();
112
113
114 assertTrue(array.length < 1024);
115 System.out.println("Repository serialized size" + array.length);
116 ByteArrayInputStream bis = new ByteArrayInputStream(array);
117 ObjectInputStream ois = new ObjectInputStream(bis);
118 WizardRepository wizrep = (WizardRepository) ois.readObject();
119 assertEquals(wizrep, wizardRepository);
120 } catch (IOException ex) {
121 ex.printStackTrace();
122 fail("I/O Error Unable to verify successful serialization for wizard repository:" + wizardRepository.getClass().getName());
123 } catch (ClassNotFoundException ex) {
124 ex.printStackTrace();
125 fail("Class Cast Exception: Unable to verify successful serialization");
126 } catch (ClassCastException ex) {
127 ex.printStackTrace();
128 fail("Deserialized the wrong type!");
129 }
130
131 }
132
133
134 }