All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.identityconnectors.dbcommon.ExpectProxy Maven / Gradle / Ivy

There is a newer version: 2.1.7
Show newest version
/*
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.     
 * 
 * The contents of this file are subject to the terms of the Common Development 
 * and Distribution License("CDDL") (the "License").  You may not use this file 
 * except in compliance with the License.
 * 
 * You can obtain a copy of the License at 
 * http://IdentityConnectors.dev.java.net/legal/license.txt
 * See the License for the specific language governing permissions and limitations 
 * under the License. 
 * 
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at identityconnectors/legal/license.txt.
 * If applicable, add the following below this CDDL Header, with the fields 
 * enclosed by brackets [] replaced by your own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 */
package org.identityconnectors.dbcommon;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * This is a Test helper class for testing expected method calls and return values of interfaces
 * 

Limitation:

First implementation supports just a method name checking

* * @version $Revision 1.0$ * @param Type of the interface for testing * @since 1.0 */ public class ExpectProxy implements InvocationHandler { private List methodNames = new ArrayList(); private List retVals = new ArrayList(); private int count = 0; /** * Program the expected function call * @param methodName the expected method name * @param retVal the expected return value or proxy * @return the proxy */ public ExpectProxy expectAndReturn( final String methodName, final Object retVal) { this.methodNames.add(methodName); this.retVals.add(retVal); return this; } /** * Program the expected method call * @param methodName the expected method name * @return the proxy */ public ExpectProxy expect(final String methodName) { this.methodNames.add(methodName); //retVals must have same number of values as methodNames this.retVals.add(null); return this; } /** * Program the expected method call * @param methodName the expected method name * @param throwEx the expected exception * @return the proxy */ public ExpectProxy expectAndThrow( final String methodName, final Throwable throwEx) { return this.expectAndReturn(methodName, throwEx); } /** * Test that all expected was called in the order * @return true/false all was called */ public boolean isDone() { return count == methodNames.size(); } /** * The InvocationHandler method */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String ext = ""; if (methodNames.size() > count) { final String mname = this.methodNames.get(count); ext = " The expected call no: " + (count + 1) + " was " + mname + "."; if (method.getName().equals(mname)) { final Object ret = retVals.get(count++); if (ret instanceof Throwable) { throw (Throwable) ret; } return ret; } } throw new AssertionError( "The call of method :" + method + " was not expected." + ext + " Please call expectAndReturn(methodName,retVal) to fix it"); } /** * Return the Proxy implementation of the Interface * @param clazz of the interface * @return the proxy */ @SuppressWarnings("unchecked") public T getProxy(Class clazz) { ClassLoader cl = getClass().getClassLoader(); Class intef[] = new Class[]{clazz}; return (T) Proxy.newProxyInstance(cl, intef, this); } }