1 5 package org.exoplatform.test.web.unit; 6 7 import java.util.* ; 8 import java.io.File ; 9 import org.exoplatform.test.web.*; 10 import com.meterware.httpunit.*; 11 17 public class SubmitFormUnit extends WebUnit { 18 private String formName_; 19 private ArrayList parameters_; 20 private String submitButton_ ; 21 22 public SubmitFormUnit(String name, String description) { 23 super(name, description); 24 parameters_ = new ArrayList(5); 25 } 26 27 public SubmitFormUnit setFormName(String name) { 28 formName_ = name; 29 return this; 30 } 31 32 public SubmitFormUnit setField(String field, String value) { 33 parameters_.add(new BasicParameter(field, value)); 34 return this; 35 } 36 37 public SubmitFormUnit setSubmitButton(String 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 )entry.getKey(), (String )entry.getValue())); 47 } 48 return this; 49 } 50 51 public SubmitFormUnit setFileField(String field, File value) { 52 ExoUploadFileSpec[] array = { new ExoUploadFileSpec(value) }; 53 parameters_.add(new FileParameter(field, array)); 54 return this; 55 } 56 57 public SubmitFormUnit setFileField(String field, String resourceName, java.io.InputStream 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 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 { 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 resourceName_ ; 91 public ExoUploadFileSpec(java.io.File file) { 92 super(file) ; 93 resourceName_ = file.getAbsolutePath(); 94 } 95 96 public ExoUploadFileSpec(String fileName, java.io.InputStream inputStream, String resourceName) { 97 super(fileName, inputStream, null) ; 98 resourceName_ = resourceName ; 99 } 100 } 101 102 static class BasicParameter implements Parameter { 103 String field_; 104 String value_; 105 106 public BasicParameter(String field, String value) { 107 field_ = field; 108 value_ = value; 109 } 110 111 public void setWebFormValue(WebForm form, ExoWebClient client) { 112 String value = getRealValue(client, value_); 113 if (form.isReadOnlyParameter(field_) || form.isHiddenParameter(field_)) { 114 form.getScriptableObject().setParameterValue(field_, value); 117 } else { 118 form.setParameter(field_, value); 119 } 120 } 121 } 122 123 static class FileParameter implements Parameter { 124 String field_; 125 ExoUploadFileSpec[] value_; 126 127 public FileParameter(String 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 field_; 139 boolean state_; 140 141 public CheckboxParameter(String field, boolean state) { 142 field_ = field; 143 state_ = state ; 144 } 145 146 public void setWebFormValue(WebForm form, ExoWebClient client) { 147 form.toggleCheckbox(field_) ; 149 } 150 } 151 152 public String getActionDescription() { 153 return "This web unit submit the data of a form."; 154 } 155 156 public String getExtraInfo() { 157 StringBuffer b = new StringBuffer () ; 158 b.append("<pre>") ; 159 b. append("Form name: ").append(formName_).append("\n") ; 160 for(int i = 0; i < parameters_.size() ; i++) { 161 Object 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 |