KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > Helper


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

4 package org.oddjob;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.ObjectInput JavaDoc;
10 import java.io.ObjectInputStream JavaDoc;
11 import java.io.ObjectOutput JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13
14 import org.oddjob.arooa.ArooaConstants;
15 import org.oddjob.arooa.ArooaContext;
16 import org.oddjob.arooa.ArooaFactory;
17 import org.oddjob.arooa.ArooaRuntime;
18 import org.oddjob.arooa.PropertyProxyResolver;
19 import org.oddjob.arooa.SimpleObjectFactory;
20 import org.oddjob.arooa.handlers.MainHandler;
21 import org.oddjob.arooa.handlers.MappedPropertyHandler;
22 import org.oddjob.arooa.handlers.NestedPropertyHandler;
23 import org.oddjob.arooa.handlers.ValueHandler;
24 import org.oddjob.arooa.xml.XMLDefinitionHelper;
25 import org.oddjob.framework.BeanUtilsProvider;
26 import org.oddjob.state.JobState;
27 import org.oddjob.state.JobStateEvent;
28 import org.oddjob.state.JobStateListener;
29 import org.xml.sax.InputSource JavaDoc;
30
31 /**
32  * Useful utilities for tests.
33  *
34  * @author Rob Gordon.
35  */

36 public class Helper {
37
38     public static JobState getJobState(Object JavaDoc o) {
39         Stateful stateful = (Stateful) o;
40         JSL l = new JSL();
41         stateful.addJobStateListener(l);
42         stateful.removeJobStateListener(l);
43         return l.js;
44     }
45     
46     static class JSL implements JobStateListener {
47         JobState js;
48         public void jobStateChange(JobStateEvent event) {
49             js = event.getJobState();
50         }
51     };
52     
53     public static Object JavaDoc copy(Object JavaDoc object) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
54
55         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
56         ObjectOutput JavaDoc oo = new ObjectOutputStream JavaDoc(out);
57         oo.writeObject(object);
58         oo.close();
59             
60         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
61         ObjectInput JavaDoc oi = new ObjectInputStream JavaDoc(in);
62         Object JavaDoc o = oi.readObject();
63         oi.close();
64         return o;
65     }
66     
67
68     public static class Surrogate {
69         Object JavaDoc value;
70         public void addConfiguredWhatever(Object JavaDoc value) {
71             this.value = value;
72         }
73     }
74     
75     public static Object JavaDoc createTypeFromXml(String JavaDoc xml) {
76         
77         SimpleObjectFactory valueFactory = new SimpleObjectFactory();
78         valueFactory.addResource(Oddjob.TYPE_PROPERTIES);
79
80         PropertyProxyResolver propertyProxyResolver = new PropertyProxyResolver();
81         propertyProxyResolver.addResource(Oddjob.PROXY_PROPERTIES);
82         
83         ArooaContext context = new ArooaContext();
84         context.set(ArooaConstants.VALUE_FACTORY, valueFactory);
85         context.set(ArooaConstants.PROPERTY_PROXY_RESOLVER, propertyProxyResolver);
86         context.set(ArooaConstants.PROPERTY_HANDLER, new NestedPropertyHandler());
87         context.set(ArooaConstants.MAPPED_HANDLER, new MappedPropertyHandler());
88         context.set(ArooaConstants.BEAN_UTILS_BEAN, BeanUtilsProvider.beanFor(Oddjob.CONVERTER_PROPERTIES));
89         context.set(ArooaConstants.RTC_LEAVE_PROXY, new Boolean JavaDoc(true));
90         Surrogate surrogate = new Surrogate();
91         
92         ArooaRuntime runtime = new ArooaRuntime(surrogate, "test", context);
93         context.set(ArooaConstants.CURRENTLY_CONFIGURING, runtime);
94         
95         InputSource JavaDoc inputSource = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(xml.getBytes()));
96         
97         XMLDefinitionHelper ph = new XMLDefinitionHelper(context);
98         ph.parse(inputSource, new MainHandler(new ValueHandler("whatever")));
99         
100         runtime.configure();
101         return surrogate.value;
102     }
103
104     public static Object JavaDoc createComponentFromXml(String JavaDoc xml) {
105         
106         SimpleObjectFactory componentFactory = new SimpleObjectFactory();
107         componentFactory.addResource(Oddjob.JOB_PROPERTIES);
108         
109         SimpleObjectFactory valueFactory = new SimpleObjectFactory();
110         valueFactory.addResource(Oddjob.TYPE_PROPERTIES);
111
112         PropertyProxyResolver propertyProxyResolver = new PropertyProxyResolver();
113         propertyProxyResolver.addResource(Oddjob.PROXY_PROPERTIES);
114
115         ArooaFactory af = new ArooaFactory();
116         af.setComponentFactory(componentFactory);
117         af.setValueFactory(valueFactory);
118         af.setPropertProxyResolver(propertyProxyResolver);
119         af.setBeanUtilsBean(BeanUtilsProvider.beanFor(Oddjob.CONVERTER_PROPERTIES));
120         
121         return af.build(xml);
122         
123     }
124 }
125
Popular Tags