KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > TypeLiteralTest


1 /**
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.google.inject;
18
19 import junit.framework.TestCase;
20
21 import java.util.List JavaDoc;
22 import java.lang.reflect.Type JavaDoc;
23
24 /**
25  * @author crazybob@google.com (Bob Lee)
26  */

27 public class TypeLiteralTest extends TestCase {
28
29   public void testWithParameterizedTypeImpl() {
30     TypeLiteral<List JavaDoc<String JavaDoc>> a = new TypeLiteral<List JavaDoc<String JavaDoc>>() {};
31     TypeLiteral<List JavaDoc<String JavaDoc>> b = new TypeLiteral<List JavaDoc<String JavaDoc>>(
32         new TypeWithArgument(List JavaDoc.class, String JavaDoc.class)) {};
33     assertEquals(a, b);
34   }
35
36   public void testEquality() {
37     TypeLiteral<List JavaDoc<String JavaDoc>> t1 = new TypeLiteral<List JavaDoc<String JavaDoc>>() {};
38     TypeLiteral<List JavaDoc<String JavaDoc>> t2 = new TypeLiteral<List JavaDoc<String JavaDoc>>() {};
39     TypeLiteral<List JavaDoc<Integer JavaDoc>> t3 = new TypeLiteral<List JavaDoc<Integer JavaDoc>>() {};
40     TypeLiteral<String JavaDoc> t4 = new TypeLiteral<String JavaDoc>() {};
41
42     assertEquals(t1, t2);
43     assertEquals(t2, t1);
44
45     assertFalse(t2.equals(t3));
46     assertFalse(t3.equals(t2));
47
48     assertFalse(t2.equals(t4));
49     assertFalse(t4.equals(t2));
50
51     TypeLiteral<String JavaDoc> t5 = TypeLiteral.get(String JavaDoc.class);
52     assertEquals(t4, t5);
53   }
54
55   public void testMissingTypeParameter() {
56     try {
57       new TypeLiteral() {};
58       fail();
59     } catch (RuntimeException JavaDoc e) { /* expected */ }
60   }
61
62   public void testTypesInvolvingArraysForEquality() {
63     TypeLiteral<String JavaDoc[]> stringArray = new TypeLiteral<String JavaDoc[]>() {};
64     assertEquals(stringArray, new TypeLiteral<String JavaDoc[]>() {});
65
66     TypeLiteral<List JavaDoc<String JavaDoc[]>> listOfStringArray
67         = new TypeLiteral<List JavaDoc<String JavaDoc[]>>() {};
68     assertEquals(listOfStringArray, new TypeLiteral<List JavaDoc<String JavaDoc[]>>() {});
69   }
70 }
71
Popular Tags