View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  package com.octo.captcha.engine.bufferedengine.buffer;
13  
14  import org.springframework.beans.factory.xml.XmlBeanFactory;
15  import org.springframework.core.io.ClassPathResource;
16  
17  import javax.sql.DataSource;
18  import java.sql.Connection;
19  import java.sql.ResultSet;
20  import java.sql.SQLException;
21  import java.sql.Statement;
22  
23  /***
24   * <p><ul><li></li></ul></p>
25   *
26   * @author <a href="mailto:marc.antoine.garrigue@gmail.com">Marc-Antoine Garrigue</a>
27   * @version 1.0
28   */
29  public class DatabaseCaptchaBufferTest extends CaptchaBufferTestAbstract {
30  
31      private DataSource datasource;
32      private String CREATE = "CREATE TABLE jcaptcha_t ( timeMillis bigint NULL, hashCode bigint NULL,locale   	varchar(25) NULL,captcha OTHER NULL)";
33      private String EMPTY = "DELETE from jcaptcha_t";
34  
35  
36      public CaptchaBuffer getBuffer() {
37          //just get initialize the database and create
38          //get the datasource from spring conf
39          this.datasource = (DataSource) (new XmlBeanFactory(new ClassPathResource("testDatabaseCaptchaBuffer.xml"))).getBean("dataSource");
40  
41          //drop and recreate the table
42          Connection con = null;
43          Statement ps = null;
44          ResultSet rs = null;
45          try {
46              con = datasource.getConnection();
47              ps = con.createStatement();
48              try {
49  
50                  ps.execute(CREATE);
51  
52              } catch (SQLException e) {
53              }
54  
55  
56              ps = con.createStatement();
57              ps.execute(EMPTY);
58  
59          } catch (SQLException e) {
60  
61              if (rs != null) {
62                  try {
63                      rs.close();
64                  } catch (SQLException ex) {
65                  }
66                  throw new RuntimeException(e);
67              }
68          } finally {
69              if (ps != null) {
70                  try {
71                      ps.close();
72                  } catch (SQLException e) {
73                  }
74              }
75              if (con != null) {
76                  try {
77                      con.close();
78                  } catch (SQLException e) {
79                  }
80              }
81          }
82          DatabaseCaptchaBuffer buffer = new DatabaseCaptchaBuffer(datasource);
83          return buffer;
84      }
85  
86  
87  }