KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > web > unit > SubmitFormUnit


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.test.web.unit;
6
7 import java.util.* ;
8 import java.io.File JavaDoc;
9 import org.exoplatform.test.web.*;
10 import com.meterware.httpunit.*;
11 /**
12  * May 21, 2004
13  * @author: Tuan Nguyen
14  * @email: tuan08@users.sourceforge.net
15  * @version: $Id: SubmitFormUnit.java,v 1.3 2004/10/12 14:21:12 tuan08 Exp $
16  **/

17 public class SubmitFormUnit extends WebUnit {
18   private String JavaDoc formName_;
19   private ArrayList parameters_;
20   private String JavaDoc submitButton_ ;
21
22   public SubmitFormUnit(String JavaDoc name, String JavaDoc description) {
23     super(name, description);
24     parameters_ = new ArrayList(5);
25   }
26
27   public SubmitFormUnit setFormName(String JavaDoc name) {
28     formName_ = name;
29     return this;
30   }
31
32   public SubmitFormUnit setField(String JavaDoc field, String JavaDoc value) {
33     parameters_.add(new BasicParameter(field, value));
34     return this;
35   }
36
37   public SubmitFormUnit setSubmitButton(String JavaDoc name) {
38     submitButton_ = name ;
39     return this;
40   }
41   
42   public SubmitFormUnit setField(Map map) {
43     Iterator i = map.entrySet().iterator() ;
44     while(i.hasNext()) {
45       Map.Entry entry = (Map.Entry) i.next() ;
46       parameters_.add(new BasicParameter((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue()));
47     }
48     return this;
49   }
50
51   public SubmitFormUnit setFileField(String JavaDoc field, File JavaDoc value) {
52     ExoUploadFileSpec[] array = { new ExoUploadFileSpec(value) };
53     parameters_.add(new FileParameter(field, array));
54     return this;
55   }
56   
57   public SubmitFormUnit setFileField(String JavaDoc field, String JavaDoc resourceName, java.io.InputStream JavaDoc in) {
58     ExoUploadFileSpec[] array = { new ExoUploadFileSpec(field, in, resourceName) };
59     parameters_.add(new FileParameter(field, array));
60     return this;
61   }
62   
63   
64   public SubmitFormUnit setCheckBox(String JavaDoc field, boolean state) {
65     parameters_.add(new CheckboxParameter(field, state));
66     return this;
67   }
68
69   public WebResponse execute(WebResponse previousResponse, WebTable block, ExoWebClient client)
70       throws Exception JavaDoc {
71     WebForm form = Util.findFormWithName(previousResponse, block, formName_);
72     for (int i = 0; i < parameters_.size(); i++) {
73       Parameter param = (Parameter) parameters_.get(i);
74       param.setWebFormValue(form, client) ;
75     }
76     WebResponse response = null ;
77     if(submitButton_ == null) {
78       response = form.submit();
79     } else {
80       response = form.submit(form.getSubmitButton(submitButton_));
81     }
82     return response;
83   }
84
85   interface Parameter {
86     public void setWebFormValue(WebForm form, ExoWebClient client) ;
87   }
88   
89   class ExoUploadFileSpec extends UploadFileSpec {
90     String JavaDoc resourceName_ ;
91     public ExoUploadFileSpec(java.io.File JavaDoc file) {
92       super(file) ;
93       resourceName_ = file.getAbsolutePath();
94     }
95     
96     public ExoUploadFileSpec(String JavaDoc fileName, java.io.InputStream JavaDoc inputStream, String JavaDoc resourceName) {
97       super(fileName, inputStream, null) ;
98       resourceName_ = resourceName ;
99     }
100   }
101   
102   static class BasicParameter implements Parameter {
103     String JavaDoc field_;
104     String JavaDoc value_;
105
106     public BasicParameter(String JavaDoc field, String JavaDoc value) {
107       field_ = field;
108       value_ = value;
109     }
110     
111     public void setWebFormValue(WebForm form, ExoWebClient client) {
112       String JavaDoc value = getRealValue(client, value_);
113       if (form.isReadOnlyParameter(field_) || form.isHiddenParameter(field_)) {
114         //HttpUnitOptions.setParameterValuesValidated( false ) ;
115
//form.getRequest().setParameter(param.field_ , value ) ;
116
form.getScriptableObject().setParameterValue(field_, value);
117       } else {
118         form.setParameter(field_, value);
119       }
120     }
121   }
122
123   static class FileParameter implements Parameter {
124     String JavaDoc field_;
125     ExoUploadFileSpec[] value_;
126
127     public FileParameter(String JavaDoc field, ExoUploadFileSpec[] value) {
128       field_ = field;
129       value_ = value;
130     }
131     
132     public void setWebFormValue(WebForm form, ExoWebClient client) {
133       form.setParameter(field_, value_);
134     }
135   }
136   
137   class CheckboxParameter implements Parameter {
138     String JavaDoc field_;
139     boolean state_;
140
141     public CheckboxParameter(String JavaDoc field, boolean state) {
142       field_ = field;
143       state_ = state ;
144     }
145
146     public void setWebFormValue(WebForm form, ExoWebClient client) {
147       //form.setCheckbox(field_, state_) ;
148
form.toggleCheckbox(field_) ;
149     }
150   }
151   
152   public String JavaDoc getActionDescription() {
153     return "This web unit submit the data of a form.";
154   }
155   
156   public String JavaDoc getExtraInfo() {
157     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
158     b.append("<pre>") ;
159     b. append("Form name: ").append(formName_).append("\n") ;
160     for(int i = 0; i < parameters_.size() ; i++) {
161       Object JavaDoc o = parameters_.get(i) ;
162       if(o instanceof BasicParameter) {
163         BasicParameter param = (BasicParameter) o ;
164         b.append("Field: ").append(param.field_).append(", value: ").append(param.value_).append("\n") ;
165       } else if(o instanceof FileParameter) {
166         FileParameter param = (FileParameter) o ;
167         b.append("Field: ").append(param.field_).append("\n") ;
168         for(int j = 0; j < param.value_.length; j++) {
169           b.append(" file: ").append(param.value_[j].resourceName_).append("\n") ;
170         }
171       } else if(o instanceof CheckboxParameter ) {
172         CheckboxParameter param = (CheckboxParameter) o ;
173         b.append("Field: ").append(param.field_).append(", state: ").append(param.state_).append("\n") ;
174       }
175     }
176     b.append("</pre>") ;
177     return b.toString() ;
178   }
179 }
180
Popular Tags