Added:
trunk/connector-mxj/src/InitializeUserExample.java
trunk/connector-mxj/src/com/mysql/management/util/InitializeUser.java
trunk/connector-mxj/src/com/mysql/management/util/QueryUtil.java
trunk/connector-mxj/src/com/mysql/management/util/SQLRuntimeException.java
Removed:
trunk/connector-mxj/src/InitializePasswordExample.java
Modified:
trunk/connector-mxj/CHANGES
trunk/connector-mxj/src/ConnectorMXJObjectTestExample.java
trunk/connector-mxj/src/ConnectorMXJUrlTestExample.java
trunk/connector-mxj/src/ShowUsers.java
trunk/connector-mxj/src/TestDb.java
trunk/connector-mxj/src/com/mysql/management/AcceptanceTest.java
trunk/connector-mxj/src/com/mysql/management/MysqldResource.java
trunk/connector-mxj/src/com/mysql/management/MysqldResourceI.java
trunk/connector-mxj/src/com/mysql/management/driverlaunched/AcceptanceTest.java
trunk/connector-mxj/src/com/mysql/management/driverlaunched/MysqldResourceNotFoundException.java
trunk/connector-mxj/src/com/mysql/management/driverlaunched/ServerLauncherSocketFactory.java
trunk/connector-mxj/src/com/mysql/management/jmx/AllTestsSuite.java
trunk/connector-mxj/src/com/mysql/management/jmx/TestFactory.java
trunk/connector-mxj/src/com/mysql/management/jmx/jboss/AllTestsSuite.java
trunk/connector-mxj/src/com/mysql/management/util/AllTestsSuite.java
trunk/connector-mxj/src/com/mysql/management/util/NotImplementedException.java
trunk/connector-mxj/src/com/mysql/management/util/ProcessUtilTest.java
trunk/connector-mxj/src/com/mysql/management/util/QuietTestCase.java
trunk/connector-mxj/src/com/mysql/management/util/RuntimeI.java
trunk/connector-mxj/src/com/mysql/management/util/RuntimeTest.java
trunk/connector-mxj/src/com/mysql/management/util/Str.java
trunk/connector-mxj/src/com/mysql/management/util/TestUtil.java
trunk/connector-mxj/src/com/mysql/management/util/Utils.java
Log:
2007-05-25 - added new connection property "initialize-user" which, if set to
"true" will remove the default, unpassworded anonymous and "root"
users, create the user/password from the connection url
- added new tests for initial-user & expanded some existing tests
- added InitializeUser and QueryUtil classes to support new feature
- removed InitializePasswordExample
- added InitializeUserExample
- refactored duplication from tests and examples to QueryUtil
- removed PatchedStandardSocketFactory (fixed in Connetor/J 5.0.6)
- added copyright notices to some classes which were missing them
Modified: trunk/connector-mxj/CHANGES
===================================================================
--- trunk/connector-mxj/CHANGES 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/CHANGES 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,6 +1,17 @@
# Changelog
# $Id: CHANGES,v 1.14 2005/10/25 19:11:16 eherman Exp $
+2007-05-25 - added new connection property "initialize-user" which, if set to
+ "true" will remove the default, unpassworded anonymous and "root"
+ users, create the user/password from the connection url
+ - added new tests for initial-user & expanded some existing tests
+ - added InitializeUser and QueryUtil classes to support new feature
+ - removed InitializePasswordExample
+ - added InitializeUserExample
+ - refactored duplication from tests and examples to QueryUtil
+ - removed PatchedStandardSocketFactory (fixed in Connetor/J 5.0.6)
+ - added copyright notices to some classes which were missing them
+
2007-05-21 - added robustness around reading portfile
2007-05-18 - upadted commercial license files
Modified: trunk/connector-mxj/src/ConnectorMXJObjectTestExample.java
===================================================================
--- trunk/connector-mxj/src/ConnectorMXJObjectTestExample.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/ConnectorMXJObjectTestExample.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,12 +1,28 @@
+/*
+ Copyright (C) 2004 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import com.mysql.management.MysqldResource;
+import com.mysql.management.util.QueryUtil;
public class ConnectorMXJObjectTestExample {
public static String DRIVER = "com.mysql.jdbc.Driver";
@@ -28,7 +44,14 @@
try {
String url = "jdbc:mysql://localhost:" + port + "/test";
conn = DriverManager.getConnection(url, userName, password);
- printQueryResults(conn, "SELECT VERSION()");
+ String sql = "SELECT VERSION()";
+ System.out.println("------------------------");
+ System.out.println(sql);
+ System.out.println("------------------------");
+ new QueryUtil(conn).printQueryResults(sql, System.out);
+ System.out.println("------------------------");
+ System.out.flush();
+ Thread.sleep(100); // wait for System.out to finish flush
} finally {
try {
if (conn != null) {
@@ -60,24 +83,4 @@
return mysqldResource;
}
-
- public static void printQueryResults(Connection conn, String SQLquery)
- throws Exception {
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(SQLquery);
- int columns = rs.getMetaData().getColumnCount();
- System.out.println("------------------------");
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= columns; i++) {
- System.out.println(rs.getString(i));
- }
- System.out.println();
- }
- rs.close();
- stmt.close();
- System.out.println("------------------------");
- System.out.flush();
- Thread.sleep(100); // wait for System.out to finish flush
- }
}
Modified: trunk/connector-mxj/src/ConnectorMXJUrlTestExample.java
===================================================================
--- trunk/connector-mxj/src/ConnectorMXJUrlTestExample.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/ConnectorMXJUrlTestExample.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,10 +1,27 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
+
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.Statement;
import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;
+import com.mysql.management.util.QueryUtil;
public class ConnectorMXJUrlTestExample {
public static String DRIVER = "com.mysql.jdbc.Driver";
@@ -28,7 +45,14 @@
Connection conn = null;
try {
conn = DriverManager.getConnection(url, userName, password);
- printQueryResults(conn, "SELECT VERSION()");
+ String sql = "SELECT VERSION()";
+ System.out.println("------------------------");
+ System.out.println(sql);
+ System.out.println("------------------------");
+ new QueryUtil(conn).printQueryResults(sql, System.out);
+ System.out.println("------------------------");
+ System.out.flush();
+ Thread.sleep(100); // wait for System.out to finish flush
} finally {
try {
if (conn != null)
@@ -40,24 +64,4 @@
ServerLauncherSocketFactory.shutdown(databaseDir, null);
}
}
-
- public static void printQueryResults(Connection conn, String SQLquery)
- throws Exception {
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(SQLquery);
- int columns = rs.getMetaData().getColumnCount();
- System.out.println("------------------------");
- System.out.println();
- while (rs.next()) {
- for (int i = 1; i <= columns; i++) {
- System.out.println(rs.getString(i));
- }
- System.out.println();
- }
- rs.close();
- stmt.close();
- System.out.println("------------------------");
- System.out.flush();
- Thread.sleep(100); // wait for System.out to finish flush
- }
}
Deleted: trunk/connector-mxj/src/InitializePasswordExample.java
===================================================================
--- trunk/connector-mxj/src/InitializePasswordExample.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/InitializePasswordExample.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,130 +0,0 @@
-import java.io.File;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-import com.mysql.jdbc.MysqlErrorNumbers;
-import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;
-import com.mysql.management.util.Files;
-
-public class InitializePasswordExample {
- public static String DRIVER = "com.mysql.jdbc.Driver";
-
- public static String JAVA_IO_TMPDIR = "java.io.tmpdir";
-
- public static void main(String[] args) throws Exception {
- File ourAppDir = new File(System.getProperty(JAVA_IO_TMPDIR));
- File databaseDir = new File(ourAppDir, "test-mxj");
- new Files().deleteTree(databaseDir);
-
- int port = 3336;
- String url = "jdbc:mysql:mxj://localhost:" + port + "/test" //
- + "?" + "server.basedir=" + databaseDir;
-
- System.out.println(url);
-
- go(databaseDir, url);
- go(databaseDir, url);
- }
-
- private static void go(File databaseDir, String url) throws Exception {
- try {
- InitializePasswordExample example = new InitializePasswordExample(
- url, "secret");
- boolean setPassword = example.initializeRootPassword();
- System.out.println("Set password: " + setPassword);
- System.out.println(example.queryForString("SELECT VERSION()"));
- setPassword = example.initializeRootPassword();
- System.out.println("Set password: " + setPassword);
- System.out.println(example.queryForString("SELECT VERSION()"));
- setPassword = example.initializeRootPassword();
- System.out.println("Set password: " + setPassword);
- System.out.println(example.queryForString("SELECT VERSION()"));
- } finally {
- ServerLauncherSocketFactory.shutdown(databaseDir, null);
- }
- }
-
- private String userName;
-
- private String password;
-
- private String url;
-
- public InitializePasswordExample(String url, String rootPassWord)
- throws Exception {
- this.url = url;
- this.userName = "root";
- this.password = rootPassWord;
- Class.forName(DRIVER);
- }
-
- public boolean initializeRootPassword() {
- final String sql = "SET PASSWORD FOR root@localhost=PASSWORD(?)";
- String NO_PASSWORD = null;
- Connection conn = null;
- PreparedStatement psmt = null;
- try {
- conn = DriverManager.getConnection(url, userName, NO_PASSWORD);
- psmt = conn.prepareStatement(sql);
- psmt.setObject(1, password);
- psmt.execute();
- psmt.close();
- return true;
- } catch (SQLException e) {
- if (e.getErrorCode() != MysqlErrorNumbers.ER_ACCESS_DENIED_ERROR) {
- throw new RuntimeException(e);
- }
- } finally {
- close(conn, psmt, null);
- }
- return false;
- }
-
- public String queryForString(String SQLquery) {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- conn = DriverManager.getConnection(url, userName, password);
- stmt = conn.createStatement();
- rs = stmt.executeQuery(SQLquery);
- boolean rowReturned = rs.next();
- if (rowReturned) {
- return rs.getString(1);
- }
- return null;
- } catch (SQLException e) {
- throw new RuntimeException(e);
- } finally {
- close(conn, stmt, rs);
- }
- }
-
- private void close(Connection conn, Statement stmt, ResultSet rs) {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
-}
Added: trunk/connector-mxj/src/InitializeUserExample.java
===================================================================
--- trunk/connector-mxj/src/InitializeUserExample.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/InitializeUserExample.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -0,0 +1,118 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
+
+import java.io.File;
+import java.io.PrintStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;
+import com.mysql.management.util.Files;
+import com.mysql.management.util.QueryUtil;
+import com.mysql.management.util.SQLRuntimeException;
+
+/**
+ * This class demonstrates that the user InitializeUser utility may be called
+ * again, and again and behaves as expected to only initialize the user the very
+ * first time, even between mysqld restarts.
+ */
+public class InitializeUserExample {
+ public static String DRIVER = "com.mysql.jdbc.Driver";
+
+ public static String JAVA_IO_TMPDIR = "java.io.tmpdir";
+
+ private String userName;
+
+ private String password;
+
+ private String url;
+
+ public InitializeUserExample(String url, String userName, String password) {
+ this.url = url;
+ this.userName = userName;
+ this.password = password;
+ }
+
+ private void go(File databaseDir, PrintStream out) throws Exception {
+ new Files().deleteTree(databaseDir);
+ try {
+ out.println("URL: " + url);
+ out.println("User: " + userName);
+ out.println("password: " + password);
+ for (int i = 0; i < 2; i++)
+ try {
+ String sql = "SELECT VERSION()";
+ out.println(sql + ":" + connectAndQueryForString(sql));
+ out.println(sql + ":" + connectAndQueryForString(sql));
+ out.println(sql + ":" + connectAndQueryForString(sql));
+ } finally {
+ ServerLauncherSocketFactory.shutdown(databaseDir, null);
+ }
+ } finally {
+ new Files().deleteTree(databaseDir);
+ }
+ }
+
+ public String connectAndQueryForString(String SQLquery) {
+ Connection conn = null;
+ try {
+ conn = DriverManager.getConnection(url, userName, password);
+ QueryUtil util = new QueryUtil(conn);
+ return util.queryForString(SQLquery);
+ } catch (SQLException e) {
+ throw new SQLRuntimeException(e);
+ } finally {
+ close(conn);
+ }
+ }
+
+ private void close(Connection conn) {
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ }
+ }
+
+ // ------------------------------------------------------------
+
+ public static void main(String[] args) throws Exception {
+ File ourAppDir = new File(System.getProperty(JAVA_IO_TMPDIR));
+ File databaseDir = new File(ourAppDir, "test-mxj");
+ int port = 3336;
+
+ Class.forName(DRIVER);
+
+ String url = "jdbc:mysql:mxj://localhost:" + port + "/foo_bar" //
+ + "?" + "server.basedir=" + databaseDir //
+ + "&" + "createDatabaseIfNotExist=true"//
+ + "&" + "server.initialize-user=true" //
+ ;
+
+ InitializeUserExample exampleApp;
+
+ exampleApp = new InitializeUserExample(url, "root", "p4ssw0rd");
+ exampleApp.go(databaseDir, System.out);
+
+ exampleApp = new InitializeUserExample(url, "bob", "sEcr3t");
+ exampleApp.go(databaseDir, System.out);
+ }
+}
Modified: trunk/connector-mxj/src/ShowUsers.java
===================================================================
--- trunk/connector-mxj/src/ShowUsers.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/ShowUsers.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,21 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
+
import java.io.File;
import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;
Modified: trunk/connector-mxj/src/TestDb.java
===================================================================
--- trunk/connector-mxj/src/TestDb.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/TestDb.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,21 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
+
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -52,7 +70,9 @@
printResults(out, rs, printMetaData);
}
} finally {
- cleanUp(conn, stmt, rs);
+ if (conn != null) {
+ conn.close();
+ }
}
}
@@ -80,25 +100,6 @@
out.println();
}
- private void cleanUp(Connection conn, Statement stmt, ResultSet rs)
- throws SQLException {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- conn.close();
- }
-
public static void main(String[] args) throws Exception {
String driver = (args.length > 0) ? args[0] : "com.mysql.jdbc.Driver";
String url = (args.length > 1) ? args[1] : "jdbc:mysql:///test";
Modified: trunk/connector-mxj/src/com/mysql/management/AcceptanceTest.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/AcceptanceTest.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/AcceptanceTest.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -20,14 +20,13 @@
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
-import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.mysql.management.util.Files;
+import com.mysql.management.util.QueryUtil;
import com.mysql.management.util.QuietTestCase;
import com.mysql.management.util.Str;
import com.mysql.management.util.TestUtil;
@@ -40,10 +39,6 @@
private Connection conn = null;
- private Statement stmt = null;
-
- private ResultSet rs = null;
-
private File tmpDir;
private MysqldResourceI mysqld;
@@ -58,20 +53,6 @@
protected void tearDown() {
super.tearDown();
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
if (conn != null) {
try {
@@ -180,19 +161,11 @@
checkVersion(mysqld.getVersion());
}
- private void checkVersion(String version) throws SQLException {
- stmt = conn.createStatement();
- rs = stmt.executeQuery("SELECT VERSION()");
- int cols = rs.getMetaData().getColumnCount();
- assertTrue(rs.next());
- assertTrue(cols >= 1);
- String searchIn = rs.getString(1);
+ private void checkVersion(String version) {
+ QueryUtil util = new QueryUtil(conn);
+ String searchIn = util.queryForString("SELECT VERSION()");
assertTrue("<" + version + "> not found in <" + searchIn + ">",
new Str().containsIgnoreCase(searchIn, version));
- assertEquals(cols, 1);
- assertFalse(rs.next());
- rs.close();
- stmt.close();
}
private void makeDb(String url, String userName, String password)
@@ -200,20 +173,78 @@
new TestUtil().assertConnectViaJDBC(url, userName, password, true);
Class.forName(com.mysql.jdbc.Driver.class.getName());
conn = DriverManager.getConnection(url, userName, password);
- stmt = conn.createStatement();
- stmt.execute("CREATE DATABASE MY1");
- stmt.execute("USE MY1");
+ QueryUtil util = new QueryUtil(conn);
+ util.execute("CREATE DATABASE MY1");
+ util.execute("USE MY1");
String sql = "GRANT ALL PRIVILEGES ON MY1.*"
+ " TO 'JAVA'@'%' IDENTIFIED BY 'SAPR3'" + " WITH GRANT OPTION";
- stmt.execute(sql);
+ util.execute(sql);
sql = "GRANT ALL PRIVILEGES ON MY1.*"
+ " TO 'JAVA'@'localhost' IDENTIFIED BY 'SAPR3'"
+ " WITH GRANT OPTION";
- stmt.execute(sql);
+ util.execute(sql);
- stmt.execute("commit");
- stmt.close();
+ util.execute("commit");
}
+
+ public void testInitializeUser() throws Exception {
+ File baseDir4 = new File(tmpDir, "mxj-init-user-test");
+ fileUtil.deleteTree(baseDir4);
+ mysqld = new MysqldResource(baseDir4);
+
+ Map params = new HashMap();
+ int port = new TestUtil().testPort();
+ params.put(MysqldResourceI.PORT, Integer.toString(port));
+
+ String rootUser = "root";
+ String rootPass = "";
+ String aliceName = "alice";
+ String alicePass = "q3htgi98q34";
+
+ mysqld.start("init-user", params);
+
+ String url = "jdbc:mysql://127.0.0.1:" + port + "/test";
+ conn = DriverManager.getConnection(url, rootUser, rootPass);
+ QueryUtil util = new QueryUtil(conn);
+ util.execute("SELECT 1");
+ conn.close();
+ conn = null;
+
+ Exception exception = null;
+ try {
+ conn = DriverManager.getConnection(url, aliceName, alicePass);
+ fail("Should not be able to connect as " + aliceName);
+ } catch (Exception e) {
+ exception = e;
+ }
+ assertNotNull("" + exception, exception);
+
+ mysqld.shutdown();
+ assertEquals(false, mysqld.isRunning());
+ fileUtil.deleteTree(baseDir4);
+
+ params.put(MysqldResourceI.INITIALIZE_USER, Boolean.TRUE.toString());
+ params.put(MysqldResourceI.INITIALIZE_USER_NAME, aliceName);
+ params.put(MysqldResourceI.INITIALIZE_PASSWORD, alicePass);
+ mysqld.start("init-user", params);
+
+ conn = DriverManager.getConnection(url, aliceName, alicePass);
+ util = new QueryUtil(conn);
+ util.execute("SELECT 1");
+ conn.close();
+ conn = null;
+
+ exception = null;
+ try {
+ conn = DriverManager.getConnection(url, rootUser, rootPass);
+ fail("Should not be able to connect as " + rootUser);
+ } catch (Exception e) {
+ exception = e;
+ }
+ assertNotNull("" + exception, exception);
+
+ mysqld.shutdown();
+ }
}
Modified: trunk/connector-mxj/src/com/mysql/management/MysqldResource.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/MysqldResource.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/MysqldResource.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -33,6 +33,7 @@
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.MysqlErrorNumbers;
import com.mysql.management.util.CommandLineOptionsParser;
+import com.mysql.management.util.InitializeUser;
import com.mysql.management.util.ListToString;
import com.mysql.management.util.NullPrintStream;
import com.mysql.management.util.Platform;
@@ -173,8 +174,15 @@
String portStr = "" + port;
mysqldArgs.put(MysqldResourceI.PORT, portStr);
mysqldArgs.remove(MysqldResourceI.MYSQLD_VERSION);
- mysqldArgs.remove(MysqldResourceI.USE_DEFAULT_ARCHITECTURE);
+ String initUserProp = (String) mysqldArgs
+ .remove(MysqldResourceI.INITIALIZE_USER);
+ boolean initUser = Boolean.valueOf(initUserProp).booleanValue();
+ String user = (String) mysqldArgs
+ .remove(MysqldResourceI.INITIALIZE_USER_NAME);
+ String password = (String) mysqldArgs
+ .remove(MysqldResourceI.INITIALIZE_PASSWORD);
+
if (populateAllOptions) {
options = optionParser.getOptionsFromHelp(getHelp(mysqldArgs));
} else {
@@ -199,6 +207,14 @@
boolean ready = canConnectToServer(port, killDelay);
setReadyForConnection(ready);
+
+ if (initUser) {
+ try {
+ new InitializeUser(port, user, password, err).initializeUser();
+ } catch (Throwable t) {
+ t.printStackTrace(err);
+ }
+ }
}
// Will wait 250 miliseconds between each try.
@@ -208,11 +224,8 @@
Connection conn = null;
String bogusUser = "Connector/MXJ";
String password = "Bogus Password";
- String url = "jdbc:mysql://localhost:"
- + port
- + "/test"
- + "?connectTimeout=150"
- + "&socketFactory=com.mysql.management.util.PatchedStandardSocketFactory";
+ String url = "jdbc:mysql://127.0.0.1:" + port + "/test"
+ + "?connectTimeout=150";
for (int i = 0; i < triesBeforeGivingUp; i++) {
try {
Modified: trunk/connector-mxj/src/com/mysql/management/MysqldResourceI.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/MysqldResourceI.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/MysqldResourceI.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -37,8 +37,13 @@
public static final String MYSQLD_VERSION = "mysql-version";
- public static final String USE_DEFAULT_ARCHITECTURE = "use-default-architecture";
+ public static final String INITIALIZE_USER = "initialize-user";
+ public static final String INITIALIZE_USER_NAME = INITIALIZE_USER + ".user";
+
+ public static final String INITIALIZE_PASSWORD = INITIALIZE_USER
+ + ".password";
+
void setVersion(String version);
String getVersion();
Modified: trunk/connector-mxj/src/com/mysql/management/driverlaunched/AcceptanceTest.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/driverlaunched/AcceptanceTest.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/driverlaunched/AcceptanceTest.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -64,6 +64,9 @@
System.setProperty(Files.USE_TEST_DIR, "");
}
new Files().cleanTestDir();
+ if (dataDir != null) {
+ new Files().deleteTree(dataDir);
+ }
}
}
@@ -72,13 +75,15 @@
port = new TestUtil().testPort();
- String url = "jdbc:mysql:mxj://localhost:" + port + "/test" //
- + "?server.use-default-architecture=true" //
- + "&server.datadir=" + dataDir.getPath() //
+ String url = "jdbc:mysql:mxj://localhost:" + port
+ + "/alice_db" //
+ + "?server.datadir=" + dataDir.getPath()
+ + "&server.initialize-user=true"//
+ + "&createDatabaseIfNotExist=true"//
;
- new TestUtil().assertConnectViaJDBC(url);
- new TestUtil().assertConnectViaJDBC(url);
- new TestUtil().assertConnectViaJDBC(url);
+ new TestUtil().assertConnectViaJDBC(url, "alice", "opt4g01396");
+ new TestUtil().assertConnectViaJDBC(url, "alice", "opt4g01396");
+ new TestUtil().assertConnectViaJDBC(url, "alice", "opt4g01396");
}
}
Modified: trunk/connector-mxj/src/com/mysql/management/driverlaunched/MysqldResourceNotFoundException.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/driverlaunched/MysqldResourceNotFoundException.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/driverlaunched/MysqldResourceNotFoundException.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.driverlaunched;
public class MysqldResourceNotFoundException extends RuntimeException {
Modified: trunk/connector-mxj/src/com/mysql/management/driverlaunched/ServerLauncherSocketFactory.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/driverlaunched/ServerLauncherSocketFactory.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/driverlaunched/ServerLauncherSocketFactory.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Properties;
+import com.mysql.jdbc.NonRegisteringDriver;
import com.mysql.jdbc.SocketFactory;
import com.mysql.jdbc.StandardSocketFactory;
import com.mysql.management.MysqldFactory;
@@ -66,11 +67,18 @@
Map serverOpts = new HashMap();
for (Enumeration enums = props.propertyNames(); enums.hasMoreElements();) {
String key = enums.nextElement().toString();
+ String rawValue = props.getProperty(key);
+ // System.err.println(key + ":" + rawValue);
if (key.startsWith(SERVER_DOT)) {
- String val = replaceNullStringWithNull(props.getProperty(key));
+ String val = replaceNullStringWithNull(rawValue);
serverOpts.put(key.substring(SERVER_DOT.length()), val);
}
}
+ serverOpts.put(MysqldResourceI.INITIALIZE_USER_NAME, props
+ .getProperty(NonRegisteringDriver.USER_PROPERTY_KEY));
+ serverOpts.put(MysqldResourceI.INITIALIZE_PASSWORD, props
+ .getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY));
+
serverOpts.put(MysqldResourceI.PORT, Integer.toString(port));
Object baseDirStr = serverOpts.get(MysqldResourceI.BASEDIR);
File baseDir = new Files().newFile(baseDirStr);
Modified: trunk/connector-mxj/src/com/mysql/management/jmx/AllTestsSuite.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/jmx/AllTestsSuite.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/jmx/AllTestsSuite.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.jmx;
import junit.framework.Test;
Modified: trunk/connector-mxj/src/com/mysql/management/jmx/TestFactory.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/jmx/TestFactory.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/jmx/TestFactory.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,5 +1,19 @@
-/**
- *
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
*/
package com.mysql.management.jmx;
Modified: trunk/connector-mxj/src/com/mysql/management/jmx/jboss/AllTestsSuite.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/jmx/jboss/AllTestsSuite.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/jmx/jboss/AllTestsSuite.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.jmx.jboss;
import junit.framework.Test;
Modified: trunk/connector-mxj/src/com/mysql/management/util/AllTestsSuite.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/AllTestsSuite.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/AllTestsSuite.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2004 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.util;
import junit.framework.Test;
Added: trunk/connector-mxj/src/com/mysql/management/util/InitializeUser.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/InitializeUser.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/InitializeUser.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -0,0 +1,110 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
+package com.mysql.management.util;
+
+import java.io.PrintStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public class InitializeUser {
+
+ private String userName;
+
+ private String password;
+
+ private String url;
+
+ private PrintStream err;
+
+ public InitializeUser(int port, String userName, String password,
+ PrintStream err) {
+ this.userName = userName;
+ this.password = password;
+ this.url = "jdbc:mysql://127.0.0.1:" + port + "/mysql";
+ this.err = err;
+
+ try {
+ Class.forName(com.mysql.jdbc.Driver.class.getName());
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /** returns true if the password was set with this attempt */
+ public boolean initializeUser() {
+ Connection conn = null;
+ try {
+ conn = DriverManager.getConnection(url, userName, password);
+ return false;
+ } catch (SQLException e) {
+ // Okay, current user not initialized;
+ } finally {
+ close(conn);
+ }
+
+ try {
+ final String NO_PASSWORD = null;
+ conn = DriverManager.getConnection(url, "root", NO_PASSWORD);
+ } catch (SQLException e) {
+ String msg = "User initialization error." //
+ + " Can not connect as " + userName + " with password." //
+ + " Can not connect as root without password." //
+ + " URL: " + url;
+ throw new SQLRuntimeException(msg, e, null, null);
+ }
+ try {
+ QueryUtil util = new QueryUtil(conn, err);
+ // util.execute("drop user ''");
+ // util.execute("drop user 'root'@'localhost'");
+ // util.execute("drop user 'root'@'127.0.0.1'");
+ util.execute("DELETE from user");
+ String sql = "grant all on *.* to ?@'localhost' identified by ? with grant option";
+ final Object[] params = new Object[] { userName, password };
+ util.execute(sql, params);
+ util.execute("flush privileges");
+ } finally {
+ close(conn);
+ }
+
+ try {
+ conn = DriverManager.getConnection(url, userName, password);
+ QueryUtil util = new QueryUtil(conn, err);
+ util.execute("SELECT 1");
+ } catch (SQLException e) {
+ String msg = "User initialization error." //
+ + " Can not connect as " + userName + " with password" //
+ + " after creating user and password." //
+ + " URL: " + url;
+ throw new SQLRuntimeException(msg, e, null, null);
+ } finally {
+ close(conn);
+ }
+ return true;
+ }
+
+ private void close(Connection conn) {
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (Throwable t) {
+ t.printStackTrace(err);
+ }
+ }
+ }
+}
Modified: trunk/connector-mxj/src/com/mysql/management/util/NotImplementedException.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/NotImplementedException.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/NotImplementedException.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.util;
public final class NotImplementedException extends RuntimeException {
Modified: trunk/connector-mxj/src/com/mysql/management/util/ProcessUtilTest.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/ProcessUtilTest.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/ProcessUtilTest.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.util;
import java.io.File;
Added: trunk/connector-mxj/src/com/mysql/management/util/QueryUtil.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/QueryUtil.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/QueryUtil.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -0,0 +1,264 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
+package com.mysql.management.util;
+
+import java.io.PrintStream;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+public class QueryUtil {
+
+ private final Connection conn;
+
+ private PrintStream err;
+
+ public QueryUtil(Connection conn) {
+ this(conn, System.err);
+ }
+
+ public QueryUtil(Connection conn, PrintStream err) {
+ this.conn = conn;
+ this.err = err;
+ }
+
+ /**
+ * @param query
+ * the SQL Query to execute
+ * @param params
+ * the Object[] containing the variables for the query
+ *
+ * @return a java.util.List of Maps. Each Row of the query result set is
+ * represented by a map in the list. Each map in the list has an
+ * Map.Entry of column name to column value.
+ */
+ public List executeQuery(String query, Object[] params) {
+ PreparedStatement pStmt = null;
+ ResultSet rs = null;
+ try {
+ pStmt = prepareStatement(query, params);
+ rs = pStmt.executeQuery();
+ return mapRows(rs);
+ } catch (SQLException sqlEx) {
+ throw new SQLRuntimeException(sqlEx, query, params);
+ } finally {
+ cleanupRsAndStmt(pStmt, rs, query, null);
+ }
+ }
+
+ private List mapRows(ResultSet rs) throws SQLException {
+ List rows;
+ int columns = rs.getMetaData().getColumnCount();
+ rows = new ArrayList();
+ while (rs.next()) {
+ Map rowVals = new LinkedHashMap(columns);
+ for (int i = 1; i <= columns; i++) {
+ String columnName = rs.getMetaData().getColumnName(i);
+ Object columValue = rs.getObject(i);
+ rowVals.put(columnName, columValue);
+ }
+ rows.add(rowVals);
+ }
+ return rows;
+ }
+
+ /**
+ * @param query
+ * the SQL Query to execute
+ *
+ * @return a java.util.List of Maps. Each Row of the query result set is
+ * represented by a map in the list. Each map in the list has an
+ * Map.Entry of column name to column value.
+ */
+ public List executeQuery(String SQLquery) {
+ return executeQuery(SQLquery, null);
+ }
+
+ /**
+ * @param query
+ * the SQL Query to execute
+ * @param params
+ * the Object[] containing the parameters for the query
+ *
+ * @return the row count (or 0 for SQL statements which return nothing)
+ */
+ public int executeUpdate(String query, Object[] params) {
+ PreparedStatement pStmt = null;
+ try {
+ pStmt = prepareStatement(query, params);
+ return pStmt.executeUpdate();
+ } catch (SQLException sqlEx) {
+ throw new SQLRuntimeException(sqlEx, query, params);
+ } finally {
+ cleanupRsAndStmt(pStmt, null, query, params);
+ }
+ }
+
+ public boolean execute(String query, Object[] params) {
+ PreparedStatement pstmt = null;
+ try {
+ pstmt = prepareStatement(query, params);
+ return pstmt.execute();
+ } catch (SQLException e) {
+ throw new SQLRuntimeException(e, query, params);
+ } finally {
+ cleanupRsAndStmt(pstmt, null, query, null);
+ }
+ }
+
+ public int[] executeBatch(String query, Object[][] params) {
+ PreparedStatement pStmt = null;
+ ResultSet resultSet = null;
+ try {
+ pStmt = prepareStatement(query);
+ for (int i = 0; i < params.length; i++) {
+ setParameters(query, params[i], pStmt);
+ pStmt.addBatch();
+ }
+ return pStmt.executeBatch();
+ } catch (SQLException e) {
+ throw new SQLRuntimeException(e, query, params);
+ } finally {
+ cleanupRsAndStmt(pStmt, resultSet, query, params);
+ }
+ }
+
+ public int executeUpdate(String query) {
+ return executeUpdate(query, null);
+ }
+
+ public boolean execute(String query) {
+ return execute(query, null);
+ }
+
+ public List executeUpdateReturningKeys(String query) {
+ return executeUpdateReturningKeys(query, null);
+ }
+
+ public List executeUpdateReturningKeys(String query, Object[] params) {
+ PreparedStatement pStmt = null;
+ ResultSet resultSet = null;
+ try {
+ pStmt = prepareStatement(query, params);
+ pStmt.executeUpdate();
+ resultSet = pStmt.getGeneratedKeys();
+ return mapRows(resultSet);
+ } catch (SQLException e) {
+ throw new SQLRuntimeException(e, query, params);
+ } finally {
+ cleanupRsAndStmt(pStmt, resultSet, query, params);
+ }
+ }
+
+ private PreparedStatement prepareStatement(String query, Object[] params)
+ throws SQLException {
+ PreparedStatement pStmt = prepareStatement(query);
+ setParameters(query, params, pStmt);
+ return pStmt;
+ }
+
+ private void setParameters(String query, Object[] params,
+ PreparedStatement pStmt) throws SQLException {
+ int numParams = pStmt.getParameterMetaData().getParameterCount();
+ int paramCount = (params == null) ? 0 : params.length;
+
+ if (numParams != paramCount) {
+ final String msg = "Expected " + numParams + " parameters, got "
+ + paramCount;
+ throw new SQLRuntimeException(msg, query, params);
+ }
+
+ if (params != null) {
+ for (int i = 0; i < params.length; i++) {
+ Object param = params[i];
+ pStmt.setObject(i + 1, param);
+ }
+ }
+ }
+
+ private PreparedStatement prepareStatement(String query)
+ throws SQLException {
+ if (query == null) {
+ throw new IllegalArgumentException("query string may not be null");
+ }
+ if (query.length() == 0) {
+ throw new IllegalArgumentException("query string may not be empty");
+ }
+ return conn.prepareStatement(query);
+ }
+
+ private void cleanupRsAndStmt(Statement stmt, ResultSet rs, String origSql,
+ Object[] params) {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (Throwable t) {
+ new SQLRuntimeException(t, origSql, params)
+ .printStackTrace(err);
+ }
+ }
+ if (stmt != null) {
+ try {
+ stmt.close();
+ } catch (Throwable t) {
+ new SQLRuntimeException(t, origSql, params)
+ .printStackTrace(err);
+ }
+ }
+ }
+
+ public void printQueryResults(String SQLquery, PrintStream out) {
+ List results = executeQuery(SQLquery);
+ for (Iterator it = results.iterator(); it.hasNext();) {
+ final Map row = (Map) it.next();
+ for (Iterator it2 = row.entrySet().iterator(); it2.hasNext();) {
+ final Map.Entry entry = (Map.Entry) it.next();
+ out.println(entry.getKey() + ": " + entry.getValue());
+ }
+ if (it.hasNext()) {
+ out.println();
+ }
+ }
+ }
+
+ public String queryForString(String SQLquery) {
+ return queryForString(SQLquery, null);
+ }
+
+ public String queryForString(String SQLquery, Object[] params) {
+ List restults = executeQuery(SQLquery, params);
+ for (Iterator it = restults.iterator(); it.hasNext();) {
+ Map row = (Map) it.next();
+ for (Iterator it2 = row.entrySet().iterator(); it2.hasNext();) {
+ Map.Entry column = (Map.Entry) it2.next();
+ if (column.getValue() == null) {
+ return null;
+ }
+ return column.getValue().toString();
+ }
+ }
+ return null;
+ }
+}
Modified: trunk/connector-mxj/src/com/mysql/management/util/QuietTestCase.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/QuietTestCase.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/QuietTestCase.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.util;
import java.io.PrintStream;
Modified: trunk/connector-mxj/src/com/mysql/management/util/RuntimeI.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/RuntimeI.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/RuntimeI.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.util;
import java.io.File;
Modified: trunk/connector-mxj/src/com/mysql/management/util/RuntimeTest.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/RuntimeTest.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/RuntimeTest.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.util;
import junit.framework.TestCase;
Added: trunk/connector-mxj/src/com/mysql/management/util/SQLRuntimeException.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/SQLRuntimeException.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/SQLRuntimeException.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -0,0 +1,115 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
+package com.mysql.management.util;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.sql.SQLException;
+
+public class SQLRuntimeException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ private String sql;
+
+ private Object[] params;
+
+ public SQLRuntimeException(SQLException cause) {
+ this(cause, null, null);
+ }
+
+ public SQLRuntimeException(Throwable cause, String sql) {
+ this(cause, sql, null);
+ }
+
+ public SQLRuntimeException(Throwable cause, String sql, Object[] params) {
+ super(cause);
+ this.sql = sql;
+ this.params = params;
+ }
+
+ public SQLRuntimeException(String msg, String sql, Object[] params) {
+ super(msg);
+ this.sql = sql;
+ this.params = params;
+ }
+
+ public SQLRuntimeException(String msg, Throwable cause, String sql,
+ Object[] params) {
+ super(msg, cause);
+ this.sql = sql;
+ this.params = params;
+ }
+
+ public int getErrorCode() {
+ for (Throwable t = this; t != null; t = t.getCause()) {
+ if (t instanceof SQLException) {
+ return ((SQLException) t).getErrorCode();
+ }
+ }
+ return 0;
+ }
+
+ public void printStackTrace(PrintStream ps) {
+ synchronized (ps) {
+ PrintWriter pw = new PrintWriter(ps);
+ printState(pw);
+ pw.flush();
+ super.printStackTrace(ps);
+ }
+ }
+
+ public void printStackTrace(PrintWriter pw) {
+ synchronized (pw) {
+ printState(pw);
+ super.printStackTrace(pw);
+ }
+ }
+
+ private void printState(PrintWriter p) {
+ p.println("SQL: " + sql);
+ if (params != null) {
+ if (params instanceof Object[][]) {
+ for (int i = 0; i < params.length; i++) {
+ dumpParams(p, ((Object[][]) params)[i]);
+ }
+ } else {
+ dumpParams(p, params);
+ }
+ }
+ p.println("ErrorCode: " + getErrorCode());
+ }
+
+ private void dumpParams(PrintWriter p, Object[] paramList) {
+ p.print("PARAMS: ");
+ for (int i = 0; i < paramList.length; i++) {
+ final Object param = paramList[i];
+
+ final String type = (param == null) ? "no type"
+ : shortClassName(param.getClass());
+
+ p.print(" (" + type + "): '" + param + "' ");
+ }
+ p.println();
+ }
+
+ private String shortClassName(Class aClass) {
+ String name = aClass.getName();
+ int lastDot = name.lastIndexOf('.');
+ return name.substring(lastDot + 1);
+ }
+}
Modified: trunk/connector-mxj/src/com/mysql/management/util/Str.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/Str.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/Str.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,11 +1,3 @@
-package com.mysql.management.util;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.List;
-
/*
Copyright (C) 2004 MySQL AB
@@ -23,7 +15,14 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+package com.mysql.management.util;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* String utility methods.
*
@@ -39,8 +38,11 @@
private String newLine;
public Str() {
- newLine = System.getProperty("line.separator");
+ this(System.getProperty("line.separator"));
}
+ public Str(String newLine) {
+ this.newLine = newLine;
+ }
public boolean containsIgnoreCase(String searchIn, String searchFor) {
Modified: trunk/connector-mxj/src/com/mysql/management/util/TestUtil.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/TestUtil.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/TestUtil.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -23,6 +23,8 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.List;
+import java.util.Map;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
@@ -85,6 +87,7 @@
}
checkVersion(conn);
checkBigInt(conn);
+ variousStuff(conn);
} finally {
if (conn != null) {
conn.close();
@@ -191,32 +194,70 @@
}
/** creates table, inserts, selects, drops table */
- private void checkBigInt(Connection conn) throws SQLException {
- Statement stmt = conn.createStatement();
- ResultSet rs = null;
+ private void checkBigInt(Connection conn) {
+ QueryUtil util = new QueryUtil(conn);
String tableName = "bigIntRegression";
String col1 = "bigIntCol";
long testVal = 6692730313872877584L;
try {
- stmt.executeUpdate("DROP TABLE IF EXISTS " + tableName);
- stmt.executeUpdate("CREATE TABLE " + tableName + " (" + col1
+ util.executeUpdate("DROP TABLE IF EXISTS " + tableName);
+ util.executeUpdate("CREATE TABLE " + tableName + " (" + col1
+ " BIGINT NOT NULL)");
- stmt.executeUpdate("INSERT INTO " + tableName + " VALUES ("
+ util.executeUpdate("INSERT INTO " + tableName + " VALUES ("
+ testVal + ")");
- rs = stmt.executeQuery("SELECT " + col1 + " FROM " + tableName);
+ List rows = util.executeQuery("SELECT " + col1 + " FROM "
+ + tableName);
+ for (int i = 0; i < rows.size(); i++) {
+ Map row = (Map) rows.get(i);
+ Assert.assertTrue(row.size() > 0);
+ Map.Entry column1 = (Map.Entry) row.entrySet().iterator()
+ .next();
+ Number n = (Number) column1.getValue();
+ Assert.assertEquals(testVal, n.longValue());
+ }
- int rows = 0;
- while (rs.next()) {
- long retrieveAsLong = rs.getLong(1);
- Assert.assertEquals(testVal, retrieveAsLong);
- rows++;
+ Assert.assertEquals(1, rows.size());
+ } finally {
+ util.executeUpdate("DROP TABLE IF EXISTS " + tableName);
+ }
+ }
+
+ /** creates table, inserts, selects, batch updates, drops table */
+ private void variousStuff(Connection conn) {
+ QueryUtil util = new QueryUtil(conn);
+ String tableName = "foo_table";
+ String col1 = "foo_id";
+ String col2 = "bar";
+ try {
+ util.executeUpdate("DROP TABLE IF EXISTS " + tableName);
+ util.executeUpdate("CREATE TABLE " + tableName + " (" + //
+ col1 + " INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY," + //
+ col2 + " TEXT" //
+ + ")");
+
+ String sql = "INSERT INTO " + tableName + " VALUES (NULL,'alpha')";
+ List rows = util.executeUpdateReturningKeys(sql);
+ Assert.assertEquals(1, rows.size());
+ Map key1Map = (Map) rows.get(0);
+ Assert.assertEquals(1, key1Map.size());
+ Map.Entry entry = ((Map.Entry) key1Map.entrySet().iterator().next());
+ Object columnName = entry.getKey();
+ Object generatedKey = entry.getValue();
+ Assert.assertNotNull("GENERATED_KEY", columnName);
+ Assert.assertEquals(new Long(1), generatedKey);
+
+ sql = "INSERT INTO " + tableName + " VALUES (NULL,?)";
+ Object[][] batchParams = { new Object[] { "foo" },
+ new Object[] { "bar" }, new Object[] { "baz" }, };
+
+ int[] ints = util.executeBatch(sql, batchParams);
+ Assert.assertEquals(3, ints.length);
+ for (int i = 0; i < ints.length; i++) {
+ Assert.assertEquals(1, ints[i]);
}
- rs.close();
-
- Assert.assertEquals(1, rows);
} finally {
- stmt.executeUpdate("DROP TABLE IF EXISTS " + tableName);
+ util.executeUpdate("DROP TABLE IF EXISTS " + tableName);
}
}
}
Modified: trunk/connector-mxj/src/com/mysql/management/util/Utils.java
===================================================================
--- trunk/connector-mxj/src/com/mysql/management/util/Utils.java 2007-05-21 17:28:13 UTC (rev 93)
+++ trunk/connector-mxj/src/com/mysql/management/util/Utils.java 2007-05-25 16:00:36 UTC (rev 94)
@@ -1,3 +1,20 @@
+/*
+ Copyright (C) 2007 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 2 as
+ published by the Free Software Foundation.
+
+ 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
+
+ */
package com.mysql.management.util;
import java.io.File;
| Thread |
|---|
| • Connector/MXJ commit: r94 - in trunk/connector-mxj: . src src/com/mysql/management src/com/mysql/management/driverlaunched src/com/mysql/management/jm... | eherman | 25 May |