1   package com.sri.emo.wizard.completion;
2   
3   import com.jcorporate.expresso.services.test.TestSystemInitializer;
4   import com.sri.emo.test.DatabaseTestFixture;
5   import com.sri.emo.test.EmoTestSuite;
6   import com.sri.emo.wizard.WizardException;
7   import junit.framework.TestCase;
8   import com.sri.emo.wizard.completion.persistence.ExpressoCompletionRepository;
9   import com.sri.emo.wizard.completion.persistence.CompletionDBObjConverterImpl;
10  import com.sri.emo.wizard.Wizard;
11  import java.io.*;
12  
13  /***
14   * @author Michael Rimov
15   * @version 1.0
16   */
17  public class TestCompletionMementoConverter extends TestCase {
18  //    private CompletionMementoConverter completionMementoConverter = null;
19      private DatabaseTestFixture databaseTestFixture;
20  
21      private Wizard oneWizard;
22  
23      private CompletionRepository repository;
24  
25      protected void setUp() throws Exception {
26          super.setUp();
27          databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
28              EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
29          databaseTestFixture.setUp();
30  
31          repository = new ExpressoCompletionRepository(new CompletionDBObjConverterImpl());
32          EmoCompletionFactory completionFactory = new EmoCompletionFactory(repository);
33  
34          completionFactory.setWizardId(15); //todo extract magic number
35          oneWizard = completionFactory.buildWizard();
36      }
37  
38      protected void tearDown() throws Exception {
39          databaseTestFixture.tearDown();
40          databaseTestFixture = null;
41          oneWizard = null;
42          repository = null;
43      }
44  
45      public void testToMemento() throws WizardException {
46          CompletionMementoConverter converter = new CompletionMementoConverter(repository);
47          Serializable memento = converter.toMemento(oneWizard);
48          assertNotNull(memento);
49          try {
50              EmoCompletionWizard completionWizard = (EmoCompletionWizard) memento;
51              assertTrue(completionWizard.getCompletionBean() == null);
52          } catch (ClassCastException ex) {
53              fail("Memento was not internally a completion wizard type");
54          }
55      }
56  
57  
58      public void testFromMemento() throws WizardException {
59          CompletionMementoConverter converter = new CompletionMementoConverter(repository);
60          Serializable memento = converter.toMemento(oneWizard);
61  
62          Wizard testWizard = converter.fromMemento(memento);
63          assertNotNull(testWizard);
64          assertEquals(testWizard,oneWizard);
65          assertNotNull(((EmoCompletionWizard)testWizard).getCompletionBean());
66      }
67  
68      public void testMementoIsSerializable() throws Exception {
69          CompletionMementoConverter converter = new CompletionMementoConverter(repository);
70          Serializable memento = converter.toMemento(oneWizard);
71  
72          ByteArrayOutputStream bos = new ByteArrayOutputStream();
73          ObjectOutputStream oos = new ObjectOutputStream(bos);
74          oos.writeObject(memento);
75          oos.flush();
76          oos.close();
77  
78          ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
79          ObjectInputStream ois = new ObjectInputStream(bis);
80          Serializable result = (Serializable)ois.readObject();
81          assertEquals(memento, result);
82      }
83  
84  }