[jboss-svn-commits] JBL Code SVN: r13031 - in labs/jbossesb/trunk/product/rosetta: tests/src/org/jboss/soa/esb/notification and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Jul 3 02:09:58 EDT 2007
Author: beve
Date: 2007-07-03 02:09:58 -0400 (Tue, 03 Jul 2007)
New Revision: 13031
Modified:
labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFiles.java
labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java
labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifySqlTable.java
labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyUtil.java
labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyFilesUnitTest.java
labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifySqlTableUnitTest.java
labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java
Log:
Additional work for JIRA: JBESB-452 "Refactor Notifiers":
NotifySqlTable SqlException is now being caught,logged and a NotificationException is throw with the sql exception as cause
NotifySqlTableUnitTest Unit test for testing the exception handling
NotifyFilesTest methods stringNotification and objectNotification now throw IOException. sendNotifcation catches IOException and
for every failure of notifying a file a error message containing the filename and stacktrace is saved, and later
thrown with a NotifcationException.
NotifyFilesUnitTest Unit test for testing the exception handling
NotifyJMS Moved createException to NotifyUtil so the other notification classes can use it.
NotifyTopicsUnitTest updated to reflect the move of createException mentioned above.
Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFiles.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFiles.java 2007-07-03 05:11:18 UTC (rev 13030)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyFiles.java 2007-07-03 06:09:58 UTC (rev 13031)
@@ -24,9 +24,11 @@
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.URI;
+import org.apache.log4j.Logger;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageType;
@@ -48,6 +50,8 @@
*/
public class NotifyFiles extends NotificationTarget
{
+ private Logger log = Logger.getLogger( NotifyFiles.class );
+
/**
* Mnemonic for the child element name that hold the files to write ("file")
*/
@@ -146,6 +150,7 @@
{
FileOutputStream fileOutStream = null;
+ final StringBuilder exceptions = new StringBuilder();
for (NotificationFile notificationFile : m_oaOutF)
{
try
@@ -165,50 +170,40 @@
stringNotification(fileOutStream, content);
}
}
- catch (Exception e)
- { /* We do nothing here for the time being */
+ catch (IOException e)
+ {
+ final String msg = "[Exception while notifying file : " + notificationFile;
+ log.error(msg, e);
+ exceptions.append( NotifyUtil.createExceptionErrorString( msg, e ));
}
finally
{
try
{
- fileOutStream.close();
+ if ( fileOutStream != null )
+ fileOutStream.close();
}
- catch (Exception eCl)
+ catch (IOException eCl)
{
- /*
- * Unable to Close - What could we do
- */
+ log.error( "IOException while closing fileOutStream: ", eCl );
}
}
}
+ if ( exceptions.length() > 0 )
+ throw new NotificationException( exceptions.toString() );
} // __________________________________
- private void stringNotification (FileOutputStream p_oF, String p_s)
+ protected void stringNotification (FileOutputStream p_oF, String p_s) throws IOException
{
- try
- {
- p_oF.write(p_s.getBytes());
-
- if (!p_s.endsWith("\n"))
- p_oF.write("\n".getBytes());
- }
- catch (Exception e)
- { /* We do nothing here for the time being */
- }
+ p_oF.write(p_s.getBytes());
+ if (!p_s.endsWith("\n"))
+ p_oF.write("\n".getBytes());
} // __________________________________
- private void objectNotification (FileOutputStream p_oF, Object p_o)
+ protected void objectNotification (FileOutputStream p_oF, Object p_o) throws IOException
{
- try
- {
- ObjectOutputStream OS = new ObjectOutputStream(p_oF);
- OS.writeObject(p_o);
- }
- catch (Exception e)
- {
- /* We do nothing here for the time being */
- }
+ ObjectOutputStream OS = new ObjectOutputStream(p_oF);
+ OS.writeObject(p_o);
} // __________________________________
/**
Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java 2007-07-03 05:11:18 UTC (rev 13030)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java 2007-07-03 06:09:58 UTC (rev 13031)
@@ -22,8 +22,6 @@
package org.jboss.soa.esb.notification;
-import java.io.PrintWriter;
-import java.io.StringWriter;
import java.util.Iterator;
import java.util.Properties;
@@ -57,8 +55,6 @@
{
protected Logger log = Logger.getLogger(this.getClass());
-
-
/**
* Abstract method - All classes that extend NotifyJMS must implement it
* according to their own javax.jms.Destination needs (Queue/Topic)
@@ -237,7 +233,7 @@
{
final String msg = "[JMSException while sending to : " + m_oaMssProd[i1].getDestination();
log.error(msg, e);
- jmsExceptions.append( createExceptionErrorString( msg, e ));
+ jmsExceptions.append( NotifyUtil.createExceptionErrorString( msg, e ));
}
}
if ( jmsExceptions.length() > 0 )
@@ -249,22 +245,4 @@
}
}
- /*
- * Util method for generating an error message containing the
- * stacktrace for the passed in Exception instance.
- *
- * Note that there is no need to close a StringWriter instance.
- */
- protected String createExceptionErrorString( final String msg, final Exception e )
- {
- StringBuilder sb = new StringBuilder();
- sb.append("[").append(msg);
- sb.append(", Stacktrace : ");
- StringWriter sw = new StringWriter();
- e.printStackTrace(new PrintWriter(sw));
- sb.append(sw.toString());
- sb.append("]");
- return sb.toString();
- }
-
} // ____________________________________________________________________________
Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifySqlTable.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifySqlTable.java 2007-07-03 05:11:18 UTC (rev 13030)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifySqlTable.java 2007-07-03 06:09:58 UTC (rev 13031)
@@ -23,8 +23,10 @@
package org.jboss.soa.esb.notification;
import java.sql.PreparedStatement;
+import java.sql.SQLException;
import java.util.Properties;
+import org.apache.log4j.Logger;
import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.helpers.persist.JdbcCleanConn;
@@ -33,6 +35,8 @@
public class NotifySqlTable extends NotificationTarget
{
+ private Logger log = Logger.getLogger( NotifySqlTable.class );
+
public static final String CHILD_COLUMN = "column";
public static final String ATT_TABLE = "table";
@@ -129,24 +133,46 @@
}
m_oCols.setProperty(m_sDataCol, content);
JdbcCleanConn oConn = null;
+ PreparedStatement ps = null;
try
{
- oConn = new JdbcCleanConn(new SimpleDataSource(m_sDriver, m_sURL,
- m_sUser, m_sPwd));
- PreparedStatement PS = oConn.prepareStatement(getInsertStmt());
- oConn.execUpdWait(PS, 3);
+ oConn = createJdbcCleanConnection();
+ ps = oConn.prepareStatement(getInsertStmt());
+ oConn.execUpdWait(ps, 3);
oConn.commit();
+ }
+ catch( SQLException e)
+ {
+ final String errorMsg = "SqlException while trying to notify table. Insert statement : " + getInsertStmt();
+ log.error( errorMsg , e );
+ throw new NotificationException( errorMsg, e);
}
- catch (Exception e)
+ finally
{
- e.printStackTrace(System.err);
- } finally
- {
+ if ( ps != null )
+ {
+ try
+ {
+ ps.close();
+ }
+ catch (SQLException e)
+ {
+ log.error( "SqlException while trying to close prepared statement PS", e );
+ }
+ }
if (null != oConn)
{
oConn.release();
}
}
} // __________________________________
+
+ /*
+ * extracted this method to simplify testing. DanielB
+ */
+ protected JdbcCleanConn createJdbcCleanConnection()
+ {
+ return new JdbcCleanConn(new SimpleDataSource(m_sDriver, m_sURL, m_sUser, m_sPwd));
+ }
} // ____________________________________________________________________________
Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyUtil.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyUtil.java 2007-07-03 05:11:18 UTC (rev 13030)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyUtil.java 2007-07-03 06:09:58 UTC (rev 13031)
@@ -24,7 +24,9 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
+import java.io.PrintWriter;
import java.io.Serializable;
+import java.io.StringWriter;
import java.util.Map;
import org.jboss.internal.soa.esb.notification.MacroExpander;
@@ -98,5 +100,23 @@
e.printStackTrace(System.out);
}
} // __________________________________
+
+ /*
+ * Util method for generating an error message containing the
+ * stacktrace for the passed in Exception instance.
+ *
+ * Note that there is no need to close a StringWriter instance.
+ */
+ public static String createExceptionErrorString( final String msg, final Exception e )
+ {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("[").append(msg);
+ sb.append(", Stacktrace : ");
+ final StringWriter sw = new StringWriter();
+ e.printStackTrace(new PrintWriter(sw));
+ sb.append(sw.toString());
+ sb.append("]");
+ return sb.toString();
+ }
} // ____________________________________________________________________________
Modified: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyFilesUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyFilesUnitTest.java 2007-07-03 05:11:18 UTC (rev 13030)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyFilesUnitTest.java 2007-07-03 06:09:58 UTC (rev 13031)
@@ -22,6 +22,8 @@
package org.jboss.soa.esb.notification;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.Serializable;
import junit.framework.TestCase;
@@ -95,6 +97,26 @@
assertEquals(file1Obj.length(), file3Obj.length() * 2);
}
+ public void test_sendNotficationNegative()
+ {
+ final ConfigTree rootEl = new ConfigTree("notif");
+ addFileConfig(rootEl, "file1.notif", true);
+ addFileConfig(rootEl, "file2.notif", true);
+ final NotifyFiles notifyFiles = new MockNotifyFiles(rootEl);
+ final Message message = MessageFactory.getInstance().getMessage(MessageType.JAVA_SERIALIZED);
+ message.getBody().setByteArray("object".getBytes());
+ try
+ {
+ notifyFiles.sendNotification( message );
+ fail( "sendNotification should have thrown a NotificationException");
+ }
+ catch(Exception e)
+ {
+ assertTrue ( e instanceof NotificationException );
+ }
+
+ }
+
private void addFileConfig(ConfigTree rootEl, String file, Boolean append) {
File fileObj = getFileObject(file);
ConfigTree fileEl = new ConfigTree("file",rootEl);
@@ -126,4 +148,24 @@
String javaPackage = NotifyFilesUnitTest.class.getPackage().getName();
return javaPackage.replace('.', '/');
}
+
+ private static class MockNotifyFiles extends NotifyFiles
+ {
+
+ public MockNotifyFiles(ConfigTree p_oP)
+ {
+ super( p_oP );
+ }
+
+ protected void stringNotification (FileOutputStream p_oF, String p_s) throws IOException
+ {
+ throw new IOException("Mock IOException from stringNotification...");
+ } // __________________________________
+
+ protected void objectNotification (FileOutputStream p_oF, Object p_o) throws IOException
+ {
+ throw new IOException("Mock IOException from objectNotification...");
+ } // __________________________________
+
+ }
}
Modified: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifySqlTableUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifySqlTableUnitTest.java 2007-07-03 05:11:18 UTC (rev 13030)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifySqlTableUnitTest.java 2007-07-03 06:09:58 UTC (rev 13031)
@@ -21,9 +21,20 @@
*/
package org.jboss.soa.esb.notification;
+import java.io.IOException;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
import junit.framework.TestCase;
+import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.persist.JdbcCleanConn;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.jboss.soa.esb.message.format.MessageType;
+import org.xml.sax.SAXException;
/**
* NotifySqlTable unit tests.
@@ -49,4 +60,54 @@
assertEquals("insert into table (\"colname3\",\"colname2\",\"colname1\") values ('colvalue3','colvalue2','colvalue1')", nst.getInsertStmt());
}
+
+ public void test_sendNotificationNegative() throws SAXException, IOException, ConfigurationException
+ {
+ ConfigTree domEl = ConfigTree.fromInputStream(getClass().getResourceAsStream("NotifySqlTable_testfile1.xml"));
+ NotifySqlTable nst = new MockNotifySqlTable(domEl);
+ org.jboss.soa.esb.message.Message message = MessageFactory.getInstance().getMessage(MessageType.JAVA_SERIALIZED);
+ message.getBody().setByteArray((new Integer(123).toString().getBytes()));
+ try
+ {
+ nst.sendNotification( message );
+ fail( "sendNotification should have thrown an NotificationException");
+ }
+ catch( Exception e )
+ {
+ assertTrue ( e instanceof NotificationException );
+ }
+
+ }
+
+ private static class MockNotifySqlTable extends NotifySqlTable
+ {
+
+ public MockNotifySqlTable(ConfigTree p_oP) throws ConfigurationException
+ {
+ super( p_oP );
+ }
+
+ @Override
+ protected JdbcCleanConn createJdbcCleanConnection()
+ {
+ return new MockJdbcCleanConn( null );
+ }
+ }
+
+ private static class MockJdbcCleanConn extends JdbcCleanConn
+ {
+
+ public MockJdbcCleanConn(DataSource p_oDS)
+ {
+ super( p_oDS );
+ }
+
+ @Override
+ public PreparedStatement prepareStatement( String p_sSt ) throws SQLException
+ {
+ throw new SQLException( "MockJdbcCleanConn prepareStatement throwing SQLException");
+ }
+
+
+ }
}
Modified: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java 2007-07-03 05:11:18 UTC (rev 13030)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java 2007-07-03 06:09:58 UTC (rev 13031)
@@ -99,7 +99,7 @@
final String msg = "[JMSException while publishing to : /topic/SomeName";
try
{
- String string = notifyTopics.createExceptionErrorString( msg, new JMSException("junit test dummy exception") );
+ String string = NotifyUtil.createExceptionErrorString( msg, new JMSException("junit test dummy exception") );
assertNotNull( string );
}catch(Exception e )
{
More information about the jboss-svn-commits
mailing list