Skip to content

Instantly share code, notes, and snippets.

@tfennelly
Created April 11, 2011 20:40
Show Gist options
  • Save tfennelly/914283 to your computer and use it in GitHub Desktop.
Save tfennelly/914283 to your computer and use it in GitHub Desktop.
public static void testModel(String ediMappingModelFile, String ediMessageFile, String factoryClassName, boolean dump) throws EDIConfigurationException, IOException, SAXException, IllegalNameException {
StackTraceElement[] thisStack = Thread.currentThread().getStackTrace();
Class callerClass = null;
for(int i = 0; i < thisStack.length; i++) {
if(thisStack[i].getClassName().equals(EJCTestUtil.class.getName())) {
try {
callerClass = Class.forName(thisStack[i + 1].getClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
TestCase.fail("Exception resolving caller class: " + e.getMessage());
}
}
}
if(callerClass == null) {
TestCase.fail("Failed to resolve caller class.");
}
InputStream mappingModel = callerClass.getResourceAsStream(ediMappingModelFile);
testModel(ediMessageFile, mappingModel, factoryClassName, dump, callerClass);
}
public static void testModel(String ediMessageFile, InputStream mappingModelStream, String factoryClassName, boolean dump, Class callerClass) throws IOException, SAXException, IllegalNameException {
Archive archive = null;
try {
archive = buildModelArchive(mappingModelStream, ORG_SMOOKS_EJC_TEST);
} catch (ClassNotFoundException e) {
e.printStackTrace();
TestCase.fail("Exception building model archive: " + e.getMessage());
}
testModel(archive, ediMessageFile, factoryClassName, dump, callerClass);
}
public static void testModel(Archive archive, String ediMessageFile, String factoryClassName, boolean dump, Class callerClass) throws IOException {
ArchiveClassLoader classLoader = new ArchiveClassLoader(archive);
Thread.currentThread().setContextClassLoader(classLoader);
try {
Class factoryClass = null;
Object factoryInstance = null;
try {
factoryClass = classLoader.loadClass(ORG_SMOOKS_EJC_TEST + "." + factoryClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
TestCase.fail("Exception loading model Factory class: " + e.getMessage());
}
try {
factoryInstance = factoryClass.getMethod("getInstance").invoke(null);
} catch (Exception e) {
e.printStackTrace();
TestCase.fail("Exception creating Factory class instance: " + e.getMessage());
}
String ediMessage = StreamUtils.readStreamAsString(callerClass.getResourceAsStream(ediMessageFile));
Object modelInstance = null;
try {
modelInstance = findFromEDIMethod(factoryClass).invoke(factoryInstance, new StringReader(ediMessage));
} catch (Exception e) {
e.printStackTrace();
TestCase.fail("Exception invoking 'fromEDI' method on Factory class instance: " + e.getMessage());
}
StringWriter ediOut = new StringWriter();
try {
findToEDIMethod(factoryClass).invoke(factoryInstance, modelInstance, ediOut);
} catch (Exception e) {
e.printStackTrace();
TestCase.fail("Exception invoking 'fromEDI' method on Factory class instance: " + e.getMessage());
}
if(dump) {
System.out.println("\n==== Serialized EDI Model ====");
System.out.println(ediOut.toString());
System.out.println("==============================\n");
}
TestCase.assertEquals(StreamUtils.normalizeLines(ediMessage, false).trim(), ediOut.toString().trim());
} finally {
Thread.currentThread().setContextClassLoader(classLoader.getParent());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment