View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.util;
21  
22  import org.junit.Test;
23  
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  
27  import static org.hamcrest.Matchers.equalTo;
28  import static org.hamcrest.Matchers.notNullValue;
29  import static org.hamcrest.Matchers.sameInstance;
30  import static org.junit.Assert.assertThat;
31  import static org.junit.Assert.fail;
32  import static org.junit.Assume.assumeThat;
33  
34  /**
35   * Tests for the class loader utilities.
36   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
37   */
38  public class ClassLoadersTest {
39      @Test
40      public void testPushContextClassLoader() throws Exception {
41          Thread th = Thread.currentThread();
42          ClassLoader orig = th.getContextClassLoader();
43          ClassLoader loader = new URLClassLoader(new URL[0], ClassLoaders.inferDefault(ClassLoadersTest.class));
44          ClassLoaderContext ctx = ClassLoaders.pushContext(loader);
45          try {
46              assertThat(th.getContextClassLoader(), sameInstance(loader));
47          } finally {
48              ctx.pop();
49          }
50          assertThat(th.getContextClassLoader(), sameInstance(orig));
51          try {
52              ctx.pop();
53              fail("re-popping context should be illegal state");
54          } catch (IllegalStateException e) {
55              /* expected */
56          }
57      }
58  
59      @Test
60      public void testInferLoaderFromRefClass() throws Exception {
61          Thread th = Thread.currentThread();
62          ClassLoader orig = th.getContextClassLoader();
63          assumeThat("have a context class loader",
64                     orig, notNullValue());
65          ClassLoaderContext ctx = ClassLoaders.pushContext(null);
66          try {
67              assertThat(ClassLoaders.inferDefault(ClassLoadersTest.class),
68                         equalTo(getClass().getClassLoader()));
69          } finally {
70              ctx.pop();
71          }
72      }
73  
74      @Test
75      public void testInferLoaderFromThread() throws Exception {
76          Thread th = Thread.currentThread();
77          ClassLoader orig = th.getContextClassLoader();
78          assumeThat("have a context class loader",
79                     orig, notNullValue());
80          assertThat(ClassLoaders.inferDefault(ClassLoadersTest.class),
81                     equalTo(orig));
82      }
83  }