source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/util/TestReflectionUtils.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: 4.2 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.net.URL;
22import java.net.URLClassLoader;
23import java.util.HashMap;
24
25import org.apache.hadoop.conf.Configuration;
26import org.apache.hadoop.mapred.JobConf;
27import org.apache.hadoop.mapred.JobConfigurable;
28
29import junit.framework.TestCase;
30
31public class TestReflectionUtils extends TestCase {
32
33  private static Class toConstruct[] = { String.class, TestReflectionUtils.class, HashMap.class };
34  private Throwable failure = null;
35
36  public void setUp() {
37    ReflectionUtils.clearCache();
38  }
39   
40  public void testCache() throws Exception {
41    assertEquals(0, cacheSize());
42    doTestCache();
43    assertEquals(toConstruct.length, cacheSize());
44    ReflectionUtils.clearCache();
45    assertEquals(0, cacheSize());
46  }
47   
48   
49  @SuppressWarnings("unchecked")
50  private void doTestCache() {
51    for (int i=0; i<toConstruct.length; i++) {
52      Class cl = toConstruct[i];
53      Object x = ReflectionUtils.newInstance(cl, null);
54      Object y = ReflectionUtils.newInstance(cl, null);
55      assertEquals(cl, x.getClass());
56      assertEquals(cl, y.getClass());
57    }
58  }
59   
60  public void testThreadSafe() throws Exception {
61    Thread[] th = new Thread[32];
62    for (int i=0; i<th.length; i++) {
63      th[i] = new Thread() {
64          public void run() {
65            try {
66              doTestCache();
67            } catch (Throwable t) {
68              failure = t;
69            }
70          }
71        };
72      th[i].start();
73    }
74    for (int i=0; i<th.length; i++) {
75      th[i].join();
76    }
77    if (failure != null) {
78      failure.printStackTrace();
79      fail(failure.getMessage());
80    }
81  }
82   
83  private int cacheSize() throws Exception {
84    return ReflectionUtils.getCacheSize();
85  }
86   
87  public void testCantCreate() {
88    try {
89      ReflectionUtils.newInstance(NoDefaultCtor.class, null);
90      fail("invalid call should fail");
91    } catch (RuntimeException rte) {
92      assertEquals(NoSuchMethodException.class, rte.getCause().getClass());
93    }
94  }
95   
96  @SuppressWarnings("unchecked")
97  public void testCacheDoesntLeak() throws Exception {
98    int iterations=9999; // very fast, but a bit less reliable - bigger numbers force GC
99    for (int i=0; i<iterations; i++) {
100      URLClassLoader loader = new URLClassLoader(new URL[0], getClass().getClassLoader());
101      Class cl = Class.forName("org.apache.hadoop.util.TestReflectionUtils$LoadedInChild", false, loader);
102      Object o = ReflectionUtils.newInstance(cl, null);
103      assertEquals(cl, o.getClass());
104    }
105    System.gc();
106    assertTrue(cacheSize()+" too big", cacheSize()<iterations);
107  }
108   
109  private static class LoadedInChild {
110  }
111   
112  public static class NoDefaultCtor {
113    public NoDefaultCtor(int x) {}
114  }
115 
116  /**
117   * This is to test backward compatibility of ReflectionUtils for
118   * JobConfigurable objects.
119   * This should be made deprecated along with the mapred package HADOOP-1230.
120   * Should be removed when mapred package is removed.
121   */
122  public void testSetConf() {
123    JobConfigurableOb ob = new JobConfigurableOb();
124    ReflectionUtils.setConf(ob, new Configuration());
125    assertFalse(ob.configured);
126    ReflectionUtils.setConf(ob, new JobConf());
127    assertTrue(ob.configured);
128  }
129 
130  private static class JobConfigurableOb implements JobConfigurable {
131    boolean configured;
132    public void configure(JobConf job) {
133      configured = true;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.