1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.configuration; 6 7 import java.io.Serializable; 8 import org.mockito.configuration.AnnotationEngine; 9 import org.mockito.configuration.DefaultMockitoConfiguration; 10 import org.mockito.configuration.IMockitoConfiguration; 11 import org.mockito.internal.configuration.plugins.Plugins; 12 import org.mockito.stubbing.Answer; 13 14 /** 15 * Thread-safe wrapper on user-defined org.mockito.configuration.MockitoConfiguration implementation 16 */ 17 public class GlobalConfiguration implements IMockitoConfiguration, Serializable { 18 private static final long serialVersionUID = -2860353062105505938L; 19 20 private static final ThreadLocal<IMockitoConfiguration> GLOBAL_CONFIGURATION = new ThreadLocal<IMockitoConfiguration>(); 21 22 //back door for testing getIt()23 IMockitoConfiguration getIt() { 24 return GLOBAL_CONFIGURATION.get(); 25 } 26 GlobalConfiguration()27 public GlobalConfiguration() { 28 //Configuration should be loaded only once but I cannot really test it 29 if (GLOBAL_CONFIGURATION.get() == null) { 30 GLOBAL_CONFIGURATION.set(createConfig()); 31 } 32 } 33 createConfig()34 private IMockitoConfiguration createConfig() { 35 IMockitoConfiguration defaultConfiguration = new DefaultMockitoConfiguration(); 36 IMockitoConfiguration config = new ClassPathLoader().loadConfiguration(); 37 if (config != null) { 38 return config; 39 } else { 40 return defaultConfiguration; 41 } 42 } 43 validate()44 public static void validate() { 45 new GlobalConfiguration(); 46 } 47 getAnnotationEngine()48 public AnnotationEngine getAnnotationEngine() { 49 return GLOBAL_CONFIGURATION.get().getAnnotationEngine(); 50 } 51 tryGetPluginAnnotationEngine()52 public org.mockito.plugins.AnnotationEngine tryGetPluginAnnotationEngine() { 53 IMockitoConfiguration configuration = GLOBAL_CONFIGURATION.get(); 54 if (configuration.getClass() == DefaultMockitoConfiguration.class) { 55 return Plugins.getAnnotationEngine(); 56 } 57 return configuration.getAnnotationEngine(); 58 } 59 60 61 cleansStackTrace()62 public boolean cleansStackTrace() { 63 return GLOBAL_CONFIGURATION.get().cleansStackTrace(); 64 } 65 enableClassCache()66 public boolean enableClassCache() { 67 return GLOBAL_CONFIGURATION.get().enableClassCache(); 68 } 69 getDefaultAnswer()70 public Answer<Object> getDefaultAnswer() { 71 return GLOBAL_CONFIGURATION.get().getDefaultAnswer(); 72 } 73 } 74