/**
* EasyBeans
* Copyright (C) 2006 Bull S.A.S.
* Contact: easybeans@ow2.org
*
* This library 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 any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id:EasyBeansManagedRuntime.java 1477 2007-06-16 16:50:19Z benoitf $
* --------------------------------------------------------------------------
*/
package org.ow2.easybeans.assemblies.tomcat.test;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import net.sourceforge.jwebunit.junit.WebTester;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Test the Tomcat assembly.
* @author Florent Benoit
*/
public class TomcatTest {
/**
* Tester object.
*/
private WebTester webTester;
/**
* MBean server connection.
*/
private MBeanServerConnection mBeanServerConnection = null;
/**
* Initialize the test framework.
* @throws Exception if the framework is not initalized.
*/
@BeforeClass
protected void init() throws Exception {
// Build tester object
this.webTester = new WebTester();
// Get the http port property
String httpPort = System.getProperty("http.port");
if (httpPort == null) {
throw new Exception("Cannot get the http.port system property");
}
// Get the rmi port property
String rmiPort = System.getProperty("rmi.port");
if (rmiPort == null) {
throw new Exception("Cannot get the rmi.port system property");
}
// Connect to the JMX server
String url = "service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/EasyBeansConnector";
JMXServiceURL jmxServiceURL = new JMXServiceURL(url);
JMXConnector connector = JMXConnectorFactory.connect(jmxServiceURL);
this.mBeanServerConnection = connector.getMBeanServerConnection();
this.webTester.getTestContext().setBaseUrl("http://localhost:" + httpPort + "/");
}
/**
* Check that the EasyBeans war is available (and then deployed).
* @throws Exception if EasyBeans is not available
*/
@Test
public void easyBeansAvailable() throws Exception {
this.webTester.beginAt("/ow2-easybeans");
Assert.assertTrue(this.webTester.getPageSource().contains("EasyBeans"));
}
/**
* Test if the EAR example is working.
*/
@Test(dependsOnMethods = "easyBeansAvailable")
public void testEarWebSample() {
this.webTester.beginAt("/ear-web");
// Get content
String pageContent = this.webTester.getPageSource();
// Check if authors Victor Hugo & Balzac are present
if (!pageContent.contains("Honore de Balzac") || !pageContent.contains("Victor Hugo")) {
Assert.fail("Page '" + pageContent + "' does not contain the authors !");
}
// Some books available too ?
if (!pageContent.contains("Les Chouans") || !pageContent.contains("Les Miserables")) {
Assert.fail("Page '" + pageContent + "' does not contain the books !");
}
}
/**
* Test if the maven EAR example is working.
*/
@Test(dependsOnMethods = "easyBeansAvailable")
public void testMavenEarWebSample() {
this.webTester.beginAt("/modules-webapp/sayHello");
// Get content
String pageContent = this.webTester.getPageSource();
// Check if Hello string is present
if (!pageContent.contains("Hello")) {
Assert.fail("Page '" + pageContent + "' does not contain hello !");
}
/*
if (!pageContent.contains("library")) {
Assert.fail("Page '" + pageContent + "' does not contain library !");
}
*/
}
/**
* Test that the statistics are working.
* @throws Exception if there are problems
*/
@Test(dependsOnMethods = "testEarWebSample")
public void testStatistics() throws Exception {
ObjectName oNameBean = new ObjectName(
"DefaultDomain:j2eeType=StatelessSessionBean,EJBModule=ejb3,J2EEServer=EasyBeans_0"
+ ",J2EEApplication=ear3,name=org.ow2.easybeans.examples.ear.StatelessBean");
long numberOfCalls = ((Long) this.mBeanServerConnection.getAttribute(oNameBean, "numberOfCalls")).longValue();
long numberOfCallsInit = ((Long) this.mBeanServerConnection.getAttribute(oNameBean, "numberOfCalls_init")).longValue();
long numberOfCallListOfAuthors = ((Long) this.mBeanServerConnection.getAttribute(oNameBean,
"numberOfCalls_listOfAuthors")).longValue();
ObjectName oNameModule = new ObjectName(
"DefaultDomain:j2eeType=EJBModule,J2EEServer=EasyBeans_0,J2EEApplication=ear3,name=ejb3");
long numberOfCallsGlobal = ((Long) this.mBeanServerConnection.getAttribute(oNameModule, "numberOfCalls")).longValue();
// Incremented !
Assert.assertTrue(numberOfCalls > 2);
Assert.assertEquals(numberOfCalls, numberOfCallsInit + numberOfCallListOfAuthors, "not the sum of the values"
+ numberOfCalls + "," + numberOfCallsInit + "," + numberOfCallListOfAuthors);
Assert.assertEquals(numberOfCalls, numberOfCallsGlobal, "not the same global value" + numberOfCalls + ","
+ numberOfCallsGlobal);
}
}