Home | History | Annotate | Download | only in configuration
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockitousage.configuration;
      6 
      7 
      8 import org.junit.Test;
      9 import org.mockito.internal.configuration.ConfigurationAccess;
     10 import org.mockitoutil.SimplePerRealmReloadingClassLoader;
     11 
     12 import java.util.concurrent.Callable;
     13 
     14 import static org.mockito.Mockito.mock;
     15 
     16 public class ClassCacheVersusClassReloadingTest {
     17     // TODO refactor to use ClassLoaders
     18 
     19     private SimplePerRealmReloadingClassLoader testMethodClassLoaderRealm = new SimplePerRealmReloadingClassLoader(reloadMockito());
     20 
     21     @Test
     22     public void should_not_throw_ClassCastException_when_objenesis_cache_disabled() throws Exception {
     23         prepareMockitoAndDisableObjenesisCache();
     24 
     25         doInNewChildRealm(testMethodClassLoaderRealm, "org.mockitousage.configuration.ClassCacheVersusClassReloadingTest$DoTheMocking");
     26         doInNewChildRealm(testMethodClassLoaderRealm, "org.mockitousage.configuration.ClassCacheVersusClassReloadingTest$DoTheMocking");
     27     }
     28 
     29     public static class DoTheMocking implements Callable<Object> {
     30         public Object call() throws Exception {
     31             Class<?> clazz = this.getClass().getClassLoader().loadClass("org.mockitousage.configuration.ClassToBeMocked");
     32             return mock(clazz);
     33         }
     34     }
     35 
     36     private static void doInNewChildRealm(ClassLoader parentRealm, String callableCalledInClassLoaderRealm) throws Exception {
     37         new SimplePerRealmReloadingClassLoader(parentRealm, reloadScope()).doInRealm(callableCalledInClassLoaderRealm);
     38     }
     39 
     40     private static SimplePerRealmReloadingClassLoader.ReloadClassPredicate reloadScope() {
     41         return new SimplePerRealmReloadingClassLoader.ReloadClassPredicate() {
     42             public boolean acceptReloadOf(String qualifiedName) {
     43                 return "org.mockitousage.configuration.ClassCacheVersusClassReloadingTest$DoTheMocking".equals(qualifiedName)
     44                     || "org.mockitousage.configuration.ClassToBeMocked".equals(qualifiedName);
     45             }
     46         };
     47     }
     48 
     49     private void prepareMockitoAndDisableObjenesisCache() throws Exception {
     50         testMethodClassLoaderRealm.doInRealm("org.mockitousage.configuration.ClassCacheVersusClassReloadingTest$PrepareMockito");
     51     }
     52 
     53     public static class PrepareMockito implements Callable<Boolean> {
     54         public Boolean call() throws Exception {
     55             Class.forName("org.mockito.Mockito");
     56             ConfigurationAccess.getConfig().overrideEnableClassCache(false);
     57             return Boolean.TRUE;
     58         }
     59     }
     60 
     61     private static SimplePerRealmReloadingClassLoader.ReloadClassPredicate reloadMockito() {
     62         return new SimplePerRealmReloadingClassLoader.ReloadClassPredicate() {
     63             public boolean acceptReloadOf(String qualifiedName) {
     64                 return (!qualifiedName.contains("net.bytebuddy") && qualifiedName.contains("org.mockito"));
     65             }
     66         };
     67     }
     68 
     69 }
     70