[jboss-cvs] JBossAS SVN: r89211 - in projects/ejb-book/trunk/ch06-filetransfer: src/main/java/org/jboss/ejb3/examples/ch06/filetransfer and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed May 20 16:04:01 EDT 2009
Author: ALRubinger
Date: 2009-05-20 16:04:01 -0400 (Wed, 20 May 2009)
New Revision: 89211
Added:
projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/META-INF/
projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/META-INF/ejb-jar.xml
projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties
projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java
projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferTestCaseBase.java
projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties
Removed:
projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties
Modified:
projects/ejb-book/trunk/ch06-filetransfer/pom.xml
projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferBean.java
projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferCommonBusiness.java
projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferRemoteBusiness.java
projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java
projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferUnitTestCase.java
Log:
[EJBBOOK-8] Added SFSB tests to round out the examples
Modified: projects/ejb-book/trunk/ch06-filetransfer/pom.xml
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/pom.xml 2009-05-20 19:13:59 UTC (rev 89210)
+++ projects/ejb-book/trunk/ch06-filetransfer/pom.xml 2009-05-20 20:04:01 UTC (rev 89211)
@@ -186,6 +186,7 @@
${project.build.directory}/lib/ftpserver-core.jar,
${project.build.directory}/lib/mina-core.jar,
${project.build.directory}/lib/slf4j-api.jar,
+ ${project.build.directory}/lib/commons-net.jar,
${project.build.directory}/${project.build.finalName}.${project.packaging}</files>
<jboss.test.run>true</jboss.test.run>
</configuration>
Modified: projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferBean.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferBean.java 2009-05-20 19:13:59 UTC (rev 89210)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferBean.java 2009-05-20 20:04:01 UTC (rev 89211)
@@ -29,6 +29,7 @@
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Remote;
+import javax.ejb.Remove;
import javax.ejb.Stateful;
import org.apache.commons.net.ftp.FTPClient;
@@ -45,7 +46,7 @@
* @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
* @version $Revision: $
*/
- at Stateful
+ at Stateful(name = FileTransferBean.EJB_NAME)
@Remote(FileTransferRemoteBusiness.class)
public class FileTransferBean implements FileTransferRemoteBusiness
{
@@ -69,6 +70,11 @@
*/
private static final String ENV_ENTRY_NAME_CONNECT_PORT = "connectPort";
+ /**
+ * Name of the EJB, referenced from ejb-jar.xml and used in Global JNDI addresses
+ */
+ public static final String EJB_NAME = "FileTransferEJB";
+
//-------------------------------------------------------------------------------------||
// Instance Members -------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -334,6 +340,16 @@
}
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch06.filetransfer.FileTransferRemoteBusiness#endSession()
+ */
+ @Remove
+ @Override
+ public void endSession()
+ {
+ log.info("Session Ending...");
+ }
+
//-------------------------------------------------------------------------------------||
// Accessors / Mutators ---------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -343,6 +359,12 @@
*/
public String getConnectHost()
{
+ final String connectHost = this.connectHost;
+ if (connectHost == null || connectHost.length() == 0)
+ {
+ throw new IllegalStateException("Connect host must have been defined by env-entry name: "
+ + ENV_ENTRY_NAME_CONNECT_HOST);
+ }
return connectHost;
}
@@ -359,6 +381,12 @@
*/
public int getConnectPort()
{
+ final int connectPort = this.connectPort;
+ if (connectPort <= 0)
+ {
+ throw new IllegalStateException("Connect port must have been defined by env-entry name \""
+ + ENV_ENTRY_NAME_CONNECT_PORT + "\" and must be a positive integer");
+ }
return connectPort;
}
@@ -401,4 +429,5 @@
{
this.presentWorkingDirectory = presentWorkingDirectory;
}
+
}
Modified: projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferCommonBusiness.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferCommonBusiness.java 2009-05-20 19:13:59 UTC (rev 89210)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferCommonBusiness.java 2009-05-20 20:04:01 UTC (rev 89211)
@@ -27,7 +27,8 @@
* Contains the contract for operations common to all
* business interfaces of the FileTransferEJB.
*
- * Includes support for //TODO
+ * Includes support for switching present working directories,
+ * printing the current working directory, and making directories.
*
* @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
* @version $Revision: $
Modified: projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferRemoteBusiness.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferRemoteBusiness.java 2009-05-20 19:13:59 UTC (rev 89210)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferRemoteBusiness.java 2009-05-20 20:04:01 UTC (rev 89211)
@@ -21,15 +21,28 @@
*/
package org.jboss.ejb3.examples.ch06.filetransfer;
+import javax.ejb.Remove;
+
/**
* FileTransferRemoteBusiness
*
- * Remote Business interface for the FileTransferEJB
+ * Remote Business interface for the FileTransferEJB.
+ * Because this will only be used in EJB environments, we define
+ * a method to end the current session.
*
* @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
* @version $Revision: $
*/
public interface FileTransferRemoteBusiness extends FileTransferCommonBusiness
{
+ // ---------------------------------------------------------------------------||
+ // Contracts -----------------------------------------------------------------||
+ // ---------------------------------------------------------------------------||
+ /**
+ * Ends the current session; will result in a SFSB @Remove call
+ * as the bean implementation class will annotate this with
+ * {@link Remove}
+ */
+ void endSession();
}
Modified: projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java 2009-05-20 19:13:59 UTC (rev 89210)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/java/org/jboss/ejb3/examples/ch06/filetransfer/FtpServerPojo.java 2009-05-20 20:04:01 UTC (rev 89211)
@@ -21,9 +21,6 @@
*/
package org.jboss.ejb3.examples.ch06.filetransfer;
-import java.io.File;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
@@ -140,7 +137,6 @@
// Get the current CL
final ClassLoader tccl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
{
-
@Override
public ClassLoader run()
{
@@ -151,30 +147,16 @@
// Load the properties file to get its URI
final String usersConfigFileName = this.getUsersConfigFileName();
log.info("Using users configuration file: " + usersConfigFileName);
- final URL usersFileUrl = tccl.getResource(usersConfigFileName);
- if (usersFileUrl == null)
+ final URL usersConfigUrl = tccl.getResource(usersConfigFileName);
+ if (usersConfigUrl == null)
{
throw new RuntimeException("Could not find specified users configuration file upon the classpath: "
+ usersConfigFileName);
}
- URI usersFileUri = null;
- try
- {
- usersFileUri = usersFileUrl.toURI();
- }
- catch (final URISyntaxException urise)
- {
- throw new RuntimeException(urise);
- }
- final File usersFile = new File(usersFileUri);
- if (!usersFile.exists())
- {
- throw new RuntimeException("Specified users configuration file does not exist: " + usersFile.getAbsolutePath());
- }
// Configure the user auth mechanism
final PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
- userManagerFactory.setFile(usersFile);
+ userManagerFactory.setUrl(usersConfigUrl);
userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
final UserManager userManager = userManagerFactory.createUserManager();
serverFactory.setUserManager(userManager);
Added: projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/META-INF/ejb-jar.xml
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/META-INF/ejb-jar.xml (rev 0)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/META-INF/ejb-jar.xml 2009-05-20 20:04:01 UTC (rev 89211)
@@ -0,0 +1,44 @@
+<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+ http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
+ version="3.0">
+ <!-- TODO
+ https://jira.jboss.org/jira/browse/EJBBOOK-6
+ Update the Schema to 3.1
+ -->
+
+ <enterprise-beans>
+
+ <!--
+ In this section we'll bolster our FileTransferEJB with some
+ additional metadata to complement the info defined via
+ annotations.
+ -->
+ <session>
+
+ <!--
+ This will match the value of @Stateful.name upon our bean
+ implementation class
+ -->
+ <ejb-name>FileTransferEJB</ejb-name>
+
+ <!-- Define the FTP Host to which we'll connect -->
+ <env-entry>
+ <env-entry-name>connectHost</env-entry-name>
+ <env-entry-type>java.lang.String</env-entry-type>
+ <env-entry-value>localhost</env-entry-value>
+ </env-entry>
+
+ <!-- Define the FTP Port to which we'll connect -->
+ <env-entry>
+ <env-entry-name>connectPort</env-entry-name>
+ <env-entry-type>java.lang.Integer</env-entry-type>
+ <env-entry-value>12345</env-entry-value>
+
+ </env-entry>
+
+ </session>
+
+ </enterprise-beans>
+
+</ejb-jar>
\ No newline at end of file
Deleted: projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties 2009-05-20 19:13:59 UTC (rev 89210)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties 2009-05-20 20:04:01 UTC (rev 89211)
@@ -1,6 +0,0 @@
-# Users / Passwords file
-ftpserver.user.user.idletime=0
-ftpserver.user.user.userpassword=password
-ftpserver.user.user.homedirectory=/
-ftpserver.user.user.writepermission=true
-ftpserver.user.user.enableflag=true
\ No newline at end of file
Copied: projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties (from rev 89155, projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties)
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties (rev 0)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties 2009-05-20 20:04:01 UTC (rev 89211)
@@ -0,0 +1,6 @@
+# Users / Passwords file
+ftpserver.user.user.idletime=0
+ftpserver.user.user.userpassword=password
+ftpserver.user.user.homedirectory=/
+ftpserver.user.user.writepermission=true
+ftpserver.user.user.enableflag=true
\ No newline at end of file
Property changes on: projects/ejb-book/trunk/ch06-filetransfer/src/main/resources/ftpusers.properties
___________________________________________________________________
Name: svn:mergeinfo
+
Added: projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java (rev 0)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferIntegrationTestCase.java 2009-05-20 20:04:01 UTC (rev 89211)
@@ -0,0 +1,255 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.ejb3.examples.ch06.filetransfer;
+
+import java.io.File;
+
+import javax.ejb.NoSuchEJBException;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import junit.framework.TestCase;
+
+import org.jboss.logging.Logger;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * FileTransferIntegrationTestCase
+ *
+ * Test cases to ensure that the FileTransferEJB is working as
+ * a Stateful Session Bean from the EJB Container.
+ *
+ * Inherits some test support from {@link FileTransferTestCaseBase},
+ * and additionally tests EJB-specific tasks upon the
+ * proxy. Shows that sessions operate in isolation, and that removal
+ * of a session means you cannot use it anymore.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class FileTransferIntegrationTestCase extends FileTransferTestCaseBase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(FileTransferIntegrationTestCase.class);
+
+ /**
+ * JNDI Name to which the FileTransferEJB is bound
+ */
+ //TODO Use Global JNDI Name (not yet available in JBoss EJB3)
+ private static final String JNDI_NAME_FILETRANSFER_EJB = FileTransferBean.EJB_NAME + "/remote";
+
+ /**
+ * Naming context used for lookups
+ */
+ private static Context namingContext;
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Our view of the EJB, remote business interface type of the Proxy
+ */
+ private FileTransferRemoteBusiness client;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates and sets the JNDI Naming Context used to look up SFSB Proxies
+ */
+ @BeforeClass
+ public static void createNamingContext() throws Exception
+ {
+ // Create
+ final Context context = new InitialContext(); // Properties from ClassPath jndi.properties
+
+ // Log and set
+ log.info("Created JNDI Context: " + context);
+ namingContext = context;
+ }
+
+ /**
+ * Obtains and sets the FTP Client SFSB Proxy
+ */
+ @Before
+ public void obtainClient() throws Exception
+ {
+ // Set
+ client = this.createNewSession();
+ }
+
+ /**
+ * Ends the session upon the FTP Client SFSB Proxy
+ * and resets
+ */
+ @After
+ public void endClientSession() throws Exception
+ {
+ // End the session
+ try
+ {
+ client.endSession();
+ }
+ // If we've already been ended
+ catch (final NoSuchEJBException nsee)
+ {
+ // Ignore
+ }
+
+ // Clear
+ client = null;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Tests that two separate sessions will act in isolation from each other
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSessionIsolation() throws Exception
+ {
+ // Log
+ log.info("testSessionIsolation");
+
+ // Get the existing client as made from the test lifecycle
+ final FileTransferRemoteBusiness session1 = this.getClient();
+
+ // Make a new session and use that as another client
+ final FileTransferRemoteBusiness session2 = this.createNewSession();
+
+ // cd into a home directory for each
+ final String ftpHome = getFtpHome().getAbsolutePath();
+ session1.cd(ftpHome);
+ session2.cd(ftpHome);
+
+ // Now make a new directory for each session, and go into it
+ final String newDirSession1 = "newDirSession1";
+ final String newDirSession2 = "newDirSession2";
+ session1.mkdir(newDirSession1);
+ session1.cd(newDirSession1);
+ session2.mkdir(newDirSession2);
+ session2.cd(newDirSession2);
+
+ // Get the current working directory for each session
+ final String pwdSession1 = session1.pwd();
+ final String pwdSession2 = session2.pwd();
+
+ // Ensure each session is in the proper working directory
+ TestCase.assertEquals("Session 1 is in unexpected pwd", ftpHome + File.separator + newDirSession1, pwdSession1);
+ TestCase.assertEquals("Session 2 is in unexpected pwd", ftpHome + File.separator + newDirSession2, pwdSession2);
+
+ // End the session manually for session2 (session1 will be ended by test lifecycle)
+ session2.endSession();
+ }
+
+ /**
+ * Tests that a call to {@link FileTransferRemoteBusiness#endSession()}
+ * results in the SFSB's backing instance removal, and that subsequent
+ * operations result in a {@link NoSuchEJBException}
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSfsbRemoval() throws Exception
+ {
+ // Log
+ log.info("testSfsbRemoval");
+
+ // Get the existing client as made from the test lifecycle
+ final FileTransferRemoteBusiness sfsb = this.getClient();
+
+ // cd into the home directory
+ final String ftpHome = getFtpHome().getAbsolutePath();
+ sfsb.cd(ftpHome);
+
+ // Get and test the pwd
+ final String pwdBefore = sfsb.pwd();
+ TestCase.assertEquals("Session should be in the FTP Home directory", ftpHome, pwdBefore);
+
+ // End the session, resulting in an underlying instance
+ // removal due to the annotation with @Remove upon
+ // the bean implementation class
+ sfsb.endSession();
+
+ // Now try some other operation, and ensure that we get a NoSuchEJBException
+ boolean gotExpectedException = false;
+ try
+ {
+ // This should not succeed, because we've called a method marked as @Remove
+ sfsb.pwd();
+ }
+ catch (final NoSuchEJBException nsee)
+ {
+ gotExpectedException = true;
+ }
+ TestCase.assertTrue("Call to end the session did not result in underlying removal of the SFSB bean instance",
+ gotExpectedException);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch06.filetransfer.FileTransferTestCaseBase#getClient()
+ */
+ @Override
+ protected FileTransferRemoteBusiness getClient()
+ {
+ return this.client;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the SFSB Proxy from JNDI, creating a new user session
+ */
+ private FileTransferRemoteBusiness createNewSession() throws Exception
+ {
+ // Look up in JNDI
+ final String jndiName = JNDI_NAME_FILETRANSFER_EJB;
+ final Object proxy = namingContext.lookup(jndiName);
+ log.info("Obtained FTP EJB Proxy from JNDI at \"" + jndiName + "\" : " + proxy);
+
+ // Return
+ return (FileTransferRemoteBusiness) proxy;
+ }
+
+}
Added: projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferTestCaseBase.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferTestCaseBase.java (rev 0)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferTestCaseBase.java 2009-05-20 20:04:01 UTC (rev 89211)
@@ -0,0 +1,238 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.ejb3.examples.ch06.filetransfer;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+import org.jboss.logging.Logger;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * FileTransferTestCaseBase
+ *
+ * Base tests for the file transfer test classes, may
+ * be extended either from unit or integration tests.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public abstract class FileTransferTestCaseBase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(FileTransferTestCaseBase.class);
+
+ /**
+ * The name of the directory under the writable temp filesystem which
+ * will act as the home for these tests
+ */
+ private static final String RELATIVE_LOCATION_HOME = "ejb31_ch06-example-ftpHome";
+
+ /**
+ * The name of the system property denoting the I/O temp directory
+ */
+ private static final String SYS_PROP_NAME_IO_TMP_DIR = "java.io.tmpdir";
+
+ /**
+ * The File we'll use as the writeable home for FTP operations. Created and
+ * destroyed alongside test lifecycle.
+ */
+ private static File ftpHome;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates the directory which we'll use as the writeable home
+ * for FTP operations; called before each test is run.
+ *
+ * @throws Exception
+ */
+ @Before
+ public void createFtpHome() throws Exception
+ {
+ final File ftpHome = getFtpHome();
+ if (ftpHome.exists())
+ {
+ throw new RuntimeException("Error in test setup; FTP Home should not yet exist: " + ftpHome.getAbsolutePath());
+ }
+ final boolean created = ftpHome.mkdir();
+ if (!created)
+ {
+ throw new RuntimeException("Request to create the FTP Home failed: " + ftpHome.getAbsolutePath());
+ }
+ log.info("Created FTP Home: " + ftpHome.getAbsolutePath());
+ }
+
+ /**
+ * Removes the directory used as the writeable home
+ * for FTP operations; called after each test is run.
+ *
+ * @throws Exception
+ */
+ @After
+ public void deleteFtpHome() throws Exception
+ {
+ final File ftpHome = getFtpHome();
+ if (!ftpHome.exists())
+ {
+ throw new RuntimeException("Error in test setup; FTP Home should exist: " + ftpHome.getAbsolutePath());
+ }
+ final boolean removed = this.deleteRecursive(ftpHome);
+ if (!removed)
+ {
+ throw new RuntimeException("Request to remove the FTP Home failed: " + ftpHome.getAbsolutePath());
+ }
+ log.info("Removed FTP Home: " + ftpHome.getAbsolutePath());
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Tests that the a new directory can be made, we can switch
+ * into it, and we can obtain the present working directory of our newly-created
+ * directory
+ */
+ @Test
+ public void testMkdirCdAndPwd() throws Exception
+ {
+ // Log
+ log.info("testMkdirAndPwd");
+
+ // Get the client
+ final FileTransferCommonBusiness client = this.getClient();
+
+ // Switch to home
+ final String home = getFtpHome().getAbsolutePath();
+ client.cd(home);
+
+ // Ensure we're home
+ final String pwdBefore = client.pwd();
+ TestCase.assertEquals("Present working directory should be our home", home, pwdBefore);
+
+ // Make the directory
+ final String newDir = "newDirectory";
+ client.mkdir(newDir);
+
+ // cd into the new dir
+ client.cd(newDir);
+
+ // Ensure we're in the new directory
+ final String pwdAfter = client.pwd();
+ TestCase.assertEquals("Present working directory should be our new directory", home + File.separator + newDir,
+ pwdAfter);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the client to be used for the tests
+ */
+ protected abstract FileTransferCommonBusiness getClient();
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Recursively deletes all contents of the specified root,
+ * including the root itself. If the specified root does not exist,
+ * no action is taken.
+ *
+ * @param root
+ * @return true if deleted, false otherwise
+ */
+ protected boolean deleteRecursive(final File root)
+ {
+ // Ensure exists
+ if (!root.exists())
+ {
+ return false;
+ }
+
+ // Get all children
+ final File[] children = root.listFiles();
+ // If it's a directory
+ if (children != null)
+ {
+ // Remove all children
+ for (final File child : children)
+ {
+ this.deleteRecursive(child);
+ }
+ }
+
+ // Delete me
+ final boolean success = root.delete();
+ log.info("Deleted: " + root);
+ return success;
+ }
+
+ /**
+ * Obtains the writeable home for these tests, set under the namespace of the
+ * IO Temp directory
+ */
+ protected static File getFtpHome() throws Exception
+ {
+ // If the home is not defined
+ if (ftpHome == null)
+ {
+
+ // Get the property
+ final String sysPropIoTempDir = SYS_PROP_NAME_IO_TMP_DIR;
+ final String ioTempDir = System.getProperty(sysPropIoTempDir);
+ if (ioTempDir == null)
+ {
+ throw new RuntimeException("I/O temp directory was not specified by system property: " + sysPropIoTempDir);
+ }
+
+ // Make the File
+ final File ioTempDirFile = new File(ioTempDir);
+ if (!ioTempDirFile.exists())
+ {
+ throw new RuntimeException("I/O Temp directory does not exist: " + ioTempDirFile.getAbsolutePath());
+ }
+
+ // Append the suffix for our home
+ final File home = new File(ioTempDirFile, RELATIVE_LOCATION_HOME);
+ ftpHome = home;
+ }
+
+ // Return
+ return ftpHome;
+ }
+}
Modified: projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferUnitTestCase.java
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferUnitTestCase.java 2009-05-20 19:13:59 UTC (rev 89210)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/java/org/jboss/ejb3/examples/ch06/filetransfer/FileTransferUnitTestCase.java 2009-05-20 20:04:01 UTC (rev 89211)
@@ -21,8 +21,6 @@
*/
package org.jboss.ejb3.examples.ch06.filetransfer;
-import java.io.File;
-
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
@@ -48,7 +46,7 @@
* @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
* @version $Revision: $
*/
-public class FileTransferUnitTestCase
+public class FileTransferUnitTestCase extends FileTransferTestCaseBase
{
//-------------------------------------------------------------------------------------||
@@ -80,23 +78,6 @@
*/
private static final String FILE_NAME_USERS_CONFIG = "ftpusers.properties";
- /**
- * The name of the directory under the writable temp filesystem which
- * will act as the home for these tests
- */
- private static final String RELATIVE_LOCATION_HOME = "ejb31_ch06-example-ftpHome";
-
- /**
- * The name of the system property denoting the I/O temp directory
- */
- private static final String SYS_PROP_NAME_IO_TMP_DIR = "java.io.tmpdir";
-
- /**
- * The File we'll use as the writeable home for FTP operations. Created and
- * destroyed alongside test lifecycle.
- */
- private static File ftpHome;
-
//-------------------------------------------------------------------------------------||
// Instance Members -------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -161,14 +142,11 @@
/**
* Creates and initializes the FTP Client used in testing.
- * Creates the directory which we'll use as the writeable home
- * for FTP operations. Fired before each test is run.
+ * Fired before each test is run.
*/
@Before
- public void initialize() throws Exception
+ public void createFtpClient() throws Exception
{
- // Create the writeable home
- this.createFtpHome();
// Create client
final FileTransferBean ftpClient = new FileTransferBean();
@@ -186,8 +164,7 @@
}
/**
- * Disconnects and resets the FTP Client. Removes the writeable
- * home for FTP operations. Fired after each
+ * Disconnects and resets the FTP Client. Fired after each
* test has completed.
*
* @throws Exception
@@ -205,9 +182,6 @@
ftpClient.disconnect();
this.ftpClient = null;
}
-
- // Remove the writeable home
- this.deleteFtpHome();
}
//-------------------------------------------------------------------------------------||
@@ -215,41 +189,6 @@
//-------------------------------------------------------------------------------------||
/**
- * Tests that the a new directory can be made, we can switch
- * into it, and we can obtain the present working directory of our newly-created
- * directory
- */
- @Test
- public void testMkdirCdAndPwd() throws Exception
- {
- // Log
- log.info("testMkdirAndPwd");
-
- // Get the client
- final FileTransferCommonBusiness client = this.ftpClient;
-
- // Switch to home
- final String home = getFtpHome().getAbsolutePath();
- client.cd(home);
-
- // Ensure we're home
- final String pwdBefore = client.pwd();
- TestCase.assertEquals("Present working directory should be our home", home, pwdBefore);
-
- // Make the directory
- final String newDir = "newDirectory";
- client.mkdir(newDir);
-
- // cd into the new dir
- client.cd(newDir);
-
- // Ensure we're in the new directory
- final String pwdAfter = client.pwd();
- TestCase.assertEquals("Present working directory should be our new directory", home + File.separator + newDir,
- pwdAfter);
- }
-
- /**
* Mocks the passivation/activation process by manually invoking
* upon the {@link PrePassivate} and {@link PostActivate} lifecycle
* callbacks. The client should function properly after these calls are made,
@@ -265,7 +204,7 @@
log.info("testPassivationAndActivation");
// Get the client
- final FileTransferCommonBusiness client = this.ftpClient;
+ final FileTransferCommonBusiness client = this.getClient();
// Switch to home
final String home = getFtpHome().getAbsolutePath();
@@ -290,114 +229,16 @@
}
//-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
//-------------------------------------------------------------------------------------||
- /**
- * Deletes the writable FTP Home
- *
- * @throws Exception
+ /* (non-Javadoc)
+ * @see org.jboss.ejb3.examples.ch06.filetransfer.FileTransferTestCaseBase#getClient()
*/
- protected void deleteFtpHome() throws Exception
+ @Override
+ protected FileTransferCommonBusiness getClient()
{
- final File ftpHome = getFtpHome();
- if (!ftpHome.exists())
- {
- throw new RuntimeException("Error in test setup; FTP Home should exist: " + ftpHome.getAbsolutePath());
- }
- final boolean removed = this.deleteRecursive(ftpHome);
- if (!removed)
- {
- throw new RuntimeException("Request to remove the FTP Home failed: " + ftpHome.getAbsolutePath());
- }
- log.info("Removed FTP Home: " + ftpHome.getAbsolutePath());
+ return this.ftpClient;
}
- /**
- * Recursively deletes all contents of the specified root,
- * including the root itself. If the specified root does not exist,
- * no action is taken.
- *
- * @param root
- * @return true if deleted, false otherwise
- */
- protected boolean deleteRecursive(final File root)
- {
- // Ensure exists
- if (!root.exists())
- {
- return false;
- }
-
- // Get all children
- final File[] children = root.listFiles();
- // If it's a directory
- if (children != null)
- {
- // Remove all children
- for (final File child : children)
- {
- this.deleteRecursive(child);
- }
- }
-
- // Delete me
- final boolean success = root.delete();
- log.info("Deleted: " + root);
- return success;
- }
-
- /**
- * Creates the writable FTP Home
- *
- * @throws Exception
- */
- protected void createFtpHome() throws Exception
- {
- final File ftpHome = getFtpHome();
- if (ftpHome.exists())
- {
- throw new RuntimeException("Error in test setup; FTP Home should not yet exist: " + ftpHome.getAbsolutePath());
- }
- final boolean created = ftpHome.mkdir();
- if (!created)
- {
- throw new RuntimeException("Request to create the FTP Home failed: " + ftpHome.getAbsolutePath());
- }
- log.info("Created FTP Home: " + ftpHome.getAbsolutePath());
- }
-
- /**
- * Obtains the writeable home for these tests, set under the namespace of the
- * IO Temp directory
- */
- private static File getFtpHome() throws Exception
- {
- // If the home is not defined
- if (ftpHome == null)
- {
-
- // Get the property
- final String sysPropIoTempDir = SYS_PROP_NAME_IO_TMP_DIR;
- final String ioTempDir = System.getProperty(sysPropIoTempDir);
- if (ioTempDir == null)
- {
- throw new RuntimeException("I/O temp directory was not specified by system property: " + sysPropIoTempDir);
- }
-
- // Make the File
- final File ioTempDirFile = new File(ioTempDir);
- if (!ioTempDirFile.exists())
- {
- throw new RuntimeException("I/O Temp directory does not exist: " + ioTempDirFile.getAbsolutePath());
- }
-
- // Append the suffix for our home
- final File home = new File(ioTempDirFile, RELATIVE_LOCATION_HOME);
- ftpHome = home;
- }
-
- // Return
- return ftpHome;
- }
}
Added: projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties
===================================================================
--- projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties (rev 0)
+++ projects/ejb-book/trunk/ch06-filetransfer/src/test/resources/jndi.properties 2009-05-20 20:04:01 UTC (rev 89211)
@@ -0,0 +1,4 @@
+# JNDI Properties for Remote interaction with JBoss Application Server Naming Service
+java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
+java.naming.provider.url=jnp://localhost:1099
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list