/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.test.aspects.remoting;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.jboss.aop.advice.Interceptor;
import org.jboss.aspects.remoting.ClusterChooserInterceptor;
import org.jboss.aspects.remoting.ClusterConstants;
import org.jboss.aspects.remoting.FamilyWrapper;
import org.jboss.aspects.remoting.InvokeRemoteInterceptor;
import org.jboss.ha.client.loadbalance.LoadBalancePolicy;
import org.jboss.remoting.InvokerLocator;
/**
* @author Brian Stansberry
*
*/
public class ClusterChooserInterceptorUnitTestCase extends TestCase
{
private static int testCount;
/**
* Create a new ClusterChooserInterceptorUnitTestCase.
*
* @param name
*/
public ClusterChooserInterceptorUnitTestCase(String name)
{
super(name);
}
public void testUseAopLoadBalancePolicy() throws Throwable
{
testCount++;
MockAopLoadBalancePolicy lb = new MockAopLoadBalancePolicy();
FamilyWrapper fw = createFamilyWrapper("test" + testCount, 1);
MockInvocation invocation = setupInvocation(lb, fw);
MockNextInterceptor next = (MockNextInterceptor) invocation.getNextInterceptor();
next.setReturnValue("a");
assertEquals("a", invocation.invokeNext());
// This assertion is the real thing this method is testing
assertEquals(1, lb.getOverloadedInvocationCount());
assertEquals(getInvokerLocator(0), next.getInvocationHistory().get(0).getMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR));
}
private MockInvocation setupInvocation(LoadBalancePolicy lb, FamilyWrapper fw)
{
ClusterChooserInterceptor cci = new ClusterChooserInterceptor();
MockNextInterceptor next = new MockNextInterceptor();
MockInvocation mi = new MockInvocation(new Interceptor[]{cci, next});
mi.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.LOADBALANCE_POLICY, lb);
mi.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.CLUSTER_FAMILY_WRAPPER, fw);
return mi;
}
private FamilyWrapper createFamilyWrapper(String familyName, int numTargets) throws Exception
{
List<InvokerLocator> targets = new ArrayList<InvokerLocator>(numTargets);
for (int i = 0; i < numTargets; i++)
targets.add(getInvokerLocator(i));
return new FamilyWrapper(familyName, targets);
}
private static InvokerLocator getInvokerLocator(int targetNum) throws MalformedURLException
{
return new InvokerLocator("http://host" + String.valueOf(targetNum));
}
}