Advertisement
Guest User

test

a guest
Dec 17th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. package jdbc.tests;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.io.File;
  6. import java.sql.Connection;
  7. import java.sql.Driver;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.Properties;
  12.  
  13. import org.junit.Assert;
  14. import org.junit.Before;
  15. import org.junit.Test;
  16.  
  17. import consolePortal.IjdbcAdaptor;
  18. import consolePortal.JdbcAdaptor;
  19. import jdbc.ConnectionImp;
  20. /**
  21.  *
  22.  * @author michael.
  23.  *
  24.  */
  25. public class StatementTest {
  26.  
  27.     private String protocol = "xmldb";
  28.     private String tmp = System.getProperty("java.io.tmpdir");
  29.     private Properties info;
  30.     private IjdbcAdaptor adapter;
  31.     private Connection conn;
  32.     private Statement st;
  33.     /**
  34.      * before testing.
  35.      * @throws SQLException
  36.      * if invalid statement.
  37.      */
  38.     @Before
  39.     public final void loadData() throws SQLException {
  40.         info = new Properties();
  41.         info.setProperty("path", "jdbc:jsondb://localhost");
  42.         adapter = new JdbcAdaptor();
  43.         conn = new ConnectionImp(info, adapter);
  44.         st = conn.createStatement();
  45.     }
  46.     /**
  47.      * test execute method.
  48.      */
  49.     @Test
  50.     public final void testExecute() {
  51.         try {
  52.             conn = createUseDatabase("TestDB_Create");
  53.             assertEquals(false, st.execute("CREATE TABLE project("
  54.                     + "columnName1 varchar,"
  55.                     + " columnName2 int,"
  56.                     + " columnName3 date)"));
  57.             st.close();
  58.         } catch (SQLException e) {
  59.             fail("you shouldn't throw execption");
  60.         }
  61.         try {
  62.             conn = createUseDatabase("TestDB_Create");
  63.             st = conn.createStatement();
  64.             st.execute("CREATE TABLE project("
  65.                     + "columnName1 varchar,"
  66.                     + " columnName2 int,"
  67.                     + " columnName3 date)");
  68.             assertEquals(false, st.execute("DROP TABLE project"));
  69.             st.close();
  70.         } catch (SQLException e) {
  71.             fail("you shouldn't throw execption");
  72.         }
  73.         try {
  74.             conn = createUseDatabase("TestDB_Create");
  75.             st = conn.createStatement();
  76.             st.execute("CREATE TABLE project("
  77.                     + "columnName1 varchar,"
  78.                     + " columnName2 int,"
  79.                     + " columnName3 date)");
  80.             assertEquals(false, st.execute(
  81.                     "SELECT * From project"));
  82.             st.close();
  83.         } catch (SQLException e) {
  84.             fail("you shouldn't throw execption");
  85.         }
  86.     }
  87.     /**
  88.      * test execute update.
  89.      */
  90.     @Test
  91.     public final void testExecuteUpdate() {
  92.         try {
  93.             conn = createUseDatabase("TestDB_Create");
  94.             st = conn.createStatement();
  95.             st.execute("CREATE TABLE project(columnName1 varchar,"
  96.                 + " columnName2 int, columnName3 varchar)");
  97.             Assert.assertEquals(1, st.executeUpdate(
  98.                 "INSERT INTO project(columnName1,"
  99.                 + " columnName2, columnName3)"
  100.                 + " VALUES ('go', 4, 'to')"));
  101.             Assert.assertEquals(1, st.executeUpdate(
  102.                 "INSERT INTO project(columnName1,"
  103.                 + " columnName2, columnName3)"
  104.                 + " VALUES ('go', 4, 'to')"));
  105.             Assert.assertEquals(2, st.executeUpdate(
  106.                 "UPDATE project SET columnName1 = '1111111111',"
  107.                 + " columnName2 = 2222222,"
  108.                 + " columnName3='333333333'"));
  109.             st.close();
  110.         } catch (SQLException e) {
  111.             fail("you shouldn't throw execption");
  112.         }
  113.     }
  114.     /**
  115.      * test execute query.
  116.      */
  117.     @Test
  118.     public final void testExecuteQuery() {
  119.         try {
  120.             conn = createUseDatabase("TestDB_Create");
  121.             st = conn.createStatement();
  122.             st.execute("CREATE TABLE project("
  123.                     + "columnName1 varchar,"
  124.                     + " columnName2 int,"
  125.                     + " columnName3 varchar)");
  126.             Assert.assertEquals(1, st.executeUpdate(
  127.                     "INSERT INTO project(columnName1,"
  128.                     + " columnName2, columnName3)"
  129.                     + " VALUES ('go', 4, 'to')"));
  130.             Assert.assertEquals(1, st.executeUpdate(
  131.                     "INSERT INTO project(columnName1,"
  132.                     + " columnName2, columnName3)"
  133.                     + " VALUES ('go', 5, 'to')"));
  134.             Assert.assertEquals(1, st.executeUpdate(
  135.                     "INSERT INTO project(columnName1,"
  136.                    
  137.                     + " columnName2, columnName3)"
  138.                     + " VALUES ('go', 6, 'to')"));
  139.             Assert.assertEquals(1, st.executeUpdate(
  140.                     "INSERT INTO project(columnName1,"
  141.                     + " columnName2, columnName3)"
  142.                     + " VALUES ('goo', 7, 'too')"));
  143.             ResultSet result = st.executeQuery("SELECT * From project");
  144.             Assert.assertNotNull(result);
  145.             Assert.assertEquals(3, result.getMetaData().getColumnCount());
  146.         } catch (SQLException e) {
  147.             fail("you shouldn't throw execption");
  148.         }
  149.     }
  150.  
  151.     private Connection createUseDatabase(String databaseName) throws SQLException {
  152.         Driver driver = (Driver) TestRunner.getImplementationInstance();
  153.         Properties info = new Properties();
  154.         File dbDir = new File(tmp + "/jdbc/" + Math.round((((float) Math.random()) * 100000)));
  155.         info.put("path", dbDir.getAbsoluteFile());
  156.         Connection connection = driver.connect("jdbc:" + protocol + "://localhost", info);
  157.         Statement statement = connection.createStatement();
  158.         statement.execute("CREATE DATABASE " + databaseName);
  159.        
  160.         statement.execute("USE " + databaseName);      
  161.         statement.close();
  162.         return connection;
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement