Author: objectiser
Date: 2009-02-17 19:40:12 -0500 (Tue, 17 Feb 2009)
New Revision: 508
Added:
cdl/trunk/tools/plugins/org.jboss.tools.overlord.cdl.bpel/src/test/org/jboss/tools/overlord/cdl/bpel/generator/GeneratorTest.java
Log:
Updated test to compare bpel, generated from conversation model, to pre-defined bpel.
Added:
cdl/trunk/tools/plugins/org.jboss.tools.overlord.cdl.bpel/src/test/org/jboss/tools/overlord/cdl/bpel/generator/GeneratorTest.java
===================================================================
---
cdl/trunk/tools/plugins/org.jboss.tools.overlord.cdl.bpel/src/test/org/jboss/tools/overlord/cdl/bpel/generator/GeneratorTest.java
(rev 0)
+++
cdl/trunk/tools/plugins/org.jboss.tools.overlord.cdl.bpel/src/test/org/jboss/tools/overlord/cdl/bpel/generator/GeneratorTest.java 2009-02-18
00:40:12 UTC (rev 508)
@@ -0,0 +1,335 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.jboss.tools.overlord.cdl.bpel.generator;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.jboss.tools.overlord.cdl.bpel.model.BPELNotation;
+import org.jboss.tools.overlord.cdl.bpel.model.DefaultBPELLanguageModel;
+import org.scribble.conversation.model.ConversationNotation;
+import org.scribble.extensions.RegistryFactory;
+import org.scribble.model.ModelReference;
+import org.scribble.model.change.ModelGenerator;
+import org.w3c.dom.Node;
+
+import junit.framework.*;
+
+public class GeneratorTest extends TestCase {
+
+ public static TestSuite suite() {
+ TestSuite suite = new TestSuite("Conversation->BPEL Generator
Tests");
+ suite.addTest(new ConversationToBPELTest("ESBBrokerProcess@Broker"));
+ return suite;
+ }
+
+ /**
+ * The test case for running the conversation to BPEL test.
+ */
+ protected static class ConversationToBPELTest extends TestCase {
+
+ /**
+ * This constructor is initialized with the test
+ * name.
+ *
+ * @param name The test name
+ */
+ public ConversationToBPELTest(String name) {
+ super(name);
+ m_name = name;
+ }
+
+ /**
+ * This method runs the test.
+ *
+ * @param result The test result
+ */
+ public void run(TestResult result) {
+ result.startTest(this);
+
+ String filename="testmodels/"+m_name+".scv";
+
+ java.io.InputStream is=
+ GeneratorTest.class.getResourceAsStream(filename);
+
+ if (is == null) {
+ result.addError(this,
+ new Throwable("Unable to locate resource: "+filename));
+ } else {
+ org.scribble.parser.Parser p=new
org.scribble.conversation.parser.ConversationParser();
+ org.scribble.model.ModelReference ref=
+ new org.scribble.model.ModelReference(ConversationNotation.NOTATION_CODE);
+ org.scribble.model.admin.ModelListener l=
+ new org.scribble.model.admin.DefaultModelListener();
+
+ org.scribble.model.Model model=p.parse(ref, is, l);
+
+ if (model == null) {
+ result.addError(this, new Throwable("Model is null"));
+ } else {
+ ModelGenerator generator=(ModelGenerator)
+ RegistryFactory.getRegistry().getExtension(
+ ModelGenerator.class, null);
+
+ if (generator != null) {
+ ModelReference targetRef=
+ new ModelReference(BPELNotation.NOTATION_CODE);
+
+ DefaultBPELLanguageModel target=
+ new DefaultBPELLanguageModel(targetRef);
+
+ generator.generate(targetRef,
+ model.getModelName().getLocatedRole(),
+ target, model);
+
+ if (target.getBPELProcess() != null) {
+ try {
+ String text=getText(target.getBPELProcess(), true);
+
+ checkResults(result, text);
+ } catch(Exception e) {
+ result.addError(this, e);
+ }
+ } else {
+ result.addError(this,
+ new Throwable("No BPEL generated"));
+ }
+
+ } else {
+ result.addError(this,
+ new Throwable("Unable to find Model Generator"));
+ }
+ }
+ }
+
+ result.endTest(this);
+ }
+
+ /**
+ * This method checks the generated BPEL against a
+ * previously stored correct version.
+ *
+ * @param result The test result
+ * @param bpel The BPEL
+ */
+ protected void checkResults(TestResult result, String bpel) {
+
+ String filename="results/"+m_name+".bpel";
+
+ java.io.InputStream is=
+ GeneratorTest.class.getResourceAsStream(filename);
+
+ if (is != null) {
+
+ try {
+ byte[] b=new byte[is.available()];
+
+ is.read(b);
+
+ is.close();
+
+ String orig=new String(b);
+
+ if (orig.equals(bpel) == false) {
+ result.addError(this,
+ new Throwable("Generated BPEL does not match stored version"));
+
+ System.out.println("COMPARED GENERATED:");
+ System.out.println(bpel);
+ System.out.println("WITH ORIGINAL:");
+ System.out.println(orig);
+ }
+ } catch(Exception e) {
+ result.addError(this, e);
+ }
+ } else {
+ result.addError(this,
+ new Throwable("Resulting BPEL '"+filename+
+ "' not found for comparison"));
+ }
+ }
+
+ /**
+ * This class converts a DOM representation node to
+ * text with appropriate presentation formatting.
+ *
+ * @param node The DOM node
+ * @param pretty Whether to format the text for presentation
+ * @return The text
+ * @throws Exception Failed to convert
+ */
+ public static String getText(Node node, boolean pretty) throws Exception {
+ String ret=getText(node);
+
+ if (pretty) {
+ ret = reformat(ret);
+ }
+
+ return(ret);
+ }
+
+ /**
+ * This method reformats the XML text for presentation.
+ *
+ * @param xmltext The original XML text
+ * @return The reformatted text
+ */
+ protected static String reformat(String xmltext) {
+ String ret=xmltext;
+ int pos=0;
+ int prevpos=0;
+ StringBuffer buf=new StringBuffer();
+ int level=0;
+
+ while ((pos=ret.indexOf('<', prevpos)) != -1) {
+
+ if (prevpos < pos &&
+ ret.substring(prevpos, pos).trim().length() > 0 &&
+ ret.charAt(prevpos-1) != '?') {
+
+ if (ret.charAt(prevpos) == '\r' &&
+ ret.charAt(prevpos+1) == '\n') {
+ prevpos += 2;
+ }
+ for (int i=0; i < level; i++) {
+ buf.append(" ");
+ }
+
+ buf.append(ret.substring(prevpos, pos).trim());
+ buf.append("\r\n");
+ }
+
+ int endpos=ret.indexOf('>', pos);
+
+ if (endpos > 0) {
+ boolean noreturn=false;
+
+ if (pos > 0 && ret.charAt(pos+1) == '/') {
+ level--;
+ }
+
+ for (int i=0; i < level; i++) {
+ buf.append(" ");
+ }
+ buf.append(ret.substring(pos, endpos+1));
+
+ if (ret.charAt(endpos-1)== '?') {
+ //noreturn = true;
+
+ } else if (ret.charAt(endpos-1) == '/') {
+ // Ignore
+ } else if (pos > 0 && ret.charAt(pos+1) == '/') {
+ // Ignore
+
+ } else if (pos > 0 && ret.charAt(pos+1) == '!') {
+ // Ignore
+
+ } else {
+ level++;
+ }
+
+ if (noreturn == false) {
+ buf.append("\r\n");
+ }
+
+ pos = endpos+1;
+ }
+
+ prevpos = pos;
+ }
+
+ if (prevpos != -1 &&
+ ret.substring(prevpos).trim().length() > 0) {
+ buf.append(ret.substring(prevpos));
+ }
+
+ ret = buf.toString();
+
+ return(ret);
+ }
+
+ /**
+ * This class converts a DOM representation node to
+ * text.
+ *
+ * @param node The DOM node
+ * @return The text
+ * @throws Exception Failed to convert
+ */
+ public static String getText(Node node) throws Exception {
+ String ret=null;
+
+ try {
+ // Transform the DOM represent to text
+ java.io.ByteArrayOutputStream xmlstr=
+ new java.io.ByteArrayOutputStream();
+
+ DOMSource source=new DOMSource();
+ source.setNode(node);
+
+ StreamResult result=new StreamResult(xmlstr);
+
+ Transformer trans=
+ TransformerFactory.newInstance().newTransformer();
+ trans.transform(source, result);
+
+ xmlstr.close();
+
+ ret = new String(xmlstr.toByteArray());
+
+ if ((node instanceof org.w3c.dom.Document) == false) {
+
+ // Strip off any <?xml> header
+ int index=ret.indexOf("<?xml");
+ if (index != -1) {
+ index = ret.indexOf("<", 1);
+
+ if (index != -1) {
+ ret = ret.substring(index);
+ } else {
+ index = ret.indexOf("?>");
+
+ if (index != -1) {
+ index += 2;
+
+ // Remove any trailing whitespaces
+ // after XML header
+ while (index < ret.length() &&
+ Character.isWhitespace(ret.charAt(index))) {
+ index++;
+ }
+
+ ret = ret.substring(index);
+ }
+ }
+ }
+ }
+
+ } catch(Exception e) {
+ throw new Exception("Failed to transform " +
+ "DOM representation into text", e);
+ }
+
+ return(ret);
+ }
+
+ private String m_name=null;
+ }
+}