1   /*
2    * $Id: PlaceTest.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   package net.kwfgrid.gworkflowdl;
10  
11  import junit.framework.Assert;
12  import junit.framework.Test;
13  import junit.framework.TestSuite;
14  import net.kwfgrid.gworkflowdl.structure.*;
15  import org.jdom.Element;
16  
17  
18  public final class PlaceTest extends AbstractTestCase {
19      /***
20       * Create the test case
21       *
22       * @param testName name of the test case
23       */
24      public PlaceTest(String testName) {
25          super(testName);
26      }
27  
28      /***
29       * @return the suite of tests being tested
30       */
31      public static Test suite() {
32          return new TestSuite(PlaceTest.class);
33      }
34  
35      public static boolean addToken(Place p) {
36          if (p.getTokenNumber() == p.getCapacity()) {
37              return false;
38          }  else {
39              Token token = Factory.newToken();
40              try {
41                  p.addToken(token);
42              } catch (CapacityException e) {
43                  // impossible
44              }
45              return true;
46          }
47      }
48  
49      public static boolean setCapacity(Place p, int c) {
50              if (p.getTokenNumber() > c) {
51                  return false;
52              }  else {
53                  try {
54                      p.setCapacity(c);
55                  } catch (CapacityException e) {
56                      // impossible
57                  }
58                  return true;
59              }
60          }
61  
62      /***
63       * createControlToken Place with a variable number of different tokens.
64       * support for test which need createControlToken Places
65       *
66       * @param id          createControlToken Place ID
67       * @param tokenNumber number of Tokens
68       * @return
69       */
70      public static Place exampleWithControlTokens(String id, int tokenNumber, boolean tokenValue) {
71          Place place = Factory.newPlace();
72          place.setID(id);
73          try {
74              place.setCapacity(Place.INFINITY);
75              for (int i = 0; i < tokenNumber; i++) {
76                  place.addToken(Factory.newToken(tokenValue));
77              }
78          } catch (CapacityException e) {
79              Assert.assertTrue("createControlToken wrong", false);
80          }
81          place.setTokenType("control");
82          return place;
83      }
84  
85      /***
86       * createDataToken Place with a variable number of different tokens.
87       * support for test which need createDataToken Places
88       *
89       * @param id          createDataToken Place ID
90       * @param tokenNumber number of Tokens
91       * @return
92       */
93      public static Place exampleWithDataTokens(String id, int tokenNumber) {
94          Place place = Factory.newPlace();
95          place.setID(id);
96          try {
97              place.setCapacity(Place.INFINITY);
98              for (int i = 0; i < tokenNumber; i++) {
99                  place.addToken(TokenTest.createDataToken(id + '.' + i));
100             }
101         } catch (CapacityException e) {
102             Assert.assertTrue("createDataToken wrong", false);
103         } catch (WorkflowFormatException e) {
104             Assert.fail("exception: " + e);
105         }
106         place.setTokenType("data");
107         return place;
108     }
109 
110     // ------------------------------------------
111     /***
112      * Test place
113      */
114     public void testPlace() throws WorkflowFormatException {
115         Place place = Factory.newPlace();
116         boolean test = true;
117 
118         Assert.assertTrue(place.isEmpty());
119         Assert.assertEquals(place.getID(), Place.DEFAULT_ID);
120         Assert.assertEquals(place.getTokens().length, Place.DEFAULT_TOKEN_NUMBER);
121         Assert.assertEquals(place.getCapacity(), Place.DEFAULT_CAPACITY);
122 
123         String id1 = "helmut";
124         place.setID(id1);
125         String id2 = place.getID();
126         Assert.assertEquals("place ID", id1, id2);
127 
128         Token t1 = TokenTest.createDataToken("anton");
129         Token t2 = TokenTest.createDataToken("berta");
130         try {
131             place.addToken(t1);
132         } catch (CapacityException e) {
133             test = false;
134         }
135         Assert.assertFalse(place.isEmpty());
136 
137         try {
138             place.addToken(t2);
139         } catch (CapacityException e) {
140             test = false;
141         }
142         Assert.assertTrue(test);
143         test = true;
144         try {
145             place.setCapacity(4711);
146         } catch (CapacityException e) {
147             test = false;
148         }
149         Assert.assertTrue(test);
150         try {
151             place.addToken(t2);
152         } catch (CapacityException e) {
153             test = false;
154         }
155         Assert.assertTrue(test);
156 
157         Token[] ts = place.getTokens();
158         Assert.assertEquals("Number of tokens", 3, ts.length);
159         Assert.assertEquals("anton", (String) ((Element) t1.getData().get()).getValue());
160         Assert.assertEquals("berta", (String) ((Element) t2.getData().get()).getValue());
161 
162         try {
163             place.setCapacity(17);
164         } catch (CapacityException e) {
165             test = false;
166         }
167         Assert.assertEquals(17, place.getCapacity());
168 
169         place.removeToken(t1);
170         Assert.assertEquals("Number of tokens", 2, place.getTokens().length);
171         Assert.assertEquals("berta", ((Element) place.getTokens()[0].getData().get()).getValue());
172 
173         try {
174             place.addToken(t1);
175         } catch (CapacityException e) {
176             test = false;
177         }
178         place.removeAllTokens();
179         Assert.assertTrue(place.isEmpty());
180 
181         // up to now all addToken ok
182         Assert.assertTrue(test);
183 
184         // exceeding place capacity
185         Place p = Factory.newPlace();
186         try {
187             p.setCapacity(1);
188         } catch (CapacityException e) {
189             test = false;
190         }
191         try {
192             p.addToken(t1);
193             p.addToken(t2);
194         } catch (CapacityException e) {
195             test = false;
196             //e.printStackTrace();
197         }
198         Assert.assertFalse(test);
199 
200     }
201 }