001: package org.apache.turbine.util.parser;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.Iterator;
025:
026: import junit.framework.TestSuite;
027:
028: import org.apache.commons.fileupload.FileItem;
029: import org.apache.commons.fileupload.FileItemFactory;
030: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
031: import org.apache.turbine.test.BaseTurbineTest;
032:
033: /**
034: * test whether the Default parameter parser returns its uploaded file items
035: * in the keySet().
036: *
037: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
038: * @version $Id: DefaultParameterParserTest.java 534527 2007-05-02 16:10:59Z tv $
039: */
040:
041: public class DefaultParameterParserTest extends BaseTurbineTest {
042: public DefaultParameterParserTest(String name) throws Exception {
043: super (name, "conf/test/TurbineResources.properties");
044: }
045:
046: public static TestSuite suite() {
047: return new TestSuite(DefaultParameterParserTest.class);
048: }
049:
050: public void testFileItemsInKeySet() {
051: ParameterParser pp = new DefaultParameterParser();
052: DiskFileItemFactory factory = new DiskFileItemFactory(10240,
053: null);
054:
055: assertEquals("keySet() is not empty!", 0, pp.keySet().size());
056:
057: FileItem test = factory.createItem("upload-field",
058: "application/octet-stream", false, null);
059: pp.add("upload-field", test);
060:
061: assertEquals("FileItem not found in keySet()!", 1, pp.keySet()
062: .size());
063:
064: Iterator it = pp.keySet().iterator();
065: assertTrue(it.hasNext());
066:
067: String name = (String) it.next();
068: assertEquals("Wrong name found", "upload-field", name);
069:
070: assertFalse(it.hasNext());
071:
072: pp.add("other-field", "foo");
073:
074: assertEquals("Wrong number of fields found ", 2,
075: pp.getKeys().length);
076:
077: assertTrue(pp.containsKey("upload-field"));
078: assertTrue(pp.containsKey("other-field"));
079:
080: assertFalse(pp.containsKey("missing-field"));
081: }
082:
083: public void testToString() throws IOException {
084: ParameterParser pp = new DefaultParameterParser();
085: DiskFileItemFactory factory = new DiskFileItemFactory(10240,
086: new File("."));
087:
088: FileItem test = factory.createItem("upload-field",
089: "application/octet-stream", false, null);
090:
091: // Necessary to avoid a NullPointerException in toString()
092: test.getOutputStream();
093:
094: pp.add("upload-field", test);
095:
096: assertTrue(pp.toString()
097: .startsWith("{upload-field=[name=null,"));
098: }
099:
100: /**
101: * This Test method checks the DefaultParameterParser which carries two Sets inside it.
102: * The suggested problem (TRB-10) was that pp.keySet() returns both Keys, but
103: * pp.getStrings("key") only checks for keys which are not FileItems. This makes no
104: * sense because a FileItem is not a String and would never be returned by
105: * getStrings() anyway. The test case is retained anyway since there are
106: * outstanding issues in this area with respect to 2.4.
107: *
108: * @throws Exception
109: */
110: public void testAddPathInfo() throws Exception {
111: ParameterParser pp = new DefaultParameterParser();
112: FileItemFactory factory = new DiskFileItemFactory(10240, null);
113:
114: assertEquals("keySet() is not empty!", 0, pp.keySet().size());
115:
116: FileItem test = factory.createItem("upload-field",
117: "application/octet-stream", false, null);
118: pp.add("upload-field", test);
119:
120: assertEquals("FileItem not found in keySet()!", 1, pp.keySet()
121: .size());
122:
123: Iterator it = pp.keySet().iterator();
124: assertTrue(it.hasNext());
125:
126: String name = (String) it.next();
127: assertEquals("Wrong name found", "upload-field", name);
128:
129: assertFalse(it.hasNext());
130:
131: pp.add("other-field", "foo");
132:
133: assertEquals("Wrong number of fields found ", 2,
134: pp.getKeys().length);
135:
136: assertTrue(pp.containsKey("upload-field"));
137: assertTrue(pp.containsKey("other-field"));
138:
139: assertNull(
140: "The returned should be null because a FileItem is not a String",
141: pp.getStrings("upload-field"));
142: assertFalse(pp.containsKey("missing-field"));
143: }
144: }
|