View Javadoc

1   package net.kwfgrid.gwes;
2   
3   import java.io.*;
4   import java.util.List;
5   import java.rmi.RemoteException;
6   
7   import junit.framework.TestCase;
8   import junit.framework.Assert;
9   import net.kwfgrid.gwes.exception.NoSuchWorkflowException;
10  import net.kwfgrid.gwes.exception.DatabaseException;
11  import net.kwfgrid.gwes.exception.StateTransitionException;
12  import net.kwfgrid.gworkflowdl.protocol.*;
13  import net.kwfgrid.gworkflowdl.protocol.xupdate.XUModificationHandler;
14  import net.kwfgrid.gworkflowdl.protocol.xupdate.XUMethodCallEncoder;
15  import net.kwfgrid.gworkflowdl.protocol.server.DefaultServerDelegateExt;
16  import net.kwfgrid.gworkflowdl.protocol.server.ServerCreator;
17  import net.kwfgrid.gworkflowdl.structure.*;
18  import org.apache.log4j.Logger;
19  import org.jdom.JDOMException;
20  import org.jdom.Document;
21  import org.jdom.Namespace;
22  import org.jdom.Element;
23  import org.jdom.input.SAXBuilder;
24  import org.jaxen.JaxenException;
25  import org.jaxen.XPath;
26  import org.jaxen.SimpleNamespaceContext;
27  import org.jaxen.jdom.JDOMXPath;
28  
29  
30  /**
31   * Abstract base class for test cases.
32   */
33  public abstract class AbstractTestCase
34      extends TestCase  {
35  
36      static Logger logger = Logger.getLogger(AbstractTestCase.class);
37  
38      public final String userID;
39  
40      public final SimpleNamespaceContext jaxenNamespaceContext;
41  
42      public final static Namespace NAMESPACE_GWDL = Namespace.getNamespace("http://www.gridworkflow.org/gworkflowdl");
43  
44      /**
45       * Basedir for all file I/O. Important when running tests from
46       * the reactor.
47       */
48      public String basedir = System.getProperty("basedir");
49  
50      /**
51       * Constructor.
52       */
53      public AbstractTestCase(String testName)
54      {
55          super(testName);
56          userID = System.getProperty("user.name");
57          jaxenNamespaceContext = new SimpleNamespaceContext();
58          jaxenNamespaceContext.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
59          jaxenNamespaceContext.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
60          jaxenNamespaceContext.addNamespace("gwdl", "http://www.gridworkflow.org/gworkflowdl");
61      }
62  
63      /**
64       * Get test input file.
65       *
66       * @param path Path to test input file.
67       */
68      public String getTestFile(String path)
69      {
70  
71          File file;
72          // absolute path
73          if (path.startsWith("/")) {
74              file = new File(path);
75          }
76          // relative path
77          else {
78              if (basedir == null) {
79                  basedir = "./";
80              }
81              // check <basedir>/target/test-classes/...
82              file = new File(basedir+"/target/test-classes",path);
83              if (!file.exists()) {
84                  // check <basedir>/...
85                  file = new File(basedir,path);
86              }
87          }
88          return file.getAbsolutePath();
89      }
90  
91       public List<Element> extractTokenChildElements(String xml) throws IOException, JDOMException, JaxenException {
92           final SAXBuilder builder = new SAXBuilder(false);
93  //         builder.setFeature("http://apache.org/xml/features/validation/schema", true);
94  //         String wsdldefs = "http://schemas.xmlsoap.org/wsdl/ http://schemas.xmlsoap.org/wsdl/";
95  //         builder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", wsdldefs);
96           Document document = builder.build(new StringReader(xml));
97           XPath xpath = new JDOMXPath("/gwdl:workflow/gwdl:place/gwdl:token/*");
98           xpath.setNamespaceContext(jaxenNamespaceContext);
99           return xpath.selectNodes(document);
100      }
101 
102     public List<Element> extractTokenElements(String xml) throws IOException, JDOMException, JaxenException {
103         final SAXBuilder builder = new SAXBuilder(false);
104         Document document = builder.build(new StringReader(xml));
105         XPath xpath = new JDOMXPath("/gwdl:workflow/gwdl:place/gwdl:token");
106         xpath.setNamespaceContext(jaxenNamespaceContext);
107         return xpath.selectNodes(document);
108     }
109 
110     public String stripTokenIDs(String xml) {
111         if (xml==null) return null;
112         String noD = xml.replaceAll("token ID=\"d.*\"","token ID=\"dX\"");
113         return noD.replaceAll("token ID=\"c.*\"","token ID=\"cX\"");
114     }
115 
116      public String extractOccurrenceSequence(String xml) throws IOException, JDOMException, JaxenException {
117         final SAXBuilder builder = new SAXBuilder(false);
118         SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
119         nsContext.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
120         nsContext.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
121         nsContext.addNamespace("gwdl", "http://www.gridworkflow.org/gworkflowdl");
122         Document document = builder.build(new StringReader(xml));
123         XPath xpath = new JDOMXPath("/gwdl:workflow/gwdl:property[@name=\"occurrence.sequence\"]");
124         xpath.setNamespaceContext(nsContext);
125         return xpath.stringValueOf(document);
126     }
127 
128 
129      public String readfile(String fn) throws IOException {
130          BufferedReader in = (new BufferedReader(new FileReader(getTestFile(fn))));
131          String line;
132          StringBuffer buffer = new StringBuffer();
133          line = in.readLine();
134          while (line != null) {
135              buffer.append(line);
136              buffer.append('\n');
137              line = in.readLine();
138          }
139          return buffer.toString();
140      }
141 
142     public List xpath(String xpathS, String xml) throws IOException, JDOMException, JaxenException {
143         final SAXBuilder builder = new SAXBuilder(false);
144         Document document = builder.build(new StringReader(xml));
145         SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
146         nsContext.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
147         nsContext.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
148         nsContext.addNamespace("gwdl", "http://www.gridworkflow.org/gworkflowdl");
149         XPath xpath = new JDOMXPath(xpathS);
150         xpath.setNamespaceContext(nsContext);
151         return xpath.selectNodes(document);
152     }
153 
154     protected void configureFactory() {
155         //configure protocol
156         IMethodCallStrategy defaults = new DefaultMethodCallStrategy();
157         IMethodCallStrategy protocol = new DefaultServerDelegateExt();
158         IModificationHandler handler = new XUModificationHandler();
159         IMethodCallEncoder encoder = new XUMethodCallEncoder();
160         Protocol.setDefaultMethodCallStrategy(defaults);
161         Protocol.setProtocolMethodCallStrategy(protocol);
162         Protocol.setModificationHandler(handler);
163         Protocol.setMethodCallEncoder(encoder);
164         Creator creator = new ServerCreator(new DefaultCreator());
165         Factory.setCreator(creator);
166     }
167 
168 
169 }