View Javadoc

1   package org.apache.velocity.tools.generic;
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 org.junit.*;
23  import static org.junit.Assert.*;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import org.apache.velocity.tools.generic.Alternator;
29  
30  /**
31   * <p>Tests for AlternatorTool</p>
32   *
33   * @author Nathan Bubna
34   * @since VelocityTools 2.0
35   * @version $Id$
36   */
37  public class AlternatorToolTests {
38  
39      public @Test void ctorAlternatorTool() throws Exception
40      {
41          try
42          {
43              new AlternatorTool();
44          }
45          catch (Exception e)
46          {
47              fail("Constructor 'AlternatorTool()' failed due to: " + e);
48          }
49      }
50  
51      public @Test void methodConfigure_Map() throws Exception
52      {
53          AlternatorTool tool = new AlternatorTool();
54          Map<String,Object> conf = new HashMap<String,Object>();
55          conf.put(AlternatorTool.AUTO_ALTERNATE_DEFAULT_KEY, false);
56          tool.configure(conf);
57          assertFalse(tool.getAutoAlternateDefault());
58      }
59  
60      public @Test void methodsGetSetAutoAlternateDefault() throws Exception
61      {
62          AlternatorTool tool = new AlternatorTool();
63          assertTrue(tool.getAutoAlternateDefault());
64          tool.setAutoAlternateDefault(false);
65          assertFalse(tool.getAutoAlternateDefault());
66          tool.setAutoAlternateDefault(true);
67          assertTrue(tool.getAutoAlternateDefault());
68      }
69  
70      public @Test void methodMake_ObjectVarArgs() throws Exception
71      {
72          AlternatorTool tool = new AlternatorTool();
73          assertNull(tool.make());
74          Alternator result = tool.make(new Object[] { true });
75          assertTrue((Boolean)result.getNext());
76          assertTrue((Boolean)result.getNext());
77          result = tool.make("hi", true, false, 0);
78          assertEquals("hi", result.getNext());
79          assertTrue((Boolean)result.getNext());
80          assertFalse((Boolean)result.getNext());
81          assertEquals(0, result.getNext());
82          assertEquals("hi", result.getNext());
83          result = tool.make(new Object[] { "red", "blue" });
84          result.shift();
85          assertEquals("blue", result.toString());
86      }
87  
88      public @Test void methodMake_booleanObjectVarArgs() throws Exception
89      {
90          AlternatorTool tool = new AlternatorTool();
91          Alternator result = tool.make(false, new Object[] { "red", "blue" });
92          assertEquals("red", result.toString());
93          assertEquals("red", result.toString());
94          result.shift();
95          assertEquals("blue", result.toString());
96      }
97  
98      public @Test void methodAuto_ObjectVarArgs() throws Exception
99      {
100         AlternatorTool tool = new AlternatorTool();
101         Alternator result = tool.auto(-1,0,null,1);
102         assertEquals("-1", result.toString());
103         assertEquals(0, result.getCurrent());
104         assertEquals("0", result.toString());
105         assertEquals(null, result.toString());
106         assertEquals("1", result.toString());
107         assertEquals("-1", result.toString());
108     }
109 
110     public @Test void methodManual_ObjectVarArgs() throws Exception
111     {
112         AlternatorTool tool = new AlternatorTool();
113         Alternator result = tool.manual(new Object[] { true, false });
114         assertTrue((Boolean)result.getCurrent());
115         assertEquals("true", result.toString());
116         assertTrue((Boolean)result.getNext());
117         assertEquals("false", result.toString());
118         assertFalse((Boolean)result.getNext());
119         assertEquals("true", result.toString());
120     }
121 
122 }
123