Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * OfficeFloor - http://www.officefloor.net
   * Copyright (C) 2005-2013 Daniel Sagenschneider
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU 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 General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 package net.officefloor.plugin.jdbc.vendor;
 
 
 
Provides the abstract functionality for testing the net.officefloor.plugin.jdbc.connection.JdbcManagedObjectSource working with a vendor database.

Author(s):
Daniel Sagenschneider
 
 public abstract class AbstractVendorJdbcTest extends
Flag indicating if the database is available.
 
 	protected boolean isDatabaseAvailable;
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see net.officefloor.frame.test.AbstractOfficeConstructTestCase#setUp()
 	 */
 	protected void setUp() throws Exception {
 		super.setUp();
 
 		// Obtain the connection pool data source
 		Properties properties = this.getDataSourceProperties();
 		for (String name : properties.stringPropertyNames()) {
 			String value = properties.getProperty(name);
 			loader.addProperty(namevalue);
 		}
 		JdbcManagedObjectSource mos = loader
 
 		// Attempt to obtain connection to database to ensure available
 		try {
 			ConnectionPoolDataSource dataSource = JdbcDataSourceAccess
 			PooledConnection connection = dataSource.getPooledConnection();
 			connection.close();
 
 			// Obtained connection, so database available
 			this. = true;
 
 		} catch (Throwable ex) {
 			// Failed to get connection, so not available
 			this. = false;
 
 			// Indicate database not available
 			..println("============================================");
 			..println("  Test " + this.getName()
 					+ " invalid as database not available");
 			..println("============================================");
 		}
 
 		// Clean up database ready for testing
 		try {
 			ConnectionPoolDataSource dataSource = JdbcDataSourceAccess
 			PooledConnection connection = dataSource.getPooledConnection();
			if (this.isDropTablesOnSetup()) {
				Statement statement = connection.getConnection()
				statement.execute("DROP TABLE PRODUCT");
			}
			connection.close();
catch (SQLException ex) {
			// IMDBs will not be setup and otherwise should pass.
			// Also first runs will fail as table not yet created.
		}
	}

Obtains the properties for the javax.sql.ConnectionPoolDataSource.

Returns:
Properties for the javax.sql.ConnectionPoolDataSource.
		Properties properties = new Properties();
		// Add login timeout as always required
		properties.setProperty("loginTimeout""15");
		// Load specific properties
		this.loadProperties(properties);
		// Return the properties
		return properties;
	}

Overridden to populate the properties for the vendor JDBC implementation.

Parameters:
properties Properties to populate to initialise the net.officefloor.plugin.jdbc.connection.JdbcManagedObjectSource.
	protected abstract void loadProperties(Properties properties);

Flag indicating whether to drop the TABLEs within the database on setup.

Returns:
true to drop the TABLEs.
	protected boolean isDropTablesOnSetup() {
		return true;
	}

Tests setup of a database and selecting data from it.
	public void testSelect() throws Exception {
		// Only proceed if database available
		if (!this.) {
			return;
		}
		// Obtain the office name
		String officeName = this.getOfficeName();
		// Configure the JDBC managed object
		ManagedObjectBuilder<?> moBuilder = this.constructManagedObject("JDBC",
				JdbcManagedObjectSource.classofficeName);
		Properties properties = this.getDataSourceProperties();
		..println("Loading "
" with properties:");
		for (String name : properties.stringPropertyNames()) {
			String value = properties.getProperty(name);
			..println("    " + name + "=" + value);
			moBuilder.addProperty(namevalue);
		}
		moBuilder.addProperty(
								"InitialiseDatabase.sql"));
		// Configure the task for the connection
		JdbcTask task = new JdbcTask(new ConnectionValidator() {
			public void validateConnection(Connection connection)
					throws Throwable {
				// Obtain the product name to validate working
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement
						.executeQuery("SELECT PRODUCT_NAME FROM PRODUCT WHERE PRODUCT_ID = 1");
				resultSet.next();
				String productName = resultSet.getString("PRODUCT_NAME");
				assertEquals("Incorrect product name""Test"productName);
				statement.close();
			}
		});
		String workName = task.construct(this.getOfficeBuilder(), null"JDBC",
				"TEAM");
		// Configure the necessary Teams
		this.constructTeam("TEAM"new PassiveTeam());
		this.constructTeam("of-JDBC.jdbc.recycle"new PassiveTeam());
		// Open the Office Floor
		OfficeFloor officeFloor = this.constructOfficeFloor();
		officeFloor.openOfficeFloor();
		// Obtain the work manager with task to use the connection
		WorkManager workManager = officeFloor.getOffice(officeName)
				.getWorkManager(workName);
		// Invoke work to use the connection
		workManager.invokeWork(null);
		// Close the Office Floor
		officeFloor.closeOfficeFloor();
	}
New to GrepCode? Check out our FAQ X