/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt 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.aspects.remoting.test.proxy;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import junit.framework.TestCase;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;
/**
* Unit test for JBAS-4456.
*
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
* @version $Revision: 1.1 $
* <p>
* Copyright July 23, 2008
* </p>
*/
public class RemotingProxyFactoryTestCase extends TestCase
{
private static final String DEFAULT_SUFFIX_DEPLOYABLE_XML = "-beans.xml";
private static Logger log = Logger.getLogger(RemotingProxyFactoryTestCase.class);
private static boolean firstTime = true;
private TestBootstrap bootstrap;
public void setUp() throws Exception
{
bootstrap = new TestBootstrap();
if (firstTime)
{
firstTime = false;
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
Logger.getLogger("org.jboss.aspects.remoting").setLevel(Level.DEBUG);
Logger.getLogger("org.jboss.aspects.remoting.test.proxy").setLevel(Level.DEBUG);
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
PatternLayout layout = new PatternLayout(pattern);
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
Logger.getRootLogger().addAppender(consoleAppender);
}
URL url = getDeployableXmlUrl(getClass());
bootstrap.deploy(url);
}
@Override
public void tearDown()
{
if(bootstrap != null)
bootstrap.shutdown();
bootstrap = null;
}
/**
* Tests case in which RemotingProxyFactory is configured with jndiName property.
*/
public void testRemotingProxyWithJNDINameFromProperty() throws Throwable
{
log.info("entering " + getName());
doTestRemotingProxy("proxyTestJNDIproperty");
log.info(getName() + " PASSES");
}
/**
* Tests case in which RemotingProxyFactory is not configured with jndiName property.
* Instead, the proxy is bound into JNDI through the use of the
* org.jboss.aop.microcontainer.aspects.jndi.JndiBinding annotation.
*/
public void testRemotingProxyWithJNDINameFromAnnotation() throws Throwable
{
log.info("entering " + getName());
doTestRemotingProxy("proxyTestJNDIannotation");
log.info(getName() + " PASSES");
}
private void doTestRemotingProxy(String jndiName) throws Throwable
{
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = new InitialContext(env);
System.out.println("getting SampleTarget bound to: " + jndiName);
Object o = ctx.lookup(jndiName);
Class<?>[] interfaces = o.getClass().getInterfaces();
log.info("number of proxy interfaces: " + interfaces.length);
for (int i = 0; i < interfaces.length; i++)
{
log.info("proxy interface[" + i + "]: " + interfaces[i]);
}
SampleInterface1 t1 = (SampleInterface1) o;
System.out.println("got SampleInterface1");
SampleInterface2 t2 = (SampleInterface2) o;
System.out.println("got SampleInterface2");
Object r1 = t1.method1(new Integer(7));
log.info("first response: " + r1);
Object r2 = t2.method2("abc");
log.info("second response: " + r2);
assertTrue(r1 instanceof Integer);
int i1 = (Integer) r1;
assertEquals(((7 * 2) + 3) * 2 + 1, i1);
assertTrue(r2 instanceof String);
String s2 = (String) r2;
assertEquals("abc" + "abc" + "xyz" + "|" + "abc" + "abc" + "xyz", s2);
}
private URL getDeployableXmlUrl(Class<?> clazz) throws MalformedURLException
{
String name = clazz.getSimpleName().toString();
name = name + DEFAULT_SUFFIX_DEPLOYABLE_XML;
URL url = clazz.getResource(name);
return url;
}
static class TestBootstrap extends BasicBootstrap
{
private static Logger log = Logger.getLogger(TestBootstrap.class);
private BasicXMLDeployer deployer;
public TestBootstrap()
{
super();
log.debug("Starting " + this + "...");
run();
// Create and set an XML Deployer
deployer = new BasicXMLDeployer(getKernel());
log.info("Started: " + this);
}
public void deploy(URL url)
{
try
{
log.debug("Deploying " + url.toString() + "...");
deployer.deploy(url);
log.info("Deployed: " + url.toString());
}
catch (Throwable e)
{
throw new RuntimeException("Could not deploy " + url.toString(), e);
}
}
protected void shutdown()
{
deployer.shutdown();
log.info("Shutdown complete");
}
}
}