source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/util/TestGenericsUtil.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.7 KB
Line 
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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.util;
20
21import java.util.ArrayList;
22import java.util.List;
23
24import junit.framework.TestCase;
25
26import org.apache.hadoop.conf.Configuration;
27
28public class TestGenericsUtil extends TestCase {
29
30  public void testToArray() {
31   
32    //test a list of size 10
33    List<Integer> list = new ArrayList<Integer>(); 
34   
35    for(int i=0; i<10; i++) {
36      list.add(i);
37    }
38   
39    Integer[] arr = GenericsUtil.toArray(list);
40   
41    for (int i = 0; i < arr.length; i++) {
42      assertEquals(list.get(i), arr[i]);
43    }
44  }
45 
46  public void testWithEmptyList() {
47    try {
48      List<String> list = new ArrayList<String>();
49      String[] arr = GenericsUtil.toArray(list);
50      fail("Empty array should throw exception");
51      System.out.println(arr); //use arr so that compiler will not complain
52     
53    }catch (IndexOutOfBoundsException ex) {
54      //test case is successful
55    }
56  }
57 
58  public void testWithEmptyList2() {
59    List<String> list = new ArrayList<String>();
60    //this method should not throw IndexOutOfBoundsException
61    String[] arr = GenericsUtil.<String>toArray(String.class, list);
62   
63    assertEquals(0, arr.length);
64  }
65 
66  /** This class uses generics */
67  private class GenericClass<T> {
68    T dummy;
69    List<T> list = new ArrayList<T>();
70   
71    void add(T item) {
72      list.add(item);
73    }
74   
75    T[] funcThatUsesToArray() {
76      T[] arr = GenericsUtil.toArray(list);
77      return arr;
78    }
79  }
80 
81  public void testWithGenericClass() {
82   
83    GenericClass<String> testSubject = new GenericClass<String>();
84   
85    testSubject.add("test1");
86    testSubject.add("test2");
87   
88    try {
89      //this cast would fail, if we had not used GenericsUtil.toArray, since the
90      //rmethod would return Object[] rather than String[]
91      String[] arr = testSubject.funcThatUsesToArray();
92     
93      assertEquals("test1", arr[0]);
94      assertEquals("test2", arr[1]);
95     
96    }catch (ClassCastException ex) {
97      fail("GenericsUtil#toArray() is not working for generic classes");
98    }
99   
100  }
101 
102  public void testGenericOptionsParser() throws Exception {
103     GenericOptionsParser parser = new GenericOptionsParser(
104        new Configuration(), new String[] {"-jt"});
105    assertEquals(parser.getRemainingArgs().length, 0);
106   
107    // test if -D accepts -Dx=y=z
108    parser = 
109      new GenericOptionsParser(new Configuration(), 
110                               new String[] {"-Dx=y=z"});
111    assertEquals(parser.getConfiguration().get("x"), "y=z");
112  }
113 
114  public void testGetClass() {
115   
116    //test with Integer
117    Integer x = new Integer(42); 
118    Class<Integer> c = GenericsUtil.getClass(x);
119    assertEquals(Integer.class, c);
120   
121    //test with GenericClass<Integer>
122    GenericClass<Integer> testSubject = new GenericClass<Integer>();
123    Class<GenericClass<Integer>> c2 = GenericsUtil.getClass(testSubject);
124    assertEquals(GenericClass.class, c2);
125  }
126 
127}
Note: See TracBrowser for help on using the repository browser.