View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.component;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.faces.validator.Validator;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  /**
31   * Unit tests for UIXSelectMany.
32   *
33   */
34  public class UIXSelectManyTest extends UIXEditableValueTestCase
35  {
36    /**
37     * Creates a new UIXSelectManyTest.
38     *
39     * @param testName  the unit test name
40     */
41    public UIXSelectManyTest(
42      String testName)
43    {
44      super(testName);
45    }
46    
47    @Override
48    protected void setUp() throws Exception
49    {
50      super.setUp();
51    }
52    
53    @Override
54    protected void tearDown() throws Exception
55    {
56      super.tearDown();
57    }
58    
59    public static Test suite()
60    {
61      return new TestSuite(UIXSelectManyTest.class);
62    }
63  
64    /**
65     * Test a few generic properties of the attribute Map.
66     * (These tests really should be at a higher level...)
67     */
68    @SuppressWarnings("unchecked")
69    public void testAttributeMap()
70    {
71      UIXSelectMany component = createSelectMany();
72      assertEquals(null, component.getValueBinding("validators"));
73      Map<String, Object> attributes = component.getAttributes();
74      Validator[] validators = (Validator[]) attributes.get("validators");
75      assertEquals(0, validators.length);
76  
77      component.addValidator(new javax.faces.validator.LengthValidator());
78      validators = (Validator[]) attributes.get("validators");
79      assertEquals(1, validators.length);
80      assertTrue(validators[0] instanceof javax.faces.validator.LengthValidator);
81    }
82  
83    public void testCompareValues()
84    {
85      UIXSelectMany component = createSelectMany();
86  
87      assertFalse(component.compareValues(null, null));
88      assertFalse(component.compareValues(null, new Object[0]));
89      assertFalse(component.compareValues(null, new ArrayList<String>()));
90      assertTrue(component.compareValues(null, new Object[1]));
91      assertFalse(component.compareValues(new int[]{1, 2}, new int[]{2, 1}));
92      assertTrue(component.compareValues(new int[]{1, 2, 3}, new int[]{2, 1}));
93  
94      ArrayList<String> one;
95      ArrayList<String> two;
96  
97      one = new ArrayList<String>();
98      one.add("foo");
99      one.add("bar");
100 
101     two = new ArrayList<String>();
102     two.add("bar");
103     two.add("foo");
104 
105     assertFalse(component.compareValues(one, two));
106     // Make sure compareValues didn't destroy either list
107     assertEquals(2, one.size());
108     assertEquals(2, two.size());
109 
110     one.add("baz");
111     assertTrue(component.compareValues(one, two));
112   }
113 
114   @Override
115   public void testProcessValidations()
116   {
117     String[] submittedValue = new String[] {"foo", "bar"};
118     List<String> convertedValue = Arrays.asList(submittedValue);
119 
120     doTestProcessValidations(createEditableValue(), submittedValue, convertedValue);
121   }
122 
123   @Override
124   protected UIXEditableValue createEditableValue()
125   {
126     return createSelectMany();
127   }
128 
129   protected UIXSelectMany createSelectMany()
130   {
131   return new UIXSelectMany();
132   }
133 
134 }