Package util

Source Code of util.UnivPrepStmt

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: UnivPrepStmt.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.3  2005/04/22 15:11:07  drmlipp
* Merged changes from 1.3 branch up to 1.3p15.
*
* Revision 1.1.1.2.6.2  2005/04/13 16:14:08  drmlipp
* Optimized db access.
*
* Revision 1.2  2005/02/23 15:43:37  drmlipp
* Synchronized with 1.3.
*
* Revision 1.1.1.2.6.1  2005/02/16 16:17:51  drmlipp
* Fixed SQL statement.
*
* Revision 1.1.1.2  2004/08/18 15:18:47  drmlipp
* Update to 1.2
*
* Revision 1.6  2004/01/27 11:45:33  lipp
* Preserve newlines when reading process definitions.
*
* Revision 1.5  2003/06/27 09:44:13  lipp
* Fixed copyright/license information.
*
* Revision 1.4  2003/03/06 11:03:51  huaiyang
* clean up the data after the test.
*
* Revision 1.3  2003/03/05 08:21:53  huaiyang
* do not use autocommit.
*
* Revision 1.2  2003/03/03 15:49:16  huaiyang
* isOracle must be static.
*
* Revision 1.1  2003/02/28 15:29:30  huaiyang
* Initial.
*
*
*/
package util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.HashMap;
import java.util.Properties;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import de.danet.an.util.JDBCUtil;
import de.danet.an.util.UniversalPrepStmt;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Tests the UniversalPrepStmt
* @version 1.0
*/
public class UnivPrepStmt extends TestCase {
    /**
     * Properties to use in test cases
     */
    private static Properties props = new Properties();
    /**
     * Default connection used in test cases
     */
    private static Connection con = null;
    /**
     * Name of DB product
     */
    private static String dbDriver = null;
   
    /** flag of oracle database */
    private static boolean isOracle = false;

    /**
     * Konstruktor zum Erzeugen eines TestCase
     * @param name a <code>String</code> value
     */
    public UnivPrepStmt(String name) {
  super (name);
    }

    /**
     * Assembling the test suite
     * @return a <code>Test</code> value
     * @exception Exception if an error occurs
     */
    public static Test suite() throws Exception {
        TestSuite suite = new TestSuite();
  // test case for oracle
     suite.addTest(new UnivPrepStmt("setOracleConnection"));
     suite.addTest(new UnivPrepStmt("insertLobs"));
  suite.addTest(new UnivPrepStmt("updateLobs"));
  // test case for sapdb
      suite.addTest(new UnivPrepStmt("setSAPDBConnection"));
      suite.addTest(new UnivPrepStmt("insertLobs"));
      suite.addTest(new UnivPrepStmt("updateLobs"));
        return suite;
    }
 
    public void insertLobs() throws Exception {
  String xpdl = importXPDL("test1.xml");
  Object header = new HashMap();
  UniversalPrepStmt prepStmt = null;
  try {
      // clean up
      assertTrue(cleanup() == 0);
      //con.commit();
      // insert
      prepStmt = new UniversalPrepStmt
    (con, "INSERT INTO ProcessDefinition ("
     + "DBId, PackageId, ProcessId, Xpdl, Header, Enabled) "
     + "VALUES (?, ?, ?, ?, ?, 'T')");
      int offset = 1;
      prepStmt.setLong (offset++, 1000000);
      prepStmt.setString(offset++, "univPrepStmtPackage");
      prepStmt.setString(offset++, "univPrepStmtTest");
      /*
      prepStmt.setLargeString(offset++, null);
        prepStmt.setBinary(offset++, null);
       prepStmt.setLargeString(offset++, xpdl);
      prepStmt.setBinary(offset++, header);
      */
      prepStmt.setLargeString(offset++, null);
        prepStmt.setBinary(offset++, null);
      prepStmt.executeUpdate();
      //con.commit();
      // select
      offset = 1;
      prepStmt = new UniversalPrepStmt
    (con, "SELECT Header, Xpdl FROM ProcessDefinition "
     + "WHERE DBId = 1000000");
      ResultSet rs = prepStmt.executeQuery();
      rs.next();
      if (!isOracle) {
    return;
      }
      Object blobObject = JDBCUtil.getBinary(rs, offset++);
      assertTrue(rs.wasNull());
      if (rs.wasNull()) {
    System.out.println("Got null BLOB!");
      }
      String clobObject = JDBCUtil.getString(null, rs, offset++);
      assertTrue(rs.wasNull());
      if (rs.wasNull()) {
    System.out.println("Got null CLOB!");
      }
   } catch (SQLException sqle) {
      sqle.printStackTrace();
  } finally {
      //JDBCUtil.closeAll (null, prepStmt, con);
  }
    }

    public void updateLobs() throws Exception {
  String xpdl = importXPDL("test2.xml");
  Object header = new HashMap();
  UniversalPrepStmt prepStmt = null;
  try {
      prepStmt = new UniversalPrepStmt
    (con, "UPDATE ProcessDefinition SET "
     + "PackageId = ?, ProcessId = ?, Xpdl = ?, Header = ? "
     + "WHERE DBId = ?");
      int offset = 1;
      prepStmt.setString(offset++, "updatedUnivPrepStmtPackage");
      prepStmt.setString(offset++, "updatedunivPrepStmtTest");
      prepStmt.setLargeString(offset++, xpdl);
      prepStmt.setBinary(offset++, header);
      prepStmt.setLong (offset++, 1000000);
      //prepStmt.setString(offset++, "univPrepStmtPackage");
      prepStmt.executeUpdate();
      //con.commit();
      assertTrue(cleanup() == 1);
   } catch (SQLException sqle) {
      sqle.printStackTrace();
  } finally {
      JDBCUtil.closeAll (null, prepStmt, con);
  }
    }

    /**
     * Test setting of connections
     * @exception Exception if an error occurs
     */
    public void setOracleConnection() throws Exception {
  isOracle = true;
  setUpConnection("jdbc-oracle.properties");
    }

    /**
     * Test setting of connections
     * @exception Exception if an error occurs
     */
    public void setSAPDBConnection() throws Exception {
  isOracle = false;
  setUpConnection("jdbc-sapdb.properties");
    }
   
    private void setUpConnection(String properties) throws Exception {
  try {
      props.load(Thread.currentThread().getContextClassLoader()
           .getResourceAsStream(properties));
  } catch (NullPointerException exc) {
      props.load(Thread.currentThread().getContextClassLoader()
           .getResourceAsStream("jdbc.properties"));
  }
  // register oracle database driver
  Class.forName(props.getProperty("driver"));
  con = DriverManager.getConnection
      (props.getProperty("database"), props.getProperty("user"),
       props.getProperty("password"));
  System.out.println("Driver: " + con.getMetaData().getDriverName());
  //con.setAutoCommit(false);
    }

    private String importXPDL(String filename) throws Exception {
  InputStream is = getClass().getResourceAsStream(filename);
  assertTrue (is != null);
  BufferedReader br = new BufferedReader
      (new InputStreamReader(is, "ISO-8859-1"));
  StringBuffer sb = new StringBuffer();
  String st;
  while ((st = br.readLine()) != null) {
      sb.append(st + "\n");
  }
  return sb.toString();
    }

    private int cleanup() throws Exception {
  UniversalPrepStmt deletePrepStmt = new UniversalPrepStmt
      (con, "DELETE FROM ProcessDefinition WHERE"
       + " DBId = 1000000");
  return deletePrepStmt.executeUpdate();
    }

}
TOP

Related Classes of util.UnivPrepStmt

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.