1   package org.apache.turbine.util.parser;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.Iterator;
25  
26  import junit.framework.TestSuite;
27  
28  import org.apache.commons.fileupload.FileItem;
29  import org.apache.commons.fileupload.FileItemFactory;
30  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
31  import org.apache.turbine.test.BaseTurbineTest;
32  
33  /***
34   * test whether the Default parameter parser returns its uploaded file items
35   * in the keySet().
36   *
37   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38   * @version $Id: DefaultParameterParserTest.java 534527 2007-05-02 16:10:59Z tv $
39   */
40  
41  public class DefaultParameterParserTest
42          extends BaseTurbineTest
43  {
44      public DefaultParameterParserTest(String name)
45              throws Exception
46      {
47          super(name, "conf/test/TurbineResources.properties");
48      }
49  
50      public static TestSuite suite()
51      {
52          return new TestSuite(DefaultParameterParserTest.class);
53      }
54  
55      public void testFileItemsInKeySet()
56      {
57          ParameterParser pp = new DefaultParameterParser();
58          DiskFileItemFactory factory = new DiskFileItemFactory(10240, null);
59  
60          assertEquals("keySet() is not empty!", 0, pp.keySet().size());
61  
62          FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
63          pp.add("upload-field", test);
64  
65          assertEquals("FileItem not found in keySet()!", 1, pp.keySet().size());
66  
67          Iterator it = pp.keySet().iterator();
68          assertTrue(it.hasNext());
69  
70          String name = (String) it.next();
71          assertEquals("Wrong name found", "upload-field", name);
72  
73          assertFalse(it.hasNext());
74  
75          pp.add("other-field", "foo");
76  
77          assertEquals("Wrong number of fields found ", 2, pp.getKeys().length);
78  
79          assertTrue(pp.containsKey("upload-field"));
80          assertTrue(pp.containsKey("other-field"));
81  
82          assertFalse(pp.containsKey("missing-field"));
83      }
84  
85      public void testToString() throws IOException
86      {
87          ParameterParser pp = new DefaultParameterParser();
88          DiskFileItemFactory factory = new DiskFileItemFactory(10240, new File("."));
89  
90          FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
91  
92          // Necessary to avoid a NullPointerException in toString()
93          test.getOutputStream();
94  
95          pp.add("upload-field", test);
96  
97          assertTrue(pp.toString().startsWith("{upload-field=[name=null,"));
98      }
99  
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     {
112         ParameterParser pp = new DefaultParameterParser();
113         FileItemFactory factory = new DiskFileItemFactory(10240, null);
114 
115         assertEquals("keySet() is not empty!", 0, pp.keySet().size());
116 
117         FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
118         pp.add("upload-field", test);
119 
120         assertEquals("FileItem not found in keySet()!", 1, pp.keySet().size());
121 
122         Iterator it = pp.keySet().iterator();
123         assertTrue(it.hasNext());
124 
125         String name = (String) it.next();
126         assertEquals("Wrong name found", "upload-field", name);
127 
128         assertFalse(it.hasNext());
129 
130         pp.add("other-field", "foo");
131 
132         assertEquals("Wrong number of fields found ", 2, pp.getKeys().length);
133 
134         assertTrue(pp.containsKey("upload-field"));
135         assertTrue(pp.containsKey("other-field"));
136 
137         assertNull("The returned should be null because a FileItem is not a String", pp.getStrings("upload-field"));
138         assertFalse(pp.containsKey("missing-field"));
139     }
140 }
141