package org.jboss.soa.clustering.teiid; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.io.FileUtils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @Test(enabled = true) public class TeiidClusteringTest { private static final String VDB_NAME = "cluster"; private static final String USERNAME = "admin"; private static final String PASSWD = "teiid"; private static final String DRIVER = "org.teiid.jdbc.TeiidDriver"; private Connection c; private Connection dbConn; private List expectedResults; @BeforeMethod public void setUp() throws Exception { dbConn = getDatabaseConnection(); init(); initExpectedResults(); } @Test(enabled = true) public void testFailover() throws Exception { this.c = getTeiidConnection(); while ( true ) { try { select(); System.out.println(); Thread.sleep(1000); } catch (InterruptedException ie) { // nothing } } } @AfterMethod public void tearDown() throws Exception { dropTable("names"); closeQuietly(dbConn); closeQuietly(c); } private Connection getTeiidConnection() throws Exception { Class.forName(DRIVER); String url = "jdbc:teiid:"+ VDB_NAME +"@mm://10.16.90.18:31000,10.16.90.18:31001;autoFailover=true"; return DriverManager.getConnection(url, USERNAME, PASSWD); } private Connection getDatabaseConnection() throws Exception { Class.forName(System.getProperty("test.db.driver")); String url = System.getProperty("test.db.url"); return DriverManager.getConnection(url, System.getProperty("test.db.username"), System.getProperty("test.db.password")); } private void insert() throws Exception { String insert = "INSERT INTO names (id, name, surname) VALUES (?, ? , ?)"; PreparedStatement pstmt = dbConn.prepareStatement(insert); List names = new ArrayList(); names.add( Name.create(new Long(0), "John", "Doe") ); names.add( Name.create(new Long(1), "Kelly", "Smith") ); names.add( Name.create(new Long(2), "Peter", "Clark")); names.add( Name.create(new Long(3), "Jack", "Void")); for (Name name : names) { pstmt.setLong(1, name.getId()); pstmt.setString(2, name.getName()); pstmt.setString(3, name.getSurname()); pstmt.execute(); } } private void delete(String name) throws Exception { String delete = "DELETE FROM names WHERE name = '"+ name +"'"; dbConn.createStatement().executeUpdate(delete); expectedResults.remove(name); // cl1ExpectedResults.remove(0); // cl2ExpectedResults.remove(0); } private void dropTable(String tableName) throws Exception { String delete = "DROP TABLE "+ tableName; dbConn.createStatement().executeUpdate(delete); } private void select() { List results = new ArrayList(); try { String select = "SELECT name, CLUSTER_NAME FROM ClusterTable OPTION NOCACHE"; Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery(select); String name = null; String txtName = null; while (rs.next()) { name = rs.getString("name"); txtName = rs.getString("CLUSTER_NAME"); results.add(name); results.add(txtName); System.out.println("@@ name=" + name + ", CLUSTER_NAME="+ txtName); } stmt.close(); } catch (Exception e) { System.out.println("@@ Message: " + e.getMessage()); e.printStackTrace(); } Assert.assertTrue(results.equals(expectedResults)); results.clear(); } private void deploy(String configName) throws Exception { String serverHome = System.getProperty("org.jboss.esb.server.home"); String vdb = new File( System.getProperty("vdb.path") ).getAbsolutePath(); File f = new File(serverHome, "server/"+ configName + "/deploy"); System.out.println("Deploying to: "+ f.getAbsolutePath()); deployDs(configName, "mysql-ds.xml"); deployDs(configName, "cluster-ds.xml"); FileUtils.copyFileToDirectory(new File(vdb), f); Thread.sleep(5000); } private void undeploy(String configName) throws Exception { String serverHome = System.getProperty("org.jboss.esb.server.home"); String vdbPath = serverHome + "/server/" + configName + "/deploy/cluster.vdb"; File f = new File(vdbPath); //undeployDs(configName, "mysql-ds.xml"); //undeployDs(configName, "cluster-ds.xml"); System.out.println("Undeploying VDB from: "+ f.getAbsolutePath()); FileUtils.deleteQuietly( f ); undeployDs(configName, "mysql-ds.xml"); undeployDs(configName, "cluster-ds.xml"); Thread.sleep(5000); } private void readData() throws Exception { List dBResults = new ArrayList(); List txtResults = new ArrayList(); // this.c = null; // c = getTeiidConnection(); Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery("SELECT name, CLUSTER_NAME FROM ClusterTable OPTION NOCACHE"); String clusterName = null; while (rs.next()) { String name = rs.getString("name"); clusterName = rs.getString("CLUSTER_NAME"); dBResults.add(name); txtResults.add(clusterName); System.out.println("@@readData() name=" + name + ", CLUSTER_NAME="+ clusterName); } stmt.close(); // c.close(); // determineServer(clusterName); System.out.println("dbResults="+ dBResults + ", expectedResults="+ expectedResults); // System.out.println("txtResults="+ txtResults + ", determinedResults="+ determineResultList(clusterName)); Assert.assertTrue(dBResults.equals(expectedResults)); // Assert.assertTrue(txtResults.equals(determineResultList(clusterName))); dBResults.clear(); txtResults.clear(); } private void readData(List expectedTxtResults) throws Exception { List dBResults = new ArrayList(); List txtResults = new ArrayList(); // this.c = null; // c = getTeiidConnection(); Statement stmt = c.createStatement(); System.out.println("@@Time="+SimpleDateFormat.getDateTimeInstance().format( new Date())); ResultSet rs = stmt.executeQuery("SELECT name, CLUSTER_NAME FROM ClusterTable OPTION NOCACHE"); String clusterName = null; while (rs.next()) { String name = rs.getString("name"); clusterName = rs.getString("CLUSTER_NAME"); dBResults.add(name); txtResults.add(clusterName); System.out.println("@@readData(List): name=" + name + ", CLUSTER_NAME="+ clusterName); } stmt.close(); // c.close(); // determineServer(clusterName); System.out.println("results="+ dBResults + ", expectedResults="+ expectedResults); System.out.println("txtResults="+ txtResults + ", determinedResults="+ expectedTxtResults); Assert.assertTrue(dBResults.equals(expectedResults)); Assert.assertTrue(txtResults.equals(expectedTxtResults)); dBResults.clear(); txtResults.clear(); } private void init() throws Exception { String filePath = System.getProperty("mapping.file.path"); Configuration config = getHibernateConfiguration(new FileInputStream [] { new FileInputStream(filePath) }); SessionFactory sf = config.buildSessionFactory(); Session sess = null; Transaction tx = null; try { sess = sf.openSession(); tx = sess.beginTransaction(); sess.save(Name.create( new Long(0), "John", "Doe") ); sess.save(Name.create( new Long(1), "Kelly", "Smith") ); sess.save(Name.create( new Long(2), "Peter", "Clark") ); sess.save(Name.create( new Long(3), "Jack", "Void") ); tx.commit(); } catch (Exception e) { if (tx != null && tx.isActive()) { tx.rollback(); } } finally { sess.close(); } } private Configuration getHibernateConfiguration(InputStream [] bindings) throws Exception { Configuration cfg = new Configuration(); // static cfg.setProperty("hibernate.current_session_context_class", "thread"); cfg.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider"); cfg.setProperty("hibernate.show_sql", "true"); cfg.setProperty("hibernate.hbm2ddl.auto", "create"); // dynamic cfg.setProperty("hibernate.dialect", System.getProperty("test.db.hibernate.dialect")); cfg.setProperty("hibernate.connection.driver_class", System.getProperty("test.db.driver")); cfg.setProperty("hibernate.connection.url", System.getProperty("test.db.url")); cfg.setProperty("hibernate.connection.username", System.getProperty("test.db.username")); cfg.setProperty("hibernate.connection.password", System.getProperty("test.db.password")); // mappings for (InputStream is: bindings) { cfg.addInputStream(is); } return cfg; } private void closeQuietly(Connection conn) throws Exception { try { if (conn != null) { conn.close(); } } catch (Exception e) { System.out.println("@@ Error closing connection.."); } } private void initExpectedResults() { this.expectedResults = new ArrayList(); expectedResults.add("John"); expectedResults.add("cluster1_0"); expectedResults.add("Kelly"); expectedResults.add("cluster1_1"); expectedResults.add("Peter"); expectedResults.add("cluster1_2"); expectedResults.add("Jack"); expectedResults.add("cluster1_3"); } private void undeployDs(String configName, String dsName) throws Exception { String serverHome = System.getProperty("org.jboss.esb.server.home"); File dsFile = new File(serverHome, "/server/"+ configName + "/deploy/"+ dsName); File newFile = new File(serverHome, "/server/"+ configName ); System.out.println("@@ undeployDs: moving " + dsFile.getAbsolutePath() + " to " + newFile.getAbsolutePath()); FileUtils.moveFileToDirectory(dsFile, newFile, false); } private void deployDs(String configName, String dsName) throws Exception { String serverHome = System.getProperty("org.jboss.esb.server.home"); File dsFile = new File(serverHome, "/server/"+ configName + "/"+ dsName); File newFile = new File(serverHome, "/server/"+ configName + "/deploy"); System.out.println("@@ deployDs: moving: " + dsFile.getAbsoluteFile() + " to " + newFile.getAbsoluteFile()); FileUtils.moveFileToDirectory(dsFile, newFile, false); } }