001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: FillBeanGroup.java 3693 2007-03-14 10:55:56Z gbevin $
007: */
008: package com.uwyn.rife.engine.testelements.submission;
009:
010: import com.uwyn.rife.engine.Element;
011: import com.uwyn.rife.engine.exceptions.EngineException;
012: import com.uwyn.rife.resources.ResourceFinderClasspath;
013: import com.uwyn.rife.resources.exceptions.ResourceFinderErrorException;
014: import com.uwyn.rife.site.ValidationError;
015: import com.uwyn.rife.tools.FileUtils;
016: import com.uwyn.rife.tools.InnerClassException;
017: import com.uwyn.rife.tools.InputStreamUser;
018: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
019: import java.io.InputStream;
020: import java.io.StringBufferInputStream;
021: import java.util.Arrays;
022: import java.util.Set;
023:
024: public class FillBeanGroup extends Element {
025: public void processElement() {
026: print("<form name=\"submissionform\" action=\""
027: + getSubmissionFormUrl()
028: + "\" method=\"post\" enctype=\"multipart/form-data\">");
029: print(getSubmissionFormParameters("bean"));
030: print("<input type=\"text\" name=\"enum\">");
031: print("<input type=\"text\" name=\"string\">");
032: print("<input type=\"text\" name=\"boolean\">");
033: print("<input type=\"text\" name=\"string\">");
034: print("<input type=\"text\" name=\"stringbuffer\">");
035: print("<input type=\"text\" name=\"int\">");
036: print("<input type=\"text\" name=\"integer\">");
037: print("<input type=\"text\" name=\"char\">");
038: print("<input type=\"text\" name=\"character\">");
039: print("<input type=\"text\" name=\"boolean\">");
040: print("<input type=\"text\" name=\"booleanObject\">");
041: print("<input type=\"text\" name=\"byte\">");
042: print("<input type=\"text\" name=\"byteObject\">");
043: print("<input type=\"text\" name=\"double\">");
044: print("<input type=\"text\" name=\"doubleObject\">");
045: print("<input type=\"text\" name=\"float\">");
046: print("<input type=\"text\" name=\"floatObject\">");
047: print("<input type=\"text\" name=\"long\">");
048: print("<input type=\"text\" name=\"longObject\">");
049: print("<input type=\"text\" name=\"short\">");
050: print("<input type=\"text\" name=\"shortObject\">");
051: print("<input type=\"file\" name=\"stringFile\"/>");
052: print("<input type=\"file\" name=\"bytesFile\"/>");
053: print("<input type=\"file\" name=\"streamFile\"/>");
054: print("</form");
055: }
056:
057: public void doBean() {
058: BeanImpl bean = new BeanImpl();
059:
060: bean.setEnum(BeanImpl.Day.SATURDAY);
061: bean.setString("string");
062: bean.setStringbuffer(new StringBuffer("stringbuffer"));
063: bean.setInt(999);
064: bean.setInteger(new Integer(111));
065: bean.setChar('a');
066: bean.setCharacter(new Character('b'));
067: bean.setBoolean(false);
068: bean.setBooleanObject(new Boolean(true));
069: bean.setByte((byte) 22);
070: bean.setByteObject(new Byte((byte) 33));
071: bean.setDouble(123.45d);
072: bean.setDoubleObject(new Double(234.56d));
073: bean.setFloat(321.54f);
074: bean.setFloatObject(new Float(432.65f));
075: bean.setLong(44L);
076: bean.setLongObject(new Long(55L));
077: bean.setShort((short) 66);
078: bean.setShortObject(new Short((short) 77));
079: bean.setStringFile("stringFile");
080: bean.setBytesFile(new byte[] { 1, 2, 3 });
081: bean.setStreamFile(new StringBufferInputStream("streamFile"));
082:
083: fillSubmissionBean(bean);
084:
085: boolean notnumeric_int = false;
086: boolean notnumeric_double = false;
087: boolean notnumeric_longobject = false;
088:
089: Set<ValidationError> errors = bean.getValidationErrors();
090: for (ValidationError error : errors) {
091: if (error.getIdentifier().equals("NOTNUMERIC")) {
092: if (error.getSubject().equals("int")) {
093: notnumeric_int = true;
094: }
095: if (error.getSubject().equals("double")) {
096: notnumeric_double = true;
097: }
098: if (error.getSubject().equals("longObject")) {
099: notnumeric_longobject = true;
100: }
101: }
102: }
103: if (notnumeric_int) {
104: print("NOTNUMERIC : int\n");
105: }
106: if (notnumeric_double) {
107: print("NOTNUMERIC : double\n");
108: }
109: if (notnumeric_longobject) {
110: print("NOTNUMERIC : longObject\n");
111: }
112: print(bean.getEnum() + "," + bean.getString() + ","
113: + bean.getStringbuffer() + "," + bean.getInt() + ","
114: + bean.getInteger() + "," + bean.getChar() + ","
115: + bean.getCharacter() + "," + bean.isBoolean() + ","
116: + bean.getBooleanObject() + "," + bean.getByte() + ","
117: + bean.getByteObject() + "," + bean.getDouble() + ","
118: + bean.getDoubleObject() + "," + bean.getFloat() + ","
119: + bean.getFloatObject() + "," + bean.getLong() + ","
120: + bean.getLongObject() + "," + bean.getShort() + ","
121: + bean.getShortObject());
122: print("," + bean.getStringFile());
123:
124: try {
125: byte[] image_bytes = ResourceFinderClasspath.getInstance()
126: .useStream("uwyn.png", new InputStreamUser() {
127: public Object useInputStream(InputStream stream)
128: throws InnerClassException {
129: try {
130: return FileUtils.readBytes(stream);
131: } catch (FileUtilsErrorException e) {
132: throwException(e);
133: }
134:
135: return null;
136: }
137: });
138:
139: if (null == bean.getBytesFile()) {
140: print(",null");
141: } else {
142: print(","
143: + Arrays.equals(image_bytes, bean
144: .getBytesFile()));
145: }
146: print(","
147: + bean.getConstrainedProperty("bytesFile")
148: .getName());
149:
150: if (null == bean.getStreamFile()) {
151: print(",null");
152: } else {
153: print(","
154: + Arrays.equals(image_bytes, FileUtils
155: .readBytes(bean.getStreamFile())));
156: }
157: } catch (ResourceFinderErrorException e) {
158: throw new EngineException(e);
159: } catch (FileUtilsErrorException e) {
160: throw new EngineException(e);
161: }
162: }
163: }
|