1   package org.apache.fulcrum.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  
23  import java.util.Iterator;
24  
25  import org.apache.avalon.framework.component.ComponentException;
26  import org.apache.commons.fileupload.FileItem;
27  import org.apache.commons.fileupload.FileItemFactory;
28  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
29  import org.apache.fulcrum.testcontainer.BaseUnitTest;
30  /**
31   * Basic test that ParameterParser instantiates.
32   *
33   * @author <a href="epugh@opensourceconnections.com">Eric Pugh</a>
34   * @version $Id: ParameterParserTest.java 645892 2008-04-08 13:04:40Z tv $
35   */
36  public class ParameterParserTest extends BaseUnitTest
37  {
38      private ParameterParser parameterParser = null;
39  
40      private ParserService parserService;
41  
42      /**
43       * Defines the testcase name for JUnit.
44       *
45       * @param name the testcase's name.
46       */
47      public ParameterParserTest(String name)
48      {
49          super(name);
50      }
51  
52      protected void setUp() throws Exception
53      {
54          super.setUp();
55          try
56          {
57              parserService = (ParserService)this.lookup(ParserService.ROLE);
58              parameterParser = (ParameterParser) parserService.getParser(DefaultParameterParser.class);
59          }
60          catch (ComponentException e)
61          {
62              e.printStackTrace();
63              fail(e.getMessage());
64          }
65      }
66  
67      public void testConfiguredUrlCaseFolding() throws Exception
68      {
69          assertTrue(parameterParser.getUrlFolding() == ParserService.URL_CASE_FOLDING_NONE);
70      }
71  
72      /**
73       * Simple test to verify that URL Case Folding works properly
74       *
75       * @throws Exception
76       */
77      public void testRepositoryExists() throws Exception
78      {
79          assertEquals("TRIMMED_and_Not_Modified",parameterParser.convertAndTrim(" TRIMMED_and_Not_Modified ", ParserService.URL_CASE_FOLDING_NONE));
80          assertEquals("trimmed_and_lower_case",parameterParser.convertAndTrim(" TRIMMED_and_Lower_Case ", ParserService.URL_CASE_FOLDING_LOWER));
81          assertEquals("TRIMMED_AND_UPPER_CASE",parameterParser.convertAndTrim(" TRIMMED_and_Upper_Case ", ParserService.URL_CASE_FOLDING_UPPER));
82      }
83  
84      /**
85       * This Test method checks the DefaultParameterParser which carries two Sets inside it.
86       * The suggested problem was that pp.keySet() returns both Keys, but pp.getStrings("key")
87       * only checks for keys which are not FileItems.
88       *
89       * @throws Exception
90       */
91      public void testAddPathInfo() throws Exception
92      {
93          FileItemFactory factory = new DiskFileItemFactory(10240, null);
94  
95          assertEquals("keySet() is not empty!", 0, parameterParser.keySet().size());
96  
97          FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
98          // Push this into the parser using DefaultParameterParser's add() method.
99          ((DefaultParameterParser) parameterParser).add("upload-field", test);
100 
101         assertEquals("FileItem not found in keySet()!", 1, parameterParser.keySet().size());
102 
103         Iterator it = parameterParser.keySet().iterator();
104         assertTrue(it.hasNext());
105 
106         String name = (String) it.next();
107         assertEquals("Wrong name found", "upload-field", name);
108 
109         assertFalse(it.hasNext());
110 
111         parameterParser.add("other-field", "foo");
112 
113         assertEquals("Wrong number of fields found ", 2, parameterParser.getKeys().length);
114 
115         assertTrue(parameterParser.containsKey("upload-field"));
116         assertTrue(parameterParser.containsKey("other-field"));
117 
118         // The following will actually cause a ClassCastException because getStrings() (and others) are not catering for FileItems.
119         assertNull("The returned should be null because a FileItem is not a String", parameterParser.getStrings("upload-field"));
120         assertFalse(parameterParser.containsKey("missing-field"));
121     }
122 }