1   package org.apache.turbine.util.parser;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Iterator;
20  
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.fileupload.DefaultFileItemFactory;
24  import org.apache.commons.fileupload.FileItem;
25  import org.apache.turbine.test.BaseTurbineTest;
26  
27  /***
28   * test whether the Default parameter parser returns its uploaded file items
29   * in the keySet().
30   *
31   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32   * @version $Id: DefaultParameterParserTest.java 293298 2005-10-03 10:50:07Z henning $
33   */
34  
35  public class DefaultParameterParserTest
36          extends BaseTurbineTest
37  {
38      public DefaultParameterParserTest(String name)
39              throws Exception
40      {
41          super(name, "conf/test/TurbineResources.properties");
42      }
43  
44      public static TestSuite suite()
45      {
46          return new TestSuite(DefaultParameterParserTest.class);
47      }
48  
49      public void testFileItemsInKeySet()
50      {
51          ParameterParser pp = new DefaultParameterParser();
52          DefaultFileItemFactory factory = new DefaultFileItemFactory(10240, null);
53  
54          assertEquals("keySet() is not empty!", 0, pp.keySet().size());
55  
56          FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
57          pp.add("upload-field", test);
58  
59          assertEquals("FileItem not found in keySet()!", 1, pp.keySet().size());
60  
61          Iterator it = pp.keySet().iterator();
62          assertTrue(it.hasNext());
63  
64          String name = (String) it.next();
65          assertEquals("Wrong name found", "upload-field", name);
66  
67          assertFalse(it.hasNext());
68  
69          pp.add("other-field", "foo");
70  
71          assertEquals("Wrong number of fields found ", 2, pp.getKeys().length);
72  
73          assertTrue(pp.containsKey("upload-field"));
74          assertTrue(pp.containsKey("other-field"));
75  
76          assertFalse(pp.containsKey("missing-field"));
77      }
78  
79      public void testToString()
80      {
81          ParameterParser pp = new DefaultParameterParser();
82          DefaultFileItemFactory factory = new DefaultFileItemFactory(10240, null);
83  
84          FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
85          pp.add("upload-field", test);
86  
87          assertTrue(pp.toString().startsWith("{upload-field=[org.apache.commons.fileupload.DefaultFileItem"));
88      }
89  }
90