Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbc.tests;
- import static org.junit.Assert.*;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.Driver;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import consolePortal.IjdbcAdaptor;
- import consolePortal.JdbcAdaptor;
- import jdbc.ConnectionImp;
- /**
- *
- * @author michael.
- *
- */
- public class StatementTest {
- private String protocol = "xmldb";
- private String tmp = System.getProperty("java.io.tmpdir");
- private Properties info;
- private IjdbcAdaptor adapter;
- private Connection conn;
- private Statement st;
- /**
- * before testing.
- * @throws SQLException
- * if invalid statement.
- */
- @Before
- public final void loadData() throws SQLException {
- info = new Properties();
- info.setProperty("path", "jdbc:jsondb://localhost");
- adapter = new JdbcAdaptor();
- conn = new ConnectionImp(info, adapter);
- st = conn.createStatement();
- }
- /**
- * test execute method.
- */
- @Test
- public final void testExecute() {
- try {
- conn = createUseDatabase("TestDB_Create");
- assertEquals(false, st.execute("CREATE TABLE project("
- + "columnName1 varchar,"
- + " columnName2 int,"
- + " columnName3 date)"));
- st.close();
- } catch (SQLException e) {
- fail("you shouldn't throw execption");
- }
- try {
- conn = createUseDatabase("TestDB_Create");
- st = conn.createStatement();
- st.execute("CREATE TABLE project("
- + "columnName1 varchar,"
- + " columnName2 int,"
- + " columnName3 date)");
- assertEquals(false, st.execute("DROP TABLE project"));
- st.close();
- } catch (SQLException e) {
- fail("you shouldn't throw execption");
- }
- try {
- conn = createUseDatabase("TestDB_Create");
- st = conn.createStatement();
- st.execute("CREATE TABLE project("
- + "columnName1 varchar,"
- + " columnName2 int,"
- + " columnName3 date)");
- assertEquals(false, st.execute(
- "SELECT * From project"));
- st.close();
- } catch (SQLException e) {
- fail("you shouldn't throw execption");
- }
- }
- /**
- * test execute update.
- */
- @Test
- public final void testExecuteUpdate() {
- try {
- conn = createUseDatabase("TestDB_Create");
- st = conn.createStatement();
- st.execute("CREATE TABLE project(columnName1 varchar,"
- + " columnName2 int, columnName3 varchar)");
- Assert.assertEquals(1, st.executeUpdate(
- "INSERT INTO project(columnName1,"
- + " columnName2, columnName3)"
- + " VALUES ('go', 4, 'to')"));
- Assert.assertEquals(1, st.executeUpdate(
- "INSERT INTO project(columnName1,"
- + " columnName2, columnName3)"
- + " VALUES ('go', 4, 'to')"));
- Assert.assertEquals(2, st.executeUpdate(
- "UPDATE project SET columnName1 = '1111111111',"
- + " columnName2 = 2222222,"
- + " columnName3='333333333'"));
- st.close();
- } catch (SQLException e) {
- fail("you shouldn't throw execption");
- }
- }
- /**
- * test execute query.
- */
- @Test
- public final void testExecuteQuery() {
- try {
- conn = createUseDatabase("TestDB_Create");
- st = conn.createStatement();
- st.execute("CREATE TABLE project("
- + "columnName1 varchar,"
- + " columnName2 int,"
- + " columnName3 varchar)");
- Assert.assertEquals(1, st.executeUpdate(
- "INSERT INTO project(columnName1,"
- + " columnName2, columnName3)"
- + " VALUES ('go', 4, 'to')"));
- Assert.assertEquals(1, st.executeUpdate(
- "INSERT INTO project(columnName1,"
- + " columnName2, columnName3)"
- + " VALUES ('go', 5, 'to')"));
- Assert.assertEquals(1, st.executeUpdate(
- "INSERT INTO project(columnName1,"
- + " columnName2, columnName3)"
- + " VALUES ('go', 6, 'to')"));
- Assert.assertEquals(1, st.executeUpdate(
- "INSERT INTO project(columnName1,"
- + " columnName2, columnName3)"
- + " VALUES ('goo', 7, 'too')"));
- ResultSet result = st.executeQuery("SELECT * From project");
- Assert.assertNotNull(result);
- Assert.assertEquals(3, result.getMetaData().getColumnCount());
- } catch (SQLException e) {
- fail("you shouldn't throw execption");
- }
- }
- private Connection createUseDatabase(String databaseName) throws SQLException {
- Driver driver = (Driver) TestRunner.getImplementationInstance();
- Properties info = new Properties();
- File dbDir = new File(tmp + "/jdbc/" + Math.round((((float) Math.random()) * 100000)));
- info.put("path", dbDir.getAbsoluteFile());
- Connection connection = driver.connect("jdbc:" + protocol + "://localhost", info);
- Statement statement = connection.createStatement();
- statement.execute("CREATE DATABASE " + databaseName);
- statement.execute("USE " + databaseName);
- statement.close();
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement