[jboss-svn-commits] JBL Code SVN: r9853 - in labs/jbossesb/workspace/dmarchant/product/core/services/tests/src/org/jboss/soa/esb/esb/persistence: tests and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Feb 28 15:07:37 EST 2007


Author: driedtoast
Date: 2007-02-28 15:07:37 -0500 (Wed, 28 Feb 2007)
New Revision: 9853

Added:
   labs/jbossesb/workspace/dmarchant/product/core/services/tests/src/org/jboss/soa/esb/esb/persistence/tests/
   labs/jbossesb/workspace/dmarchant/product/core/services/tests/src/org/jboss/soa/esb/esb/persistence/tests/MessageStoreUnitTest.java
Log:
Adding to workspace

Added: labs/jbossesb/workspace/dmarchant/product/core/services/tests/src/org/jboss/soa/esb/esb/persistence/tests/MessageStoreUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dmarchant/product/core/services/tests/src/org/jboss/soa/esb/esb/persistence/tests/MessageStoreUnitTest.java	                        (rev 0)
+++ labs/jbossesb/workspace/dmarchant/product/core/services/tests/src/org/jboss/soa/esb/esb/persistence/tests/MessageStoreUnitTest.java	2007-02-28 20:07:37 UTC (rev 9853)
@@ -0,0 +1,241 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated 
+ * by the @authors tag. All rights reserved. 
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors. 
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A 
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+ * MA  02110-1301, USA.
+ * 
+ * (C) 2005-2006,
+ * @author daniel.brum at jboss.com
+ */
+
+package org.jboss.soa.esb.esb.persistence.tests;
+
+/**
+ * @author dbrum
+ * 
+ * 
+ */
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URI;
+import java.sql.Connection;
+import java.sql.Statement;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.jboss.internal.soa.esb.persistence.format.db.DBConnectionManager;
+import org.jboss.soa.esb.common.Configuration;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.jboss.soa.esb.message.format.MessageType;
+import org.jboss.soa.esb.services.persistence.MessageStore;
+import org.jboss.soa.esb.services.persistence.MessageStoreFactory;
+import org.jboss.soa.esb.services.persistence.MessageStoreType;
+import org.jboss.soa.esb.testutils.HsqldbUtil;
+import org.jboss.soa.esb.testutils.TestEnvironmentUtil;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class MessageStoreUnitTest
+{
+
+	// private static Logger log = Logger.getLogger(MessageStoreUnitTest.class);
+
+	@Test
+	public void testDefaultMessageStore ()
+	{
+		MessageStore store = MessageStoreFactory.getInstance()
+				.getMessageStore();
+		assertEquals((store != null), true);
+	}
+
+	@Test
+	public void testURIMessageStore ()
+	{
+		// only DB supported for now
+		MessageStore store1 = MessageStoreFactory.getInstance()
+				.getMessageStore(MessageStoreType.DATABASE);
+		assertEquals((store1 != null), true);
+	}
+
+	@Test
+	public void testStoreAndRetrieve () throws Exception
+	{
+		// add new messages
+		Message msg1 = MessageFactory.getInstance().getMessage(
+				MessageType.JBOSS_XML);
+		Message msg2 = MessageFactory.getInstance().getMessage(
+				MessageType.JAVA_SERIALIZED);
+		assertEquals((msg1 != null), true);
+		assertEquals((msg2 != null), true);
+
+		MessageStore store = MessageStoreFactory.getInstance().getMessageStore(
+				MessageStoreType.DATABASE);
+		assertEquals((store != null), true);
+
+		// set some properties inside the Message
+		msg1.getProperties().setProperty("prop1", "val1");
+		msg2.getProperties().setProperty("prop1", "val1");
+		msg1.getProperties().setProperty("prop2", "val2");
+		msg2.getProperties().setProperty("prop2", "val2");
+
+		// set the body inside the Message
+		msg1.getBody().setContents(("TEST BODY").getBytes());
+		msg2.getBody().setContents(("TEST BODY").getBytes());
+
+		// set some object attachments inside the Message
+		msg1.getAttachment().addItem(new String("TEST ATTACHMENT1"));
+		msg1.getAttachment().addItem(new String("TEST ATTACHMENT2"));
+		msg2.getAttachment().addItem(new String("TEST ATTACHMENT1"));
+		msg2.getAttachment().addItem(new String("TEST ATTACHMENT2"));
+
+		// keep track of the UID to use in retrieving the Message
+		URI uid1 = null;
+		uid1 = store.addMessage(msg1);
+		System.out.println(uid1);
+		assertEquals((uid1 != null), true);
+
+		URI uid2 = null;
+		uid2 = store.addMessage(msg2);
+		System.out.println(uid2);
+		assertEquals((uid2 != null), true);
+
+		// now retrieve the messages
+		try
+		{
+			Message msgIn1 = store.getMessage(uid1);
+			assertEquals((msgIn1 != null), true);
+
+			Message msgIn2 = store.getMessage(uid2);
+			assertEquals((msgIn2 != null), true);
+		}
+		catch (Exception e)
+		{
+			e.printStackTrace();
+		}
+	}
+
+	@Test
+	public void testDBConnectionManager ()
+	{
+		MessageStore store = MessageStoreFactory.getInstance().getMessageStore(
+				MessageStoreType.DATABASE);
+		assertEquals((store != null), true);
+		DBConnectionManager mgr = DBConnectionManager.getInstance();
+		assertEquals((mgr != null), true);
+	}
+
+	@BeforeClass
+	public static void runBeforeAllTests ()
+	{
+		TestEnvironmentUtil.setESBPropertiesFileToUse();
+		System.out.println(Configuration.dump());
+		try
+		{
+			if (Configuration.getStoreDriver().equals("org.hsqldb.jdbcDriver"))
+			{
+				HsqldbUtil.startHsqldb(
+						TestEnvironmentUtil.getUserDir() + "build/hsqltestdb",
+						"jbossesb");
+				// Get the registry-schema create scripts
+				String database = "hsqldb";
+				String sqlDir = TestEnvironmentUtil.getUserDir() + "install/message-store/sql/" + database + "/";
+				// Drop what is there now, if exists. We want to start fresh.
+				String sqlCreateCmd = MessageStoreUnitTest
+						.readTextFile(new File(sqlDir + "create_database.sql"));
+				String sqlDropCmd = MessageStoreUnitTest.readTextFile(new File(
+						sqlDir + "drop_database.sql"));
+
+				try
+				{
+					Class.forName(Configuration.getStoreDriver());
+				}
+				catch (Exception e)
+				{
+					System.out
+							.println("ERROR: failed to load " + database + " JDBC driver.");
+					e.printStackTrace();
+					return;
+				}
+
+				DBConnectionManager mgr = DBConnectionManager.getInstance();
+				Connection con = mgr.getConnection();
+				Statement stmnt = con.createStatement();
+				System.out.println("Dropping the schema if exist");
+				stmnt.execute(sqlDropCmd);
+				System.out.println("Creating the message store schema");
+				stmnt.execute(sqlCreateCmd);
+			}
+		}
+		catch (Exception e)
+		{ // 
+			e.printStackTrace();
+			System.out
+					.println("We should stop testing, since we don't have a db.");
+			assertTrue(false);
+		}
+	}
+
+	@AfterClass
+	public static void runAfterAllTests ()
+	{
+		try
+		{
+			if (Configuration.getStoreDriver().equals("org.hsqldb.jdbcDriver"))
+				HsqldbUtil.stopHsqldb(Configuration.getStoreUrl(),
+						Configuration.getStoreUser(), Configuration
+								.getStorePwd());
+		}
+		catch (Exception e)
+		{ // 
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * Read the file into a String.
+	 * 
+	 * @param file -
+	 *            the file to be read
+	 * @return String with the content of the file
+	 * @throws IOException -
+	 *             when we can't read the file
+	 */
+	public static String readTextFile (File file) throws IOException
+	{
+		StringBuffer sb = new StringBuffer(1024);
+		BufferedReader reader = new BufferedReader(new FileReader(file
+				.getPath()));
+		char[] chars = new char[1];
+		while ((reader.read(chars)) > -1)
+		{
+			sb.append(String.valueOf(chars));
+			chars = new char[1];
+		}
+		reader.close();
+		return sb.toString();
+	}
+
+	public static junit.framework.Test suite ()
+	{
+		return new JUnit4TestAdapter(MessageStoreUnitTest.class);
+	}
+
+}




More information about the jboss-svn-commits mailing list