1 22 package org.jboss.test.xml; 23 24 import java.net.URL ; 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 37 public class AbstractJBossXBTest extends AbstractTestCaseWithSetup 38 { 39 private static final XmlDiff DIFF = new XmlDiff(); 40 41 protected String rootName = getRootName(); 42 43 48 public AbstractJBossXBTest(String name) 49 { 50 super(name); 51 } 52 53 public void assertXmlEqual(String expected, String was) 54 { 55 String diff = DIFF.diff(expected, was); 56 if(diff != null) 57 { 58 fail(diff); 59 } 60 } 61 62 70 protected Object unmarshal(String name, Class expected) throws Exception 71 { 72 Object 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 88 protected Object unmarshal(String name, Class expected, SchemaBindingResolver resolver) throws Exception 89 { 90 Object 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 104 protected Object unmarshal(String name) throws Exception 105 { 106 return unmarshal(name, (SchemaBindingResolver) null); 107 } 108 109 116 protected Object unmarshal(String name, SchemaBindingResolver resolver) throws Exception 117 { 118 String url = findXML(name); 119 return getJBossXBDelegate().unmarshal(url, resolver); 120 } 121 122 128 protected String findXML(String name) 129 { 130 URL url = getResource(name); 131 if (url == null) 132 fail(name + " not found"); 133 return url.toString(); 134 } 135 136 143 protected static String getSchemaLocation(Class clazz, String schema) 144 { 145 String packageName = Classes.getPackageName(clazz); 146 packageName = packageName.replace('.', '/'); 147 String name = packageName + '/' + schema; 148 return name; 149 } 150 151 158 public static AbstractTestDelegate getDelegate(Class clazz) throws Exception 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 169 { 170 super.setUp(); 171 configureLogging(); 172 } 173 174 179 protected String getRootName() 180 { 181 String 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 } 192 } 193 | Popular Tags |