1925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey/* 2925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * Copyright (C) 2013 The Android Open Source Project 3925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * 4925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 5925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * you may not use this file except in compliance with the License. 6925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * You may obtain a copy of the License at 7925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * 8925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 9925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * 10925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * Unless required by applicable law or agreed to in writing, software 11925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 12925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * See the License for the specific language governing permissions and 14925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * limitations under the License. 15925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey */ 16925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey 17925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkeypackage com.android.providers.downloads; 18925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey 19925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkeyimport android.util.Log; 20925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey 21925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey/** 22925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * Helper for Mockito-based test cases. 23925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey */ 24925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkeypublic final class MockitoHelper { 25925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey private static final String TAG = "MockitoHelper"; 26925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey 27925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey private ClassLoader mOriginalClassLoader; 28925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey private Thread mContextThread; 29925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey 30925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey /** 31925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * Creates a new helper, which in turn will set the context classloader so 32925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * it can load Mockito resources. 33925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * 34925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * @param packageClass test case class 35925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey */ 36925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey public void setUp(Class<?> packageClass) throws Exception { 37925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey // makes a copy of the context classloader 38925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey mContextThread = Thread.currentThread(); 39925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey mOriginalClassLoader = mContextThread.getContextClassLoader(); 40925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey ClassLoader newClassLoader = packageClass.getClassLoader(); 41925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey Log.v(TAG, "Changing context classloader from " + mOriginalClassLoader 42925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey + " to " + newClassLoader); 43925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey mContextThread.setContextClassLoader(newClassLoader); 44925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey } 45925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey 46925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey /** 47925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey * Restores the context classloader to the previous value. 48925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey */ 49925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey public void tearDown() throws Exception { 50925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey Log.v(TAG, "Restoring context classloader to " + mOriginalClassLoader); 51925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey mContextThread.setContextClassLoader(mOriginalClassLoader); 52925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey } 53925976230936a5177365dc24b50da8607a9af8d4Jeff Sharkey} 54