2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.shale.clay.convert;
18  
19  import javax.faces.component.UIComponent;
20  import javax.faces.component.html.HtmlInputText;
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.shale.test.base.AbstractViewControllerTestCase;
26  import org.apache.shale.util.ConverterHelper;
27  
28  public class StringArrayConverterTestCase extends
29          AbstractViewControllerTestCase {
30  
31      // Construct a new instance of this test case.
32      public StringArrayConverterTestCase(String name) {
33          super(name);
34      }
35  
36      // Return the tests included in this test case.
37      public static Test suite() {
38          return (new TestSuite(StringArrayConverterTestCase.class));
39      }
40      
41      protected void setUp() throws Exception {
42          super.setUp();
43          Class clazz = Class.forName("[Ljava.lang.String;");
44          
45          facesContext.getApplication().addConverter(clazz,
46                   "org.apache.shale.clay.convert.StringArrayConverter");
47      }
48  
49      // test how Clay would use it to convert a string property into a target String[]
50      public void testDefaults() {
51          ConverterHelper helper = new ConverterHelper();
52          String[] partialTriggers = {"one", "two", "three"};
53          String asString = helper.asString(facesContext, String[].class, partialTriggers);
54          
55          assertNotNull(asString);
56          assertEquals("one two three", asString);
57          
58          String[] asObject = (String[]) helper.asObject(facesContext, String[].class, asString);
59          assertNotNull(asObject);
60          
61          assertEquals(partialTriggers .length, asObject.length);
62          for (int i = 0; i < partialTriggers .length; i++) {
63              assertEquals("item " + i + ":", partialTriggers[i], asObject[i]);
64          }
65          
66      }
67      
68      public void testCustom() {
69          StringArrayConverter converter = (StringArrayConverter) facesContext.getApplication()
70                                                        .createConverter(String[].class);
71          converter.setDelimiter(new Character('"'));
72          converter.setSeparator(new Character(','));
73          
74          UIComponent dummy = new HtmlInputText();
75          
76          String[] partialTriggers  = {"one", "two", "three"};
77          String asString = converter.getAsString(facesContext, dummy, partialTriggers);
78          
79          assertNotNull(asString);
80          assertEquals("\"one\",\"two\",\"three\"", asString);
81          
82          String[] asObject = (String[]) converter.getAsObject(facesContext, dummy, asString);
83          assertNotNull(asObject);
84          
85          assertEquals(partialTriggers.length, asObject.length);
86          for (int i = 0; i < partialTriggers.length; i++) {
87              assertEquals("item " + i + ":", partialTriggers[i], asObject[i]);
88          }
89  
90      }
91  
92  }