org.jiemamy.utils.sql.SqlExecutorTest.java Source code

Java tutorial

Introduction

Here is the source code for org.jiemamy.utils.sql.SqlExecutorTest.java

Source

 /*
  * Copyright 2007-2012 Jiemamy Project and the Others.
  * Created on 2009/05/22
  *
  * This file is part of Jiemamy.
  *
  * Licensed 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.jiemamy.utils.sql;

 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;

 import java.io.StringReader;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;

 import org.apache.commons.dbutils.DbUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;

 /**
  * {@link SqlExecutor} ?
  * 
  * @author Keisuke.K
  */
 public class SqlExecutorTest {

     /**
      * ??
      * 
      * <p>H2??</p>
      * 
      * @throws java.lang.Exception ????
      */
     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
         DriverManager.registerDriver(new org.h2.Driver());
     }

     boolean executed;

     int count;

     Connection conn;

     /**
      * ??
      * 
      * <p>H2???</p>
      * 
      * @throws java.lang.Exception ????
      */
     @Before
     public void setUp() throws Exception {
         conn = DriverManager.getConnection("jdbc:h2:mem:");

         // ??
         Statement stmt = conn.createStatement();
         stmt.executeUpdate("CREATE TABLE SQL_EXECUTOR_TEST (KEY INT PRIMARY KEY, VALUE VARCHAR(20))");
         stmt.close();
         conn.commit();

         // ???
         executed = false;
         count = 0;
     }

     /**
      * ??
      * 
      * <p>H2??</p>
      * 
      * @throws java.lang.Exception ????
      */
     @After
     public void tearDown() throws Exception {
         DbUtils.closeQuietly(conn);
     }

/**
 * ??SQL?????ResultSet????????
 * 
 * @throws Exception ????
 */
@Test
public void test01_??SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   executor.execute(new StringReader("SELECT 1 FROM DUAL;"), new SqlExecutorHandler() {
         
      public void handleResultSet(String sql, ResultSet rs) throws SQLException {
         assertThat(sql, is("SELECT 1 FROM DUAL"));
         assertThat(rs.next(), is(true));
         assertThat(rs.getInt(1), is(1));
            
         executed = true;
         count++;
      }
         
      public void handleUpdateCount(String sql, int count) {
         fail("?????");
      }
         
   });
      
   assertThat(executed, is(true));
   assertThat(count, is(1));
}

/**
 * ?SQL?????ResultSet????????
 * 
 * @throws Exception ????
 */
@Test
public void test02_?SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   executor.execute(new StringReader("SELECT 1 FROM DUAL; SELECT 2 FROM DUAL;"), new SqlExecutorHandler() {
         
      public void handleResultSet(String sql, ResultSet rs) throws SQLException {
         switch (count) {
            case 0:
               assertThat(sql, is("SELECT 1 FROM DUAL"));
               assertThat(rs.next(), is(true));
               assertThat(rs.getInt(1), is(1));
               break;
            case 1:
               assertThat(sql, is("SELECT 2 FROM DUAL"));
               assertThat(rs.next(), is(true));
               assertThat(rs.getInt(1), is(2));
               break;
            default:
               fail("?????????");
         }
            
         executed = true;
         count++;
      }
         
      public void handleUpdateCount(String sql, int count) {
         fail("?????");
      }
   });
      
   assertThat(executed, is(true));
   assertThat(count, is(2));
}

/**
 * ?SQL?????ResultSet????????
 * 
 * @throws Exception ????
 */
@Test
public void test03_?SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   executor.execute(new StringReader("SELECT 'a;b' FROM DUAL; SELECT 'ab' FROM DUAL;"), new SqlExecutorHandler() {
         
      public void handleResultSet(String sql, ResultSet rs) throws SQLException {
         switch (count) {
            case 0:
               assertThat(sql, is("SELECT 'a;b' FROM DUAL"));
               assertThat(rs.next(), is(true));
               assertThat(rs.getString(1), is("a;b"));
               break;
            case 1:
               assertThat(sql, is("SELECT 'ab' FROM DUAL"));
               assertThat(rs.next(), is(true));
               assertThat(rs.getString(1), is("ab"));
               break;
            default:
               fail("?????????");
         }
            
         executed = true;
         count++;
      }
         
      public void handleUpdateCount(String sql, int count) {
         fail("?????");
      }
   });
      
   assertThat(executed, is(true));
   assertThat(count, is(2));
}

/**
 * ?SQL???????
 * 
 * @throws Exception ????
 */
@Test
public void test04_SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   executor.execute("INSERT INTO SQL_EXECUTOR_TEST VALUES (1, 'ab');", new SqlExecutorHandler() {
         
      public void handleResultSet(String sql, ResultSet rs) {
         fail("????");
      }
         
      public void handleUpdateCount(String sql, int count) {
         assertThat(sql, is("INSERT INTO SQL_EXECUTOR_TEST VALUES (1, 'ab')"));
         assertThat(count, is(1));
            
         executed = true;
         SqlExecutorTest.this.count++;
      }
   });
      
   assertThat(executed, is(true));
   assertThat(count, is(1));
}

/**
 * ??SQL??SQL?????
 * 
 * @throws Exception ????
 */
@Test
public void test05_error_SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   try {
      executor.execute(new StringReader("SELECT 1 FROM DUAL; XXX YYY ZZZ; SELECT 2 FROM DUAL;"),
            new SqlExecutorHandler() {
                  
               public void handleResultSet(String sql, ResultSet rs) throws SQLException {
                  switch (count) {
                     case 0:
                        assertThat(sql, is("SELECT 1 FROM DUAL"));
                        assertThat(rs.next(), is(true));
                        assertThat(rs.getInt(1), is(1));
                        break;
                     case 1:
                        assertThat(sql, is("SELECT 2 FROM DUAL"));
                        assertThat(rs.next(), is(true));
                        assertThat(rs.getInt(1), is(2));
                        break;
                     default:
                        fail("?????????");
                  }
                     
                  executed = true;
                  count++;
               }
                  
               public void handleUpdateCount(String sql, int count) {
                  fail("?????");
               }
            });
      fail();
   } catch (SQLException e) {
      // success
   }
      
   assertThat(executed, is(true));
   assertThat(count, is(1));
}

/**
 * ?SQL????????
 * 
 * @throws Exception ????
 */
@Test
public void test06_?SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   executor.execute(new StringReader("SELECT 1 FROM DUAL;\nSELECT 2 FROM DUAL;"), new SqlExecutorHandler() {
         
      public void handleResultSet(String sql, ResultSet rs) throws SQLException {
         switch (count) {
            case 0:
               assertThat(sql, is("SELECT 1 FROM DUAL"));
               assertThat(rs.next(), is(true));
               assertThat(rs.getInt(1), is(1));
               break;
            case 1:
               assertThat(sql, is("SELECT 2 FROM DUAL"));
               assertThat(rs.next(), is(true));
               assertThat(rs.getInt(1), is(2));
               break;
            default:
               fail("?????????");
         }
            
         executed = true;
         count++;
      }
         
      public void handleUpdateCount(String sql, int count) {
         fail("?????");
      }
   });
      
   assertThat(executed, is(true));
   assertThat(count, is(2));
}

/**
 * ? SQL ?????????????
 * 
 * @throws Exception ????
 */
@Test
public void test07_?SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   executor.execute(new StringReader("SELECT 1 FROM DUAL;\n--comment\nSELECT 2 FROM DUAL;"),
         new SqlExecutorHandler() {
               
            public void handleResultSet(String sql, ResultSet rs) throws SQLException {
               switch (count) {
                  case 0:
                     assertThat(sql, is("SELECT 1 FROM DUAL"));
                     assertThat(rs.next(), is(true));
                     assertThat(rs.getInt(1), is(1));
                     break;
                  case 1:
                     assertThat(sql, is("--comment\nSELECT 2 FROM DUAL"));
                     assertThat(rs.next(), is(true));
                     assertThat(rs.getInt(1), is(2));
                     break;
                  default:
                     fail("?????????");
               }
                  
               executed = true;
               count++;
            }
               
            public void handleUpdateCount(String sql, int count) {
               fail("?????");
            }
         });
      
   assertThat(executed, is(true));
   assertThat(count, is(2));
}

/**
 * ? SQL ?????????????
 * 
 * @throws Exception ????
 */
@Test
public void test08_?SQL?() throws Exception {
   SqlExecutor executor = new SqlExecutor(conn);
   executor.execute(new StringReader("SELECT 1 FROM DUAL;\n/*\n * \n */ SELECT 2 FROM DUAL;"),
         new SqlExecutorHandler() {
               
            public void handleResultSet(String sql, ResultSet rs) throws SQLException {
               switch (count) {
                  case 0:
                     assertThat(sql, is("SELECT 1 FROM DUAL"));
                     assertThat(rs.next(), is(true));
                     assertThat(rs.getInt(1), is(1));
                     break;
                  case 1:
                     assertThat(sql, is("/*\n * \n */ SELECT 2 FROM DUAL"));
                     assertThat(rs.next(), is(true));
                     assertThat(rs.getInt(1), is(2));
                     break;
                  default:
                     fail("?????????");
               }
                  
               executed = true;
               count++;
            }
               
            public void handleUpdateCount(String sql, int count) {
               fail("?????");
            }
         });
      
   assertThat(executed, is(true));
   assertThat(count, is(2));
}
 }