KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > handlers > SerializedComponentHandlerTest


1 /*
2  * Copyright (c) 2004, Rob Gordon.
3  */

4 package org.oddjob.arooa.handlers;
5
6 import java.io.File JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.apache.log4j.Logger;
11 import org.oddjob.arooa.ArooaConstants;
12 import org.oddjob.arooa.ArooaContext;
13 import org.oddjob.arooa.ArooaRuntime;
14 import org.oddjob.arooa.SimpleObjectFactory;
15 import org.oddjob.arooa.registry.ComponentRegistry;
16 import org.oddjob.persist.OddjobPersistException;
17 import org.oddjob.persist.SerializePersister;
18 import org.xml.sax.SAXParseException JavaDoc;
19 import org.xml.sax.helpers.AttributesImpl JavaDoc;
20
21 /**
22  *
23  * @author Rob Gordon.
24  */

25 public class SerializedComponentHandlerTest extends TestCase {
26
27     File JavaDoc dir = new File JavaDoc("test/persist");
28     String JavaDoc id = "serialized-handler-test";
29     Logger logger = Logger.getLogger("test");
30     DummyComponent foo;
31         
32     /**
33      * Persist a component and then test that
34      * the handler loads it again by calling the
35      * handler methods with appropriate values.
36      */

37     public void testGoodLoad()
38     throws OddjobPersistException,
39             SAXParseException JavaDoc {
40         // save something to load.
41
DummyComponent test = new DummyComponent();
42         test.setFoo("Hello World");
43         SerializePersister persister = new SerializePersister();
44         persister.setDir(dir);
45         persister.persist(id, test);
46         // create a handler
47
DefaultComponentHandler def = new DefaultComponentHandler();
48         SerializedComponentHandler handler
49             = new SerializedComponentHandler(def, persister);
50         ArooaContext context = new ArooaContext();
51         context.set(ArooaConstants.COMPONENT_FACTORY, new SimpleObjectFactory());
52         context.set(ArooaConstants.COMPONENT_REGISTRY, new ComponentRegistry());
53         context.set(ArooaConstants.CURRENTLY_CONFIGURING, new ArooaRuntime(this, "test"));
54         context.set(ArooaConstants.ELEMENT_NAME, "foo");
55         context = new ArooaContext(context);
56         
57         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
58         atts.addAttribute("", "id", "id", "java.lang.String", id);
59         // start
60
handler.onStartElement("", "foo", "foo", atts, context);
61         // and end
62
handler.onEndElement("", "foo", context);
63
64         assertTrue("Component not equal to that saved", test.getFoo().equals(foo.getFoo()));
65     }
66     
67     public void addComponentFoo(DummyComponent foo) {
68         this.foo = foo;
69     }
70 }
71
Popular Tags