package org.apache.ibatis.submitted.encoding;

import static org.junit.Assert.assertEquals;

import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

public class EncodingTest {

  protected static SqlSessionFactory sqlSessionFactory;

  @BeforeClass
  public static void setUp() throws Exception {
    Connection conn = null;

    try {
      Class.forName("org.hsqldb.jdbcDriver");
      conn = DriverManager.getConnection("jdbc:hsqldb:mem:gname", "sa", "");

      Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/encoding/CreateDB.sql");

      ScriptRunner runner = new ScriptRunner(conn);
      runner.setLogWriter(null);
      runner.setErrorLogWriter(null);
      runner.runScript(reader);
      conn.commit();
      reader.close();

      reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/encoding/EncodingConfig.xml");
      sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
      reader.close();
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }

  @Test
  public void testEncoding1() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
      EncodingMapper mapper = sqlSession.getMapper(EncodingMapper.class);
      String answer = mapper.select1();
      assertEquals("Maran", answer);
    } finally {
      sqlSession.close();
    }
  }

  @Test
  public void testEncoding2() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
      EncodingMapper mapper = sqlSession.getMapper(EncodingMapper.class);
      String answer = mapper.select2();
      assertEquals("Maran", answer);
    } finally {
      sqlSession.close();
    }
  }
}

@@ -1358,32 +1358,34 @@
 %0A    %7D%0A  %7D%0A%0A
+//
   @Test%0A
   public voi
@@ -1364,32 +1364,34 @@
 %0A  %7D%0A%0A//  @Test%0A
+//
   public void te
@@ -1402,24 +1402,26 @@
 coding1() %7B%0A
+//
     SqlSessi
@@ -1461,32 +1461,34 @@
 .openSession();%0A
+//
     try %7B%0A      
@@ -1473,32 +1473,34 @@
 ();%0A//    try %7B%0A
+//
       EncodingMa
@@ -1549,32 +1549,34 @@
 gMapper.class);%0A
+//
       String ans
@@ -1595,24 +1595,26 @@
 .select1();%0A
+//
       assert
@@ -1630,32 +1630,34 @@
 aran%22, answer);%0A
+//
     %7D finally %7B%0A
@@ -1648,32 +1648,34 @@
     %7D finally %7B%0A
+//
       sqlSession
@@ -1680,30 +1680,34 @@
 on.close();%0A
+//
     %7D%0A
+//
   %7D%0A%0A  @Test

build failed. hsqldb problem?