1   package org.apache.velocity.test;
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 junit.framework.TestCase;
23  
24  import org.apache.commons.lang.ArrayUtils;
25  import org.apache.velocity.runtime.parser.node.ASTMethod;
26  
27  /**
28   * Checks that the equals method works correctly when caching method keys.
29   *
30   * @author <a href="Will Glass-Husain">wglass@forio.com</a>
31   * @version $Id: MethodCacheKeyTestCase.java 463298 2006-10-12 16:10:32Z henning $
32   */
33  public class MethodCacheKeyTestCase extends TestCase 
34  {
35     
36      public void testMethodKeyCacheEquals()
37      {
38          Class [] elements1 = new Class [] { Object.class };
39          ASTMethod.MethodCacheKey mck1 = new ASTMethod.MethodCacheKey("test",elements1);
40          
41          selfEqualsAssertions(mck1);
42          
43          Class [] elements2 = new Class [] { Object.class };
44          ASTMethod.MethodCacheKey mck2 = new ASTMethod.MethodCacheKey("test",elements2);
45          
46          assertTrue(mck1.equals(mck2));
47          
48          Class [] elements3 = new Class [] { String.class };
49          ASTMethod.MethodCacheKey mck3 = new ASTMethod.MethodCacheKey("test",elements3);
50          
51          assertFalse(mck1.equals(mck3));
52          
53          Class [] elements4 = new Class [] { Object.class };
54          ASTMethod.MethodCacheKey mck4 = new ASTMethod.MethodCacheKey("boo",elements4);
55          
56          assertFalse(mck1.equals(mck4));
57          
58          /** check for potential NPE's **/
59          Class [] elements5 = ArrayUtils.EMPTY_CLASS_ARRAY;
60          ASTMethod.MethodCacheKey mck5 = new ASTMethod.MethodCacheKey("boo",elements5);
61          selfEqualsAssertions(mck5);
62          
63          Class [] elements6 = null;
64          ASTMethod.MethodCacheKey mck6 = new ASTMethod.MethodCacheKey("boo",elements6);
65          selfEqualsAssertions(mck6);
66          
67          Class [] elements7 = new Class [] {};
68          ASTMethod.MethodCacheKey mck7 = new ASTMethod.MethodCacheKey("boo",elements7);
69          selfEqualsAssertions(mck7);
70          
71          Class [] elements8 = new Class [] {null};
72          ASTMethod.MethodCacheKey mck8 = new ASTMethod.MethodCacheKey("boo",elements8);
73          selfEqualsAssertions(mck8);      
74          
75          Class [] elements9 = new Class [] { Object.class };
76          ASTMethod.MethodCacheKey mck9 = new ASTMethod.MethodCacheKey("boo",elements9);
77          selfEqualsAssertions(mck9);      
78          
79      }
80      
81      private void selfEqualsAssertions(ASTMethod.MethodCacheKey mck)
82      {
83          assertTrue(mck.equals(mck));
84          assertTrue(!mck.equals(null));
85          assertTrue(!mck.equals((ASTMethod.MethodCacheKey) null));
86      }
87      
88     
89  }