1   /*
2    * $Id: PlaceJdomTest.java 1227 2008-12-03 10:33:55Z andreas.hoheisel@first.fraunhofer.de $
3    *
4    * Copyright (c) 2005, The K-Wf Grid Consortium
5    * Fraunhofer Institute for Computer Architecture and Software Technology
6    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
7    */
8   
9   
10  package net.kwfgrid.gworkflowdl;
11  
12  import junit.framework.Assert;
13  import junit.framework.Test;
14  import junit.framework.TestSuite;
15  import net.kwfgrid.gworkflowdl.structure.*;
16  import org.jdom.Element;
17  import org.apache.log4j.Logger;
18  
19  
20  public final class PlaceJdomTest extends AbstractTestCase {
21  
22      private final static Logger logger = Logger.getLogger(PlaceJdomTest.class);
23  
24      /***
25       * <code>
26       * maven -Dtestcase=net.kwfgrid.gworkflowdl.PlaceJdomTest test:single
27       * </code>
28       *
29       * Create the test case.
30       *
31       * @param testName name of the test case
32       */
33      public PlaceJdomTest(String testName) {
34          super(testName);
35      }
36  
37      /***
38       * @return the suite of tests being tested
39       */
40      public static Test suite() {
41          return new TestSuite(PlaceJdomTest.class);
42      }
43  
44      public void testPlaceJdom() throws WorkflowFormatException {
45  
46          Place p1 = PlaceTest.exampleWithDataTokens("anton", 3);
47          //p1.setOwl("Himbeere");
48          p1.setTokenType("data");
49          p1.setDescription("blablabla");
50          GenericProperties gp = Factory.newProperties();
51          gp.put("key1", "val1");
52          gp.put("abrakadabra", "simsalabim");
53          p1.setProperties(gp);
54  
55          Element pe1 = PlaceJdom.java2element(p1);
56  
57          boolean test = true;
58          String pxml = null;
59          Element pe2 = null;
60  
61          try {
62              pxml = JdomString.element2string(pe1);
63          } catch (java.io.IOException ioe) {
64              System.out.println(ioe.toString());
65          }
66          try {
67              pe2 = JdomString.string2element(pxml);
68              System.out.println(pxml);
69          } catch (WorkflowFormatException e) {
70              test = false;
71          }
72  
73          Assert.assertTrue(test);
74          /*
75          try {
76              pxml = JdomString.element2string(pe2);
77              System.out.println(pxml);
78          //} catch (JDOMException e) {
79          //    test = false;
80          } catch (IOException e) {
81              test = false;
82          }
83          */
84          Assert.assertTrue(test);
85  
86  
87          Place p2 = null;
88          try {
89              p2 = PlaceJdom.element2java(pe2);
90          } catch (CapacityException e) {
91              test = false;
92          }
93          Assert.assertEquals("blablabla", p2.getDescription());
94          Assert.assertTrue(test);
95  
96          Assert.assertEquals(p1.getID(), p2.getID());
97  
98          Token[] t1s = p1.getTokens();
99          Token[] t2s = p2.getTokens();
100         Assert.assertEquals(t1s.length, t2s.length);
101         for (int i = 0; i < t1s.length; i++) {
102             Assert.assertEquals("token value", ((Element) t1s[i].getData().get()).getChildTextNormalize("value"),((Element) t2s[i].getData().get()).getChildTextNormalize("value"));
103         }
104 
105     }
106 
107     public void testControlPlaceJdom() throws WorkflowFormatException {
108 
109         Place p1 = PlaceTest.exampleWithControlTokens("anton", 2, true);
110         Element pe1 = PlaceJdom.java2element(p1);
111 
112         String pxml = null;
113         Element pe2 = null;
114 
115         try {
116             pxml = JdomString.element2string(pe1);
117         } catch (java.io.IOException ioe) {
118             Assert.fail(ioe.toString());
119         }
120         try {
121             pe2 = JdomString.string2element(pxml);
122             System.out.println(pxml);
123         } catch (WorkflowFormatException e) {
124             Assert.fail(e.toString());
125         }
126 
127 
128         Place p2 = null;
129         try {
130             p2 = PlaceJdom.element2java(pe2);
131         } catch (CapacityException e) {
132             Assert.fail(e.toString());
133         }
134 
135         Assert.assertEquals(p1.getID(), p2.getID());
136 
137         Token[] t1s = p1.getTokens();
138         Token[] t2s = p2.getTokens();
139         Assert.assertEquals(t1s.length, t2s.length);
140         for (int i = 0; i < t1s.length; i++) {
141             Assert.assertEquals("token value", t1s[i].getControl(), t2s[i].getControl());
142         }
143 
144     }
145 
146 }