KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > AbstractJBossXBTest


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.xml;
23
24 import java.net.URL JavaDoc;
25
26 import org.jboss.test.AbstractTestCaseWithSetup;
27 import org.jboss.test.AbstractTestDelegate;
28 import org.jboss.util.Classes;
29 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
30
31 /**
32  * AbstractJBossXBTest.
33  *
34  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
35  * @version $Revision: 45077 $
36  */

37 public class AbstractJBossXBTest extends AbstractTestCaseWithSetup
38 {
39    private static final XmlDiff DIFF = new XmlDiff();
40
41    protected String JavaDoc rootName = getRootName();
42    
43    /**
44     * Create a new AbstractJBossXBTest.
45     *
46     * @param name the name of the test
47     */

48    public AbstractJBossXBTest(String JavaDoc name)
49    {
50       super(name);
51    }
52
53    public void assertXmlEqual(String JavaDoc expected, String JavaDoc was)
54    {
55       String JavaDoc diff = DIFF.diff(expected, was);
56       if(diff != null)
57       {
58          fail(diff);
59       }
60    }
61
62    /**
63     * Unmarshal some xml
64     *
65     * @param name the name
66     * @param expected the expected type
67     * @return the unmarshalled object
68     * @throws Exception for any error
69     */

70    protected Object JavaDoc unmarshal(String JavaDoc name, Class JavaDoc expected) throws Exception JavaDoc
71    {
72       Object JavaDoc object = unmarshal(name);
73       if (object == null)
74          fail("No object from " + name);
75       assertTrue("Object '" + object + "' cannot be assigned to " + expected.getName(), expected.isAssignableFrom(object.getClass()));
76       return object;
77    }
78    
79    /**
80     * Unmarshal some xml
81     *
82     * @param name the name
83     * @param expected the expected type
84     * @param resolver the resolver
85     * @return the unmarshalled object
86     * @throws Exception for any error
87     */

88    protected Object JavaDoc unmarshal(String JavaDoc name, Class JavaDoc expected, SchemaBindingResolver resolver) throws Exception JavaDoc
89    {
90       Object JavaDoc object = unmarshal(name, resolver);
91       if (object == null)
92          fail("No object from " + name);
93       assertTrue("Object '" + object + "' cannot be assigned to " + expected.getName(), expected.isAssignableFrom(object.getClass()));
94       return object;
95    }
96    
97    /**
98     * Unmarshal some xml
99     *
100     * @param name the name
101     * @return the unmarshalled object
102     * @throws Exception for any error
103     */

104    protected Object JavaDoc unmarshal(String JavaDoc name) throws Exception JavaDoc
105    {
106       return unmarshal(name, (SchemaBindingResolver) null);
107    }
108    
109    /**
110     * Unmarshal some xml
111     *
112     * @param name the name
113     * @return the unmarshalled object
114     * @throws Exception for any error
115     */

116    protected Object JavaDoc unmarshal(String JavaDoc name, SchemaBindingResolver resolver) throws Exception JavaDoc
117    {
118       String JavaDoc url = findXML(name);
119       return getJBossXBDelegate().unmarshal(url, resolver);
120    }
121    
122    /**
123     * Find the xml
124     *
125     * @param name the name
126     * @return the url of the xml
127     */

128    protected String JavaDoc findXML(String JavaDoc name)
129    {
130       URL JavaDoc url = getResource(name);
131       if (url == null)
132          fail(name + " not found");
133       return url.toString();
134    }
135    
136    /**
137     * Get a schema location
138     *
139     * @param clazz used to get the package
140     * @param schema the schema name
141     * @return the location in the "classpath"
142     */

143    protected static String JavaDoc getSchemaLocation(Class JavaDoc clazz, String JavaDoc schema)
144    {
145       String JavaDoc packageName = Classes.getPackageName(clazz);
146       packageName = packageName.replace('.', '/');
147       String JavaDoc name = packageName + '/' + schema;
148       return name;
149    }
150
151    /**
152     * Setup the test delegate
153     *
154     * @param clazz the class
155     * @return the delegate
156     * @throws Exception for any error
157     */

158    public static AbstractTestDelegate getDelegate(Class JavaDoc clazz) throws Exception JavaDoc
159    {
160       return new JBossXBTestDelegate(clazz);
161    }
162
163    protected JBossXBTestDelegate getJBossXBDelegate()
164    {
165       return (JBossXBTestDelegate) getDelegate();
166    }
167    
168    protected void setUp() throws Exception JavaDoc
169    {
170       super.setUp();
171       configureLogging();
172    }
173    
174    /**
175     * Get the package root name
176     *
177     * @return the root name
178     */

179    protected String JavaDoc getRootName()
180    {
181       String JavaDoc longName = getClass().getName();
182       int dot = longName.lastIndexOf('.');
183       if (dot != -1)
184          return longName.substring(dot + 1);
185       return longName;
186    }
187    
188    protected void configureLogging()
189    {
190       //enableTrace("org.jboss.xb");
191
}
192 }
193
Popular Tags