You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by cs...@apache.org on 2007/02/08 19:34:12 UTC
svn commit: r504995 - in /beehive/trunk/samples/system-controls-jdbc: ./
src/ src/org/ src/org/apache/ src/org/apache/beehive/
src/org/apache/beehive/samples/ src/org/apache/beehive/samples/jdbc/
src/org/apache/beehive/samples/jdbc/control/ src/test/
Author: cschoett
Date: Thu Feb 8 10:34:11 2007
New Revision: 504995
URL: http://svn.apache.org/viewvc?view=rev&rev=504995
Log:
First pass at a new JDBC system control sample. This sample demonstrates the following with the JDBC system control:
1) Testing a control out-of-container with JUnit
2) Calling Derby stored procedures from a JDBC control
3) Using a JDBC control custom result set mapper
The sample requires that the Apache Derby jar is available on the users system.
Added:
beehive/trunk/samples/system-controls-jdbc/
beehive/trunk/samples/system-controls-jdbc/README.txt (with props)
beehive/trunk/samples/system-controls-jdbc/build.properties (with props)
beehive/trunk/samples/system-controls-jdbc/build.xml (with props)
beehive/trunk/samples/system-controls-jdbc/src/
beehive/trunk/samples/system-controls-jdbc/src/org/
beehive/trunk/samples/system-controls-jdbc/src/org/apache/
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/AddressLabel.java (with props)
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/CustomEmployeeMapper.java (with props)
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/Employee.java (with props)
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/EmployeeJdbcControl.java (with props)
beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/StoredProcedures.java (with props)
beehive/trunk/samples/system-controls-jdbc/src/test/
beehive/trunk/samples/system-controls-jdbc/src/test/EmployeeJdbcControlTest.java (with props)
Added: beehive/trunk/samples/system-controls-jdbc/README.txt
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/README.txt?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/README.txt (added)
+++ beehive/trunk/samples/system-controls-jdbc/README.txt Thu Feb 8 10:34:11 2007
@@ -0,0 +1,11 @@
+To run this sample, following these steps:
+
+(1) Update the build.properties file in this sample so that beehive.home points to the
+ top level folder of your Beehive installation.
+
+(2) Download the latest version of derby and update the build.properties file in this sample
+ with the location of the derby jar.
+
+(3) cd to /system-controls-jdbc and run: ant clean build test
+
+(4) The JUnit log can be found in the build/logs folder of this sample.
Propchange: beehive/trunk/samples/system-controls-jdbc/README.txt
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/build.properties
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/build.properties?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/build.properties (added)
+++ beehive/trunk/samples/system-controls-jdbc/build.properties Thu Feb 8 10:34:11 2007
@@ -0,0 +1,7 @@
+#
+#
+#
+
+#beehive.home=../..
+beehive.home=/home/cschoett/beehive_alt/trunk/build/dist/apache-beehive-1.1-SNAPSHOT
+derby.jar=/home/cschoett/beehive_alt/trunk/external/derby/derby_46005.jar
Propchange: beehive/trunk/samples/system-controls-jdbc/build.properties
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/build.xml
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/build.xml?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/build.xml (added)
+++ beehive/trunk/samples/system-controls-jdbc/build.xml Thu Feb 8 10:34:11 2007
@@ -0,0 +1,97 @@
+<?xml version="1.0" ?>
+
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ $Header:$
+ -->
+<project name="controls-blank" default="usage" basedir=".">
+
+ <property environment="os"/>
+ <property file="build.properties"/>
+ <import file="${beehive.home}/beehive-imports.xml"/>
+ <import file="${beehive.home}/ant/beehive-tools.xml"/>
+
+ <property name="source.dir" location="${basedir}/src"/>
+ <property name="build.dir" location="${basedir}/build"/>
+ <property name="build.classes" location="${build.dir}/classes"/>
+ <property name="build.beansrc" location="${build.dir}/beansrc"/>
+ <property name="log.dir" location="${build.dir}/logs"/>
+ <property name="build.jar" value="mycontrols.jar"/>
+
+ <path id="build.classpath">
+ <pathelement path="${build.classes}"/>
+ <pathelement path="${beehive.home}/lib/controls/beehive-jdbc-control.jar"/>
+ <path refid="controls.dependency.path"/>
+ <pathelement path="${derby.jar}"/>
+ <fileset dir="${beehive.home}/lib/common">
+ <include name="*.jar"/>
+ </fileset>
+ </path>
+
+ <target name="clean" description="Delete all generated files">
+ <delete dir="${build.dir}"/>
+ <delete dir="${log.dir}"/>
+ <delete file="velocity.log"/>
+ </target>
+
+ <target name="build" description="Build control sources">
+ <mkdir dir="${build.classes}"/>
+ <mkdir dir="${build.beansrc}"/>
+
+ <build-controls srcdir="${source.dir}"
+ destdir="${build.classes}"
+ tempdir="${build.beansrc}"
+ classpathref="build.classpath"/>
+
+ <control-jar destfile="${build.dir}/${build.jar}" basedir="${build.classes}" />
+ </target>
+
+ <target name="test" description="Run JUnit tests" depends="clean,build">
+ <mkdir dir="${log.dir}"/>
+
+ <echo message="Web Service Control JUnit log files written to ${log.dir}"/>
+ <junit printsummary="on" tempdir="${build.classes}" fork="yes">
+ <classpath>
+ <path refid="build.classpath"/>
+ <pathelement location="${build.classes}"/>
+ </classpath>
+ <formatter type="plain"/>
+ <batchtest filtertrace="off" todir="${log.dir}">
+ <fileset dir="${source.dir}">
+ <include name="**/*Test.java"/>
+ </fileset>
+ </batchtest>
+ </junit>
+
+ </target>
+
+ <target name="usage" description="Print the usage for this build.xml">
+ <echo message=""/>
+ <echo message=""/>
+ <echo message="Controls Project Build file"/>
+ <echo message=""/>
+ <echo message=""/>
+ <echo message="----------------------------------------------------------------"/>
+ <echo message="| Standard Targets |"/>
+ <echo message="----------------------------------------------------------------"/>
+ <echo message="clean - Delete all generated files"/>
+ <echo message="build - Build control source files"/>
+ <echo message="test - Run JUnit tests"/>
+ <echo message="----------------------------------------------------------------"/>
+ </target>
+
+</project>
Propchange: beehive/trunk/samples/system-controls-jdbc/build.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/AddressLabel.java
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/AddressLabel.java?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/AddressLabel.java (added)
+++ beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/AddressLabel.java Thu Feb 8 10:34:11 2007
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beehive.samples.jdbc.control;
+
+/**
+ * AddressLabel bean, used for demonstrating custom result set mappers.
+ */
+public class AddressLabel {
+
+ private String line1;
+ private String line2;
+ private String line3;
+
+
+ public String getLine1() {
+ return line1;
+ }
+
+ public void setLine1(String line1) {
+ this.line1 = line1;
+ }
+
+ public String getLine2() {
+ return line2;
+ }
+
+ public void setLine2(String line2) {
+ this.line2 = line2;
+ }
+
+ public String getLine3() {
+ return line3;
+ }
+
+ public void setLine3(String line3) {
+ this.line3 = line3;
+ }
+
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(line1);
+ sb.append('\n');
+ sb.append(line2);
+ sb.append('\n');
+ sb.append(line3);
+ return sb.toString();
+ }
+}
Propchange: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/AddressLabel.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/CustomEmployeeMapper.java
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/CustomEmployeeMapper.java?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/CustomEmployeeMapper.java (added)
+++ beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/CustomEmployeeMapper.java Thu Feb 8 10:34:11 2007
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beehive.samples.jdbc.control;
+
+import org.apache.beehive.controls.api.ControlException;
+import org.apache.beehive.controls.api.context.ControlBeanContext;
+import org.apache.beehive.controls.system.jdbc.ResultSetMapper;
+
+import java.lang.reflect.Method;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Calendar;
+
+/**
+ * This class is a custom result set mapper. Maps a ResultSet from the employee table
+ * to an AddressLabel bean.
+ */
+public class CustomEmployeeMapper extends ResultSetMapper {
+
+ /**
+ * Map a employee result set to an AddressLabel bean
+ *
+ * @param context A ControlBeanContext instance, see Beehive controls javadoc for additional information
+ * @param m Method assoicated with this call.
+ * @param resultSet Result set to map.
+ * @param cal A Calendar instance for time/date value resolution.
+ * @return The Object resulting from the ResultSet
+ */
+ public Object mapToResultType(ControlBeanContext context, Method m, ResultSet resultSet, Calendar cal) {
+
+ try {
+ if (resultSet.next()) {
+ AddressLabel a = new AddressLabel();
+ a.setLine1(resultSet.getString("FIRSTNAME") + " " + resultSet.getString("LASTNAME"));
+ a.setLine2(resultSet.getString("ADDRESS"));
+ a.setLine3(resultSet.getString("CITY") + ", " + resultSet.getString("STATE") + " " + resultSet.getString("ZIP"));
+ return a;
+ }
+ }
+ catch (SQLException sqe) {
+ throw new ControlException("CustomEmployeeMapper failed: " + sqe);
+ }
+
+ return null;
+ }
+}
Propchange: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/CustomEmployeeMapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/Employee.java
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/Employee.java?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/Employee.java (added)
+++ beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/Employee.java Thu Feb 8 10:34:11 2007
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beehive.samples.jdbc.control;
+
+/**
+ * Java bean for employee information.
+ */
+public class Employee {
+
+ private String firstName;
+ private String lastName;
+ private int employeeId;
+ private String address;
+ private String city;
+ private String state;
+ private String zip;
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public int getEmployeeId() {
+ return employeeId;
+ }
+
+ public void setEmployeeId(int employeeId) {
+ this.employeeId = employeeId;
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public String getCity() {
+ return city;
+ }
+
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public void setZip(String zip) {
+ this.zip = zip;
+ }
+
+ public String getZip() {
+ return zip;
+ }
+}
Propchange: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/Employee.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/EmployeeJdbcControl.java
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/EmployeeJdbcControl.java?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/EmployeeJdbcControl.java (added)
+++ beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/EmployeeJdbcControl.java Thu Feb 8 10:34:11 2007
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beehive.samples.jdbc.control;
+
+import org.apache.beehive.controls.api.bean.ControlExtension;
+import org.apache.beehive.controls.system.jdbc.JdbcControl;
+
+/**
+ * Sample JDBC control. This JDBC control was designed to work with a database
+ * table generated by the following SQL:
+ *
+ * CREATE TABLE employee (firstname VARCHAR(30), lastname VARCHAR(40), employeeid INT,
+ * address VARCHAR(120), city VARCHAR(40), state VARCHAR(10), zip VARCHAR(10)
+ *
+ */
+@ControlExtension
+@JdbcControl.ConnectionDataSource(jndiName="java:/comp/env/EmployeeDB")
+public interface EmployeeJdbcControl extends JdbcControl {
+
+
+ /**
+ * Add an employee to the database. Values from the parameter are mapped into the
+ * the SQL statement.
+ *
+ * @param e Employee bean.
+ */
+ @JdbcControl.SQL(statement="INSERT INTO employee VALUES ({e.firstName}, {e.lastName}, {e.employeeId}, {e.address}, {e.city}, {e.state}, {e.zip})")
+ public void addEmployee(Employee e);
+
+ /**
+ * Get an employee record from the database.
+ *
+ * @param employeeId id of the employee.
+ * @return Employee bean.
+ */
+ @JdbcControl.SQL(statement="SELECT * FROM employee WHERE employeeid={employeeId}")
+ public Employee getEmployee(int employeeId);
+
+ /**
+ * Is the employee id value (i.e. is it assigned to an employee in the db?).
+ * This method invokes a Derby stored procedure, the first parameter to that
+ * procedure is the employee id, the second is an out parameter of type string.
+ *
+ * @param params SQLParameter list.
+ */
+ @JdbcControl.SQL(statement="{call isValidIdStoredProc(?,?)}")
+ public void isValidId(JdbcControl.SQLParameter[] params);
+
+ /**
+ * Use a custom result set mapper to transform a row from the employee db into
+ * an AddressLabel bean.
+ *
+ * @param employeeId Id of the employee record to retrieve.
+ * @return AddressLabel bean, null if employee id does not exist in table.
+ */
+ @JdbcControl.SQL(statement="SELECT * FROM employee WHERE employeeid={employeeId}",
+ resultSetMapper= CustomEmployeeMapper.class)
+ public AddressLabel getEmployeeAddressLabel(int employeeId);
+}
Propchange: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/EmployeeJdbcControl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/StoredProcedures.java
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/StoredProcedures.java?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/StoredProcedures.java (added)
+++ beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/StoredProcedures.java Thu Feb 8 10:34:11 2007
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beehive.samples.jdbc.control;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * Derby stored procedures.
+ */
+public final class StoredProcedures {
+
+ /**
+ * Check the employee ID to determine if it is valid.
+ *
+ * @param employeeId id to check, IN parameter.
+ * @param isValidId OUT parameter, contains a value of "true" or "false".
+ * @throws SQLException If SQL error.
+ * @throws ClassNotFoundException If jdbc driver class cannot be found.
+ */
+ public static void isValidIdStoredProc(int employeeId, String[] isValidId)
+ throws ClassNotFoundException, SQLException
+ {
+ Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
+ Connection conn = DriverManager.getConnection("jdbc:derby:build/TestDB");
+ Statement s = conn.createStatement();
+
+ ResultSet rs = s.executeQuery("SELECT * FROM employee WHERE employeeId = " + employeeId);
+ Boolean isValid = rs.next();
+ isValidId[0] = isValid.toString();
+
+ rs.close();
+ s.close();
+ conn.close();
+ }
+}
Propchange: beehive/trunk/samples/system-controls-jdbc/src/org/apache/beehive/samples/jdbc/control/StoredProcedures.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/samples/system-controls-jdbc/src/test/EmployeeJdbcControlTest.java
URL: http://svn.apache.org/viewvc/beehive/trunk/samples/system-controls-jdbc/src/test/EmployeeJdbcControlTest.java?view=auto&rev=504995
==============================================================================
--- beehive/trunk/samples/system-controls-jdbc/src/test/EmployeeJdbcControlTest.java (added)
+++ beehive/trunk/samples/system-controls-jdbc/src/test/EmployeeJdbcControlTest.java Thu Feb 8 10:34:11 2007
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package test;
+
+import org.apache.beehive.samples.jdbc.control.Employee;
+import org.apache.beehive.samples.jdbc.control.EmployeeJdbcControl;
+import org.apache.beehive.samples.jdbc.control.AddressLabel;
+import org.apache.beehive.controls.api.bean.Control;
+import org.apache.beehive.controls.system.jdbc.JdbcControl;
+import org.apache.beehive.controls.test.junit.ControlTestCase;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+
+/**
+ * Test the EmployeeJdbcControl control out-of-container using the ControlTestCase container.
+ */
+public class EmployeeJdbcControlTest extends ControlTestCase {
+
+ @Control
+ @JdbcControl.ConnectionOptions(useExternalConnection = true)
+ private EmployeeJdbcControl _employeeJdbc;
+
+ private Connection _con;
+
+ /**
+ * Get a connection to the database and setup the necessary tables.
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+
+ // setup the database
+ _con = getConnection();
+ Statement s = _con.createStatement();
+ try {
+ s.executeUpdate("DROP TABLE employee");
+ s.executeUpdate("DROP PROCEDURE isValidIdStoredProc");
+ }
+ catch (Exception e) {
+ // noop
+ }
+
+ s.executeUpdate("CREATE TABLE employee (firstname VARCHAR(30), lastname VARCHAR(40), employeeid INT, address VARCHAR(120), city VARCHAR(40), state VARCHAR(10), zip VARCHAR(10))");
+ s.executeUpdate("INSERT INTO employee VALUES('Jane', 'Doe', 2, '3433 Any Place', 'Denver', 'CO', '80201')");
+
+ s.executeUpdate("CREATE PROCEDURE isValidIdStoredProc(IN id INT, OUT isValid VARCHAR(5)) " +
+ "PARAMETER STYLE JAVA " +
+ "READS SQL DATA " +
+ "LANGUAGE JAVA " +
+ "EXTERNAL NAME 'org.apache.beehive.samples.jdbc.control.StoredProcedures.isValidIdStoredProc'");
+ }
+
+ /**
+ * Close the database connection and cleanup.
+ */
+ public void tearDown() throws Exception {
+ super.tearDown();
+ _con.close();
+ }
+
+ /**
+ * Test adding an employee record to the database.
+ */
+ public void testEmployeeInsert() throws Exception {
+ Employee e = new Employee();
+ e.setFirstName("John");
+ e.setLastName("Doe");
+ e.setAddress("1121 Some Lane");
+ e.setCity("Boulder");
+ e.setState("CO");
+ e.setZip("80301");
+
+ _employeeJdbc.setConnection(_con);
+ _employeeJdbc.addEmployee(e);
+ }
+
+ /**
+ * Test retrieving an employee record by employeeid from the database.
+ */
+ public void testGetEmployee() throws Exception {
+ _employeeJdbc.setConnection(_con);
+
+ Employee e = _employeeJdbc.getEmployee(2);
+ assertEquals("Jane", e.getFirstName());
+ assertEquals("Doe", e.getLastName());
+ assertEquals(2, e.getEmployeeId());
+ assertEquals("3433 Any Place", e.getAddress());
+ assertEquals("Denver", e.getCity());
+ assertEquals("CO", e.getState());
+ assertEquals("80201", e.getZip());
+ }
+
+ /**
+ * Test invoking a stored procedure with a valid user id.
+ * Parameters are passed to the stored procedure in an array
+ * of JdbcControl.SQLParameter instances.
+ */
+ public void testStoredProcedure1() throws Exception {
+ _employeeJdbc.setConnection(_con);
+
+ JdbcControl.SQLParameter[] params = new JdbcControl.SQLParameter[2];
+ params[0] = new JdbcControl.SQLParameter(2, Types.INTEGER, JdbcControl.SQLParameter.IN);
+ params[1] = new JdbcControl.SQLParameter(new String(), Types.VARCHAR, JdbcControl.SQLParameter.OUT);
+
+ _employeeJdbc.isValidId(params);
+ assertEquals("true", params[1].value);
+ }
+
+ /**
+ * Test invoking a stored procedure with a invalid user id.
+ * Parameters are passed to the stored procedure in an array
+ * of JdbcControl.SQLParameter instances.
+ */
+ public void testStoredProcedure2() throws Exception {
+ _employeeJdbc.setConnection(_con);
+
+ JdbcControl.SQLParameter[] params = new JdbcControl.SQLParameter[2];
+ params[0] = new JdbcControl.SQLParameter(8, Types.INTEGER, JdbcControl.SQLParameter.IN);
+ params[1] = new JdbcControl.SQLParameter(new String(), Types.VARCHAR, JdbcControl.SQLParameter.OUT);
+
+ _employeeJdbc.isValidId(params);
+ assertEquals("false", params[1].value);
+ }
+
+ /**
+ * Test a JDBC control method which uses a custom result set mapper.
+ */
+ public void testCustomMapper() throws Exception {
+ _employeeJdbc.setConnection(_con);
+ AddressLabel al = _employeeJdbc.getEmployeeAddressLabel(2);
+
+ assertEquals("Jane Doe", al.getLine1());
+ assertEquals("3433 Any Place", al.getLine2());
+ assertEquals("Denver, CO 80201", al.getLine3());
+ }
+
+ /**
+ * Get a JDBC connection.
+ * @return Connection
+ */
+ private Connection getConnection() {
+ Connection con = null;
+
+ try {
+ Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
+ con = DriverManager.getConnection("jdbc:derby:build/TestDB;create=true");
+ }
+ catch (SQLException sqe) {
+ fail("Unable to get connection: " + sqe.getCause());
+ }
+ catch (ClassNotFoundException e) {
+ fail("Unable to get to find JDBC driver : " + e.getException());
+ }
+ return con;
+ }
+}
Propchange: beehive/trunk/samples/system-controls-jdbc/src/test/EmployeeJdbcControlTest.java
------------------------------------------------------------------------------
svn:eol-style = native