[Jiemamy-notify:2416] commit [3416] SqlExecutor とテストクラスのひな形作成。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 5月 22日 (金) 10:39:14 JST


Revision: 3416
          http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3416
Author:   ewigkeit1204
Date:     2009-05-22 10:39:14 +0900 (Fri, 22 May 2009)

Log Message:
-----------
SqlExecutor とテストクラスのひな形作成。
SqlExecuter はスペルミスのため deprecated にした。

Modified Paths:
--------------
    leto/jiemamy-commons/trunk/pom.xml
    leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecuter.java

Added Paths:
-----------
    leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecutor.java
    leto/jiemamy-commons/trunk/src/test/java/org/jiemamy/utils/SqlExecutorTest.java


-------------- next part --------------
Modified: leto/jiemamy-commons/trunk/pom.xml
===================================================================
--- leto/jiemamy-commons/trunk/pom.xml	2009-05-22 01:00:05 UTC (rev 3415)
+++ leto/jiemamy-commons/trunk/pom.xml	2009-05-22 01:39:14 UTC (rev 3416)
@@ -130,6 +130,12 @@
       <version>1.1</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.h2database</groupId>
+      <artifactId>h2</artifactId>
+      <version>1.1.113</version>
+      <scope>test</scope>
+    </dependency>
      <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>

Modified: leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecuter.java
===================================================================
--- leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecuter.java	2009-05-22 01:00:05 UTC (rev 3415)
+++ leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecuter.java	2009-05-22 01:39:14 UTC (rev 3416)
@@ -31,7 +31,10 @@
  * SQLを実行するクラス。
  * 
  * @author daisuke
+ * @deprecated クラス名のスペルミス。今後は {@link SqlExecutor} を使用してください。
+ * @see SqlExecutor
  */
+ @ Deprecated
 public class SqlExecuter {
 	
 	private static Logger logger = LoggerFactory.getLogger(SqlExecuter.class);

Added: leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecutor.java
===================================================================
--- leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecutor.java	                        (rev 0)
+++ leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecutor.java	2009-05-22 01:39:14 UTC (rev 3416)
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2007-2009 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;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.apache.commons.lang.Validate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * SQLを実行するクラス。
+ * 
+ * @author Keisuke.K
+ */
+public class SqlExecutor {
+	
+	private static Logger logger = LoggerFactory.getLogger(SqlExecutor.class);
+	
+	private final Connection connection;
+	
+
+	/**
+	 * インスタンスを生成する。
+	 * 
+	 * @param connection コネクション
+	 */
+	public SqlExecutor(Connection connection) {
+		Validate.notNull(connection);
+		this.connection = connection;
+	}
+	
+	/**
+	 * SQLを実行する。
+	 * 
+	 * @param sql 実行するSQL
+	 * @return 結果の{@link ResultSet}
+	 * @throws SQLException SQLの実行に失敗した場合
+	 */
+	public ResultSet execute(String sql) throws SQLException {
+		logger.info(sql);
+		
+		connection.setAutoCommit(false);
+		
+		Statement statement = null;
+		ResultSet resultSet = null;
+		try {
+			statement = connection.createStatement();
+			if (statement.execute(sql)) {
+				resultSet = statement.getResultSet();
+			}
+			connection.commit();
+		} finally {
+			JmIOUtil.closeQuietly(statement);
+		}
+		return resultSet;
+	}
+	
+}


Property changes on: leto/jiemamy-commons/trunk/src/main/java/org/jiemamy/utils/SqlExecutor.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: leto/jiemamy-commons/trunk/src/test/java/org/jiemamy/utils/SqlExecutorTest.java
===================================================================
--- leto/jiemamy-commons/trunk/src/test/java/org/jiemamy/utils/SqlExecutorTest.java	                        (rev 0)
+++ leto/jiemamy-commons/trunk/src/test/java/org/jiemamy/utils/SqlExecutorTest.java	2009-05-22 01:39:14 UTC (rev 3416)
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2007-2009 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;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+
+import org.h2.Driver;
+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 Driver());
+	}
+	
+
+	private Connection conn;
+	
+
+	/**
+	 * テストの初期化。
+	 * 
+	 * <p>
+	 * H2コネクションを取得する。
+	 * </p>
+	 * 
+	 * @throws java.lang.Exception 例外が発生した場合
+	 */
+	@Before
+	public void setUp() throws Exception {
+		conn = DriverManager.getConnection("jdbc:h2:test", "sa", "");
+	}
+	
+	/**
+	 * テストの終了処理。
+	 * 
+	 * <p>
+	 * H2コネクションをクローズする。
+	 * </p>
+	 * 
+	 * @throws java.lang.Exception 例外が発生した場合
+	 */
+	@After
+	public void tearDown() throws Exception {
+		JmIOUtil.closeQuietly(conn);
+	}
+	
+	/**
+	 * 単純なSQLを実行し、正しくResultSetが取得できるかを確認する。
+	 * 
+	 * @throws Exception 例外が発生した場合
+	 */
+	@Test
+	public void test01_単純なSQLの実行() throws Exception {
+		SqlExecutor executor = new SqlExecutor(conn);
+		ResultSet rs = null;
+		
+		try {
+			rs = executor.execute("SELECT * FROM DUAL");
+			
+			assertThat(rs, is(notNullValue()));
+		} finally {
+			JmIOUtil.closeQuietly(rs);
+		}
+	}
+}


Property changes on: leto/jiemamy-commons/trunk/src/test/java/org/jiemamy/utils/SqlExecutorTest.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain



Jiemamy-notify メーリングリストの案内
Back to archive index