1   /*
2    * $Id: TokenTest.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  
18  /***
19   * <code>
20   * maven -Dtestcase=net.kwfgrid.gworkflowdl.TokenTest test:single
21   * </code>
22   *
23   * @author Andreas Hoheisel
24   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
25   * @version $Id: TokenTest.java 1227 2008-12-03 10:33:55Z andreas.hoheisel@first.fraunhofer.de $
26   */
27  public final class TokenTest extends AbstractTestCase {
28      /***
29       * Create the test case.
30       *
31       * @param testName name of the test case
32       */
33      public TokenTest(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(TokenTest.class);
42      }
43  
44      /***
45       * simplification of createDataToken generation.
46       *
47       * @param string content of the createDataToken Token
48       * @return createDataToken Token
49       */
50      public static Token createDataToken(String string) throws WorkflowFormatException {
51          Element data = new Element("data");
52          Element value = new Element("value");
53          value.setText(string);
54          data.setContent(value);
55          Token token = Factory.newToken(Factory.newData(data));
56          return token;
57      }
58  
59      /***
60       * simplification of createDataToken generation.
61       *
62       * @param string content of the createDataToken Token
63       * @return createDataToken Token
64       */
65      public static Token createDataToken(String string,GenericProperties properties) throws WorkflowFormatException {
66          Element data = new Element("data");
67          Element value = new Element("value");
68          value.setText(string);
69          data.setContent(value);
70          Token token = Factory.newToken(Factory.newData(data),properties);
71          return token;
72      }
73  
74      public void testTokenDataProperties() throws WorkflowFormatException {
75          String string1 = "regentag";
76          GenericProperties gp = Factory.newProperties();
77          gp.put("anton","schule");
78          gp.put("lisa", "zu hause");
79          Token token = createDataToken(string1, gp);
80  
81          // test properties
82          Assert.assertEquals("property anton", "schule", token.getProperties().get("anton"));
83          Assert.assertEquals("property lisa", "zu hause", token.getProperties().get("lisa"));
84          
85          // test control
86          Assert.assertNull(token.getControl());
87  
88          // test token name
89          Element element2 = (Element) token.getData().get();
90          String string2 = element2.getValue();
91          Assert.assertEquals("data content",string1, string2);
92  
93          // test clone()
94          Token t = (Token) token.clone();
95          Assert.assertEquals("token XML", token.getData().toXML(), t.getData().toXML());
96          System.out.println(token.getData().toXML());
97          System.out.println(t.getData().toXML());
98          try {
99              System.out.println(JdomString.element2string(TokenJdom.java2element(token)));
100             System.out.println(JdomString.element2string(TokenJdom.java2element(t)));
101         } catch (Exception e) {
102             Assert.fail(e.toString());
103         }
104 
105         // test adding new property to empty token
106         Token t2 = Factory.newToken();
107         t2.getProperties().put("n1","v1");
108         Assert.assertEquals("token property", "v1", t2.getProperties().get("n1"));
109     }
110 
111     public void testTokenData() throws WorkflowFormatException {
112         String string1 = "regentag";
113         Token token = createDataToken(string1);
114 
115         // test data
116         Assert.assertEquals("token XML", string1, ((Element) token.getData().get()).getValue());
117 
118         // test control
119         Assert.assertNull("Control", token.getControl());
120 
121         // test properties
122         Assert.assertNotNull("Properties", token.getProperties());
123 
124     }
125 
126     public void testTokenDataArray() throws WorkflowFormatException {
127         String xml =
128                 "<data xmlns=\"http://www.gridworkflow.org/gworkflowdl\">\r\n" +
129                 "  <x xmlns=\"\">1</x>\r\n" +
130                 "  <y xmlns=\"\">2</y>\r\n" +
131                 "</data>";
132         Data data = Factory.newData();
133         data.fromXML(xml);
134         Token token = Factory.newToken(data);
135 
136         Assert.assertEquals("token xml", xml, token.getData().toXML());
137     }
138 
139     public void testTokenDataException() {
140         boolean success = false;
141         String xml = "<notdata><x>1</x><y>2</y></notdata>";
142         Data data = Factory.newData();
143         try {
144             data.fromXML(xml);
145         } catch (WorkflowFormatException e) {
146             success = true;
147         }
148         Assert.assertTrue(success);
149     }
150 
151     public void testTokenControlFalse() {
152         Boolean control = new Boolean(false);
153         Token token = Factory.newToken(false);
154 
155         // test data
156         Assert.assertNull("Data", token.getData());
157 
158         // test control
159         Assert.assertEquals("Control", control, token.getControl());
160 
161         // test properties
162         Assert.assertNotNull("Properties", token.getProperties());
163 
164     }
165 
166     public void testTokenControlTrue() {
167         Boolean control = new Boolean(true);
168         Token token = Factory.newToken(true);
169 
170         // test data
171         Assert.assertNull("Data", token.getData());
172 
173         // test control
174         Assert.assertEquals("Control", control, token.getControl());
175 
176         // test properties
177         Assert.assertNotNull("Properties", token.getProperties());
178 
179     }
180 
181     public void testTokenControlProperties() {
182         Boolean control = new Boolean(false);
183         GenericProperties gp = Factory.newProperties();
184         gp.put("anton","schule");
185         gp.put("lisa", "zu hause");
186         Token token = Factory.newToken(false,gp);
187 
188         // test data
189         Assert.assertNull(token.getData());
190 
191         // test control
192         Assert.assertEquals("Control", control, token.getControl());
193 
194         // test properties
195         Assert.assertEquals("Properties", gp, token.getProperties());
196         
197     }
198 
199 
200 }