Java tutorial
/* * Copyright (c) 2008-2011 Simon Ritchie. * All rights reserved. * * This program 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 3 of the License, or * (at your option) any later version. * * This program 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 program. If not, see http://www.gnu.org/licenses/>. */ package org.rimudb; import static org.junit.Assert.*; import java.sql.*; import javax.naming.*; import javax.sql.*; import org.apache.commons.dbcp.datasources.*; import org.apache.commons.logging.*; import org.rimudb.pool.*; import org.rimudb.testdb.*; import org.junit.*; /** * @author Simon Ritchie * */ public class JNDIPoolTests { public final static Log log = LogFactory.getLog(JNDIPoolTests.class); /* (non-Javadoc) * @see junit.framework.TestCase#setUp() */ @Before public void setUp() throws Exception { try { // Create initial context System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory"); System.setProperty("org.osjava.sj.jndi.shared", "true"); InitialContext ic = new InitialContext(); ic.createSubcontext("java:/comp/env/"); com.mysql.jdbc.jdbc2.optional.MysqlDataSource mysql_datasource = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource(); mysql_datasource.setServerName("localhost"); mysql_datasource.setPortNumber(3306); mysql_datasource.setDatabaseName("dbtest"); mysql_datasource.setUser("dbtest"); mysql_datasource.setPassword("dbtest"); // Create a connection pool datasource org.h2.jdbcx.JdbcDataSource h2_datasource = new org.h2.jdbcx.JdbcDataSource(); h2_datasource.setURL("jdbc:h2:mem:test"); h2_datasource.setUser("sa"); h2_datasource.setPassword(""); ic.bind("jdbc/dbtest", mysql_datasource); } catch (NamingException ex) { log.error(ex); } } @Test public void testCheckout() throws Exception { // Wait for the delete connection to close Thread.sleep(100); // Connect to the database CompoundDatabase cdb = new CompoundDatabase("/testconfig/jndi-tests-cdb.xml", true); cdb.connect("dbid-1"); OrderTransactionFinder orderTransactionFinder = new OrderTransactionFinder(cdb); orderTransactionFinder.deleteAll(); OrderTransactionDO orderTransaction = new OrderTransactionDO(cdb); orderTransaction.setName("url"); orderTransaction.setOrderNr(1000); orderTransaction.setValue("http://www.rimudb.org"); orderTransaction.commit(); // Remove records from the table OrderTransactionDO[] orderTransactions = orderTransactionFinder.selectAll(); assertNotNull(orderTransactions); assertEquals(1, orderTransactions.length); orderTransactions[0].delete(); // Wait for the connection to close Thread.sleep(100); cdb.disconnectAllNoException(); } }