View Javadoc

1   /*
2       Jagacy plug-in - A TN3270 plug-in for Jameleon.
3       Copyright (C) 2005 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.plugin.jagacy;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import com.jagacy.Key;
25  import com.jagacy.Session3270;
26  import com.jagacy.util.JagacyException;
27  
28  import junit.framework.Assert;
29  
30  import net.sf.jameleon.function.FunctionTag;
31  import net.sf.jameleon.util.AssertLevel;
32  import net.sf.jameleon.util.JameleonUtility;
33  
34  public abstract class JagacyFunctionTag extends FunctionTag{
35  
36      protected JagacySessionTag sessionTag;
37      protected Session3270 session;
38  
39      /***
40       * Gets the references of the session and the test results from the parent TestCase
41       * This method gets called once all attributes are set from the macro language. Any required
42       * setup should go here.
43       */
44      public void setupEnvironment() {
45          super.setupEnvironment();
46  
47          Object obj = findAncestorWithClass(JagacySessionTag.class);
48          if (obj instanceof JagacySessionTag) {
49              sessionTag = (JagacySessionTag) obj;
50              session = sessionTag.getSession3270();
51  
52          } else {
53              throw new ClassCastException("Can only execute a Jagacy function tag under the Jagacy session tag ( jagacy-session )! " +
54                      "Please change the session tag surrounding function point " + getClass().getName());
55          }
56      }
57  
58      public void pluginTearDown() {
59          session = null;
60      }
61  
62      //Storable Method
63      public void store(String fName, int event) throws IOException {
64          try {
65              String text = null;
66              String filename = fName + ".txt";
67              text = getScreen();
68              if (text != null && text.length() > 0) {
69                  File f = new File(filename);
70                  JameleonUtility.recordResultsToFile(f, text);
71                  getFunctionResults().setErrorFile(f);
72              } else {
73                  log.debug("Not writing results to file because there is no html to be written.");
74              }
75  
76          } catch (IOException ioe){
77              throw new IOException(JameleonUtility.createErrMsg("Unable to get response to store to file in function <" + functionId + ">:\n\r " + JameleonUtility.getStack(ioe)));
78          } catch (RuntimeException re) {
79              re.printStackTrace();
80              throw new IOException(JameleonUtility.createErrMsg("Unable to get response to store to file in function <" + functionId + ">:\n\r " + JameleonUtility.getStack(re)));
81          }
82      }
83      //End Storable Method
84  
85      //Jagacy utility methods
86  
87  
88      public String getScreen(){
89          StringBuffer screen = new StringBuffer();
90          try{
91              String[] lines = session.readScreen();
92              for (int i = 0; i < lines.length; i++) {
93                  screen.append(lines[i]).append("\n");
94              }
95          }catch(JagacyException je){
96              throw new RuntimeException("Could not retrieve text from the current screen", je);
97          }
98          return screen.toString();
99      }
100 
101     /***
102      * Reads the value in the given field.
103      * @param field - The field number, indexed from 1
104      * @param offset - Offset in the field, indexed from 1
105      * @return The value of the given field.
106      */
107     public String readField(int field, int offset){
108         String value = null;
109         try{
110             int length = session.readFieldLength(field);
111             if (length > 0){
112             	length --;
113             }
114             value = session.readField(field, offset, length);
115         }catch(JagacyException je){
116             throw new RuntimeException("Could not retrieve text from the field '"+field+"' with offset '"+offset+"'.", je);
117         }
118         return value;
119     }
120 
121     /***
122      * Reads the value at the current position.
123      * @param row - The row number, indexed from 0
124      * @param column - The column number, indexed from 0
125      * @param length - The length of string to read
126      * @return The value of the given position.
127      */
128     public String readPosition(int row, int column, int length){
129         String value = null;
130         try{
131             value = session.readPosition(row, column, length);
132         }catch(JagacyException je){
133             throw new RuntimeException("Could not retrieve text from the position row '"+row+"', column '"+column+"'.", je);
134         }
135         return value;
136     }
137 
138     /***
139      * Reads the current screen.
140      * @return An array of lines on the screen.
141      */
142     public String[] readScreen(){
143         String[] lines = null;
144         try{
145             lines = session.readScreen();
146         }catch(JagacyException je){
147             throw new RuntimeException("Could not retrieve text from the current screen", je);
148         }
149         return lines;
150     }
151 
152     /***
153      * Waits for <code>timeInMillis</code> for the screen to change state.
154      * @param timeInMillis - the time in milliseconds to wait until the screen changes.
155      * @return true if the screen changed
156      */
157     public boolean waitForChange(int timeInMillis){
158         boolean changeOccured = false;
159         try{
160             changeOccured = session.waitForChange(timeInMillis);
161         }catch(JagacyException je){
162             throw new RuntimeException("Error while waiting for the screen to change!", je);
163         }
164         return changeOccured;
165     }
166 
167     /***
168      * Waits (in 100 millisecond intervals) for the value to appear at the given field and offset.     
169      * @param field - The field number, indexed from 1
170      * @param offset - Offset in the field, indexed from 1
171      * @param value - The expected value of the field
172      * @param timeInMillis - the time in milliseconds to wait until the screen changes.
173      * @return true if the field is displayed
174      */
175     public boolean waitForField(int field, int offset, String value, int timeInMillis){
176         boolean fieldShown = false;
177         try{
178             fieldShown = session.waitForField(field, offset, value, timeInMillis);
179         }catch(JagacyException je){
180             throw new RuntimeException("Error while waiting for field '"+field+"' at offset '"+offset+"' with the value '"+value+"'!", je);
181         }
182         return fieldShown;
183     }
184 
185     /***
186      * Waits (in 100 millisecond intervals) for the value to appear at the given coordinates.
187      * @param row - The row number, indexed from 0
188      * @param column - The column number, indexed from 0
189      * @param value - The expected value at this position
190      * @param timeInMillis - the time in milliseconds to wait until the screen changes.
191      * @return true if the position is displayed
192      */
193     public boolean waitForPosition(int row, int column, String value, int timeInMillis){
194         boolean positionShown = false;
195         try{
196             positionShown = session.waitForPosition(row, column, value, timeInMillis);
197         }catch(JagacyException je){
198             throw new RuntimeException("Error while waiting for value '"+value+"' at position row '"+row+"', column '"+column+"'!", je);
199         }
200         return positionShown;
201     }
202 
203     /***
204      * Waits for <code>timeInMillis</code> for the keyboard to unlock.
205      * @param timeInMillis - the time in milliseconds to wait until the screen changes.
206      * @return true if the keyboard unlocked
207      */
208     public boolean waitForUnlock(int timeInMillis){
209         boolean unlocked = false;
210         try{
211             unlocked = session.waitForUnlock(timeInMillis);
212         }catch(JagacyException je){
213             throw new RuntimeException("Error while waiting for the keyboard to unlock!", je);
214         }
215         return unlocked;
216     }
217 
218     /***
219      * Finds a label and attempts to enter the given text in the field directly after the label
220      * @param label - the text to the left of the field
221      * @param value - the value to set the field to.
222      */
223     public void writeAfterLabel(String label, String value){
224         try{
225             session.writeAfterLabel(label, value);
226         }catch(JagacyException je){
227             throw new RuntimeException("Couldn't write '"+value+"' to field '"+label+"'", je);
228         }
229     }
230 
231     /***
232      * Writes the cursor at the given coordinates
233      * @param row - the row the set the cursor on. Indexed from 0.
234      * @param column - the column to set the cursor on. Indexed from 0.
235      */
236     public void writeCursor(int row, int column){
237         try{
238             session.writeCursor(row, column);
239         }catch(JagacyException je){
240             throw new RuntimeException("Couldn't place cursor on row '"+row+"' and column, '"+column+"'!", je);
241         }
242     }
243 
244     /***
245      * Writes the given text to the nth field
246      * @param field - The field number, indexed from 1
247      * @param offset - Offset in the field, indexed from 1
248      * @param value - The value to set the field to
249      */
250     public void writeField(int field, int offset, String value){
251         try{
252             session.writeField(field, offset, value);
253         }catch(JagacyException je){
254             throw new RuntimeException("Couldn't write '"+value+"' to field, '"+field+"' with offset '"+offset+"'!", je);
255         }
256     }
257 
258     /***
259      * Writes the given Key to the screen.
260      * @param key - the key to write to the screen
261      */
262     public void writeKey(Key key){
263         try{
264             session.writeKey(key);
265         }catch(JagacyException je){
266             throw new RuntimeException("Couldn't write key, '"+key+"' to the screen!", je);
267         }
268     }
269 
270     /***
271      * Writes a string at the given coordinates
272      * @param row - the row the field exists at. Indexed from 0.
273      * @param column - the column the field exists at. Indexed from 0.
274      * @param value - the value to set the field to.
275      */
276     public void writePosition(int row, int column, String value){
277         try{
278             session.writePosition(row, column, value);
279         }catch(JagacyException je){
280             throw new RuntimeException("Couldn't write '"+value+"' to row '"+row+"', column '"+column+"'.", je);
281         }
282     }
283 
284     /***
285      * Writes the given String to the current cursor position
286      * @param value - the value to write to the current cursor position.
287      */
288     public void writeString(String value){
289         try{
290             session.writeString(value);
291         }catch(JagacyException je){
292             throw new RuntimeException("Couldn't write '"+value+"' to the screen!", je);
293         }
294     }
295 
296 
297     //Jagacy assert methods
298     /***
299      * Checks that the provided text exists in the current screen
300      * @param text - the text that should be contained in the current screen
301      */
302     public void assertTextPresent(String text){
303         int assertLevel = AssertLevel.NO_LEVEL;
304         String message = "assertTextPresent: '"+text+"' not found in current screen!";
305         assertTextPresent(message, text, assertLevel);
306     }
307 
308     /***
309      * Checks that the provided text exists in the current screen
310      * @param text - the text that should be contained in the current screen
311      * @param assertLevel - only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel} 
312      */
313     public void assertTextPresent(String text, int assertLevel){
314         String message = "assertTextPresent: '"+text+"' not found in current screen!";
315         assertTextPresent(message, text, assertLevel);
316     }
317 
318     /***
319      * Checks that the provided text exists in the current screen
320      * @param message - the message to provide if an error occurs
321      * @param text - the text that should be contained in the current screen
322      */
323     public void assertTextPresent(String message, String text){
324         int assertLevel = AssertLevel.NO_LEVEL;
325         assertTextPresent(message, text, assertLevel);
326     }
327 
328     /***
329      * Checks that the provided text exists in the current screen
330      * @param message - the message to provide if an error occurs
331      * @param text - the text that should be contained in the current screen
332      * @param assertLevel - only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel} 
333      */
334     public void assertTextPresent(final String message, final String text, final int assertLevel){
335         String fullText = getScreen();
336         if (fullText != null) {
337             assertTextContains(message, fullText, text, assertLevel);
338         }else{
339             fail(message + " no text on current screen!",assertLevel);
340         }
341     }
342 
343     /***
344      * Checks that the provided text doesn't exist in the current screen
345      * @param text - the text that should not be contained in the current screen
346      */
347     public void assertTextNotPresent(String text){
348         int assertLevel = AssertLevel.NO_LEVEL;
349         String message = "assertTextNotPresent: '"+text+"' not found in current screen!";
350         assertTextNotPresent(message, text, assertLevel);
351     }
352 
353     /***
354      * Checks that the provided text doesn't exist in the current screen
355      * @param text - the text that should not be contained in the current screen
356      * @param assertLevel - only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel} 
357      */
358     public void assertTextNotPresent(String text, int assertLevel){
359         String message = "assertTextNotPresent: '"+text+"' not found in current screen!";
360         assertTextNotPresent(message, text, assertLevel);
361     }
362 
363     /***
364      * Checks that the provided text exists in the current screen
365      * @param msg - the message to provide if an error occurs
366      * @param text - the text that should not be contained in the current screen
367      */
368     public void assertTextNotPresent(String msg, String text){
369         int assertLevel = AssertLevel.NO_LEVEL;
370         assertTextNotPresent(msg, text, assertLevel);
371     }
372 
373     /***
374      * Checks that the provided text doesn't exist in the current screen
375      * @param msg - the message to provide if an error occurs
376      * @param text - the text that should not be contained in the current screen
377      * @param assertLevel - only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel} 
378      */
379     public void assertTextNotPresent(final String msg, final String text, final int assertLevel){
380         assertMethodWithLevel(new Runnable() {
381                 public void run() {
382                     String fullText = getScreen();
383                     if (fullText != null) {
384                         Assert.assertTrue(msg, fullText.indexOf(text) == -1);
385                     }
386                 }
387         },assertLevel);
388     }
389 
390 }