Added: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/DerbyJDBCLockTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/DerbyJDBCLockTest.java?rev=946719&view=auto
==============================================================================
--- 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/DerbyJDBCLockTest.java
 (added)
+++ 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/DerbyJDBCLockTest.java
 Thu May 20 17:22:47 2010
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.karaf.main;
+
+import static org.junit.Assert.assertEquals;
+
+import java.sql.Connection;
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class DerbyJDBCLockTest extends BaseJDBCLockTest {
+    
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        password = "root";
+        driver = "org.apache.derby.jdbc.ClientDriver";
+        url = "jdbc:derby://127.0.0.1:1527/test";
+        
+        super.setUp();
+    }
+    
+    DerbyJDBCLock createLock(Properties props) {
+        return new DerbyJDBCLock(props) {
+            @Override
+            Connection doCreateConnection(String driver, String url, String 
username, String password) {
+                assertEquals(this.driver, driver);
+                assertEquals(this.url + ";create=true", url);
+                assertEquals(this.user, username);
+                assertEquals(this.password, password);
+                return connection;
+            }
+
+            @Override
+            long getCurrentTimeMillis() {
+                return 1;
+            }
+        };
+    }
+    
+    @Test
+    public void createConnectionShouldConcatinateOptionsCorrect() {
+        props.put("karaf.lock.jdbc.url", this.url + ";dataEncryption=false");
+        
+        lock = new DerbyJDBCLock(props) {
+            @Override
+            Connection doCreateConnection(String driver, String url, String 
username, String password) {
+                assertEquals(this.driver, driver);
+                assertEquals(this.url + ";create=true", url);
+                assertEquals(this.user, username);
+                assertEquals(this.password, password);
+                return connection;
+            }
+
+            @Override
+            long getCurrentTimeMillis() {
+                return 1;
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/DerbyJDBCLockTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockIntegrationTest.java?rev=946719&view=auto
==============================================================================
--- 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockIntegrationTest.java
 (added)
+++ 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockIntegrationTest.java
 Thu May 20 17:22:47 2010
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.karaf.main;
+
+
+import static org.junit.Assert.assertFalse;
+
+import java.sql.Connection;
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+
+...@ignore
+public class MySQLJDBCLockIntegrationTest extends BaseJDBCLockIntegrationTest {
+
+    @Before
+    public void setUp() throws Exception {
+        driver = "com.mysql.jdbc.Driver";
+        url = "jdbc:mysql://127.0.0.1:3306/test";
+        
+        super.setUp();
+    }
+    
+    @Override
+    MySQLJDBCLock createLock(Properties props) {
+        return new MySQLJDBCLock(props);
+    }
+    
+    @Test
+    public void initShouldCreateTheDatabaseIfItNotExists() throws Exception {
+        String database = "test" + System.currentTimeMillis();
+        
+        try {
+            executeStatement("DROP DATABASE " + database);
+        } catch (Exception e) {
+            // expected if table dosn't exist
+        }
+        
+        url = "jdbc:mysql://127.0.0.1:3306/" + database;
+        props.put("karaf.lock.jdbc.url", url);
+        lock = createLock(props);
+        
+        
+        // should throw an exeption, if the database doesn't exists
+        Connection connection = getConnection("jdbc:mysql://127.0.0.1:3306/" + 
database, user, password);
+        assertFalse(connection.isClosed());
+        
+        executeStatement("DROP DATABASE " + database);
+        close(connection);
+    }
+}
\ No newline at end of file

Propchange: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockIntegrationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockTest.java?rev=946719&view=auto
==============================================================================
--- 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockTest.java
 (added)
+++ 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockTest.java
 Thu May 20 17:22:47 2010
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.karaf.main;
+
+import static org.junit.Assert.assertEquals;
+
+import java.sql.Connection;
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class MySQLJDBCLockTest extends BaseJDBCLockTest {
+    
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        driver = "com.mysql.jdbc.Driver";
+        url = "jdbc:mysql://127.0.0.1:3306/test";
+        createTableStmtSuffix = " ENGINE=INNODB";
+        
+        super.setUp();
+    }
+    
+    MySQLJDBCLock createLock(Properties props) {
+        return new MySQLJDBCLock(props) {
+            @Override
+            Connection doCreateConnection(String driver, String url, String 
username, String password) {
+                assertEquals(this.driver, driver);
+                assertEquals(this.url + "?createDatabaseIfNotExist=true", url);
+                assertEquals(this.user, username);
+                assertEquals(this.password, password);
+                return connection;
+            }
+
+            @Override
+            long getCurrentTimeMillis() {
+                return 1;
+            }
+        };
+    }
+    
+    @Test
+    public void createConnectionShouldConcatinateOptionsCorrect() {
+        props.put("karaf.lock.jdbc.url", this.url + "?connectTimeout=10000");
+        
+        lock = new MySQLJDBCLock(props) {
+            @Override
+            Connection doCreateConnection(String driver, String url, String 
username, String password) {
+                assertEquals(this.driver, driver);
+                assertEquals(this.url + "&createDatabaseIfNotExist=true", url);
+                assertEquals(this.user, username);
+                assertEquals(this.password, password);
+                return connection;
+            }
+
+            @Override
+            long getCurrentTimeMillis() {
+                return 1;
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/MySQLJDBCLockTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockIntegrationTest.java?rev=946719&view=auto
==============================================================================
--- 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockIntegrationTest.java
 (added)
+++ 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockIntegrationTest.java
 Thu May 20 17:22:47 2010
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.karaf.main;
+
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Ignore;
+
+
+...@ignore
+public class OracleJDBCLockIntegrationTest extends BaseJDBCLockIntegrationTest 
{
+    
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        password = "root";
+        driver = "oracle.jdbc.driver.OracleDriver";
+        url = "jdbc:oracle:thin:@172.16.16.133:1521:XE";
+        momentDatatype = "NuMBER(20)";
+        
+        super.setUp();
+    }
+
+    OracleJDBCLock createLock(Properties props) {
+        return new OracleJDBCLock(props);
+    }
+    
+    @Override
+    Connection lock(String table, String node) throws ClassNotFoundException, 
SQLException {
+        Connection connection = null;
+        Statement statement = null;
+        
+        try {
+            connection = getConnection(url, user, password);
+            statement = connection.createStatement();
+            statement.execute("SELECT * FROM " + table + " FOR UPDATE");
+        } finally {
+            close(statement);
+            // connection must not be closed!
+        }
+        
+        return connection;
+    }
+}
\ No newline at end of file

Propchange: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockIntegrationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockTest.java?rev=946719&view=auto
==============================================================================
--- 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockTest.java
 (added)
+++ 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockTest.java
 Thu May 20 17:22:47 2010
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.karaf.main;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.*;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class OracleJDBCLockTest extends BaseJDBCLockTest {
+    
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        password = "root";
+        driver = "oracle.jdbc.driver.OracleDriver";
+        url = "jdbc:oracle:thin:@172.16.16.132:1521:XE";
+        momentDatatype = "NUMBER(20)";
+        
+        super.setUp();
+    }
+    
+    OracleJDBCLock createLock(Properties props) {
+        return new OracleJDBCLock(props) {
+            @Override
+            Connection doCreateConnection(String driver, String url, String 
username, String password) {
+                assertEquals(this.driver, driver);
+                assertEquals(this.url, url);
+                assertEquals(this.user, username);
+                assertEquals(this.password, password);
+                return connection;
+            }
+
+            @Override
+            long getCurrentTimeMillis() {
+                return 1;
+            }
+        };
+    }
+    
+    @Test
+    @Override
+    public void lockShouldReturnTrueItTheTableIsNotLocked() throws Exception {
+        initShouldNotCreateTheSchemaIfItAlreadyExists();
+        reset(connection, metaData, statement, preparedStatement, resultSet);
+
+        expect(connection.isClosed()).andReturn(false);
+        expect(connection.prepareStatement("SELECT * FROM " + tableName + " 
FOR UPDATE")).andReturn(preparedStatement);
+        preparedStatement.setQueryTimeout(10);
+        expect(preparedStatement.execute()).andReturn(true);
+        preparedStatement.close();
+        
+        replay(connection, metaData, statement, preparedStatement, resultSet);
+        
+        boolean lockAquired = lock.lock();
+        
+        verify(connection, metaData, statement, preparedStatement, resultSet);
+        assertTrue(lockAquired);
+    }
+    
+    @Test
+    @Override
+    public void lockShouldReturnFalseIfAnotherRowIsLocked() throws Exception {
+        initShouldNotCreateTheSchemaIfItAlreadyExists();
+        reset(connection, metaData, statement, preparedStatement, resultSet);
+        
+        expect(connection.isClosed()).andReturn(false);
+        expect(connection.prepareStatement("SELECT * FROM " + tableName + " 
FOR UPDATE")).andReturn(preparedStatement);
+        preparedStatement.setQueryTimeout(10);
+        expect(preparedStatement.execute()).andThrow(new SQLException());
+        preparedStatement.close();
+        
+        replay(connection, metaData, statement, preparedStatement, resultSet);
+        
+        boolean lockAquired = lock.lock();
+        
+        verify(connection, metaData, statement, preparedStatement, resultSet);
+        assertFalse(lockAquired);
+    }
+    
+    @Test
+    @Override
+    public void lockShouldReturnFalseIfTheRowIsAlreadyLocked() throws 
Exception {
+        initShouldNotCreateTheSchemaIfItAlreadyExists();
+        reset(connection, metaData, statement, preparedStatement, resultSet);
+        
+        expect(connection.isClosed()).andReturn(false);
+        expect(connection.prepareStatement("SELECT * FROM " + tableName + " 
FOR UPDATE")).andReturn(preparedStatement);
+        preparedStatement.setQueryTimeout(10);
+        expect(preparedStatement.execute()).andThrow(new SQLException());
+        preparedStatement.close();
+        
+        replay(connection, metaData, statement, preparedStatement, resultSet);
+        
+        boolean lockAquired = lock.lock();
+        
+        verify(connection, metaData, statement, preparedStatement, resultSet);
+        assertFalse(lockAquired);
+    }
+    
+    @Test
+    public void isAliveShouldReturnTrueIfItHoldsTheLock() throws Exception {
+        initShouldNotCreateTheSchemaIfItAlreadyExists();
+        reset(connection, metaData, statement, preparedStatement, resultSet);
+        
+        expect(connection.isClosed()).andReturn(false);
+        expect(connection.isClosed()).andReturn(false);
+        expect(connection.prepareStatement("SELECT * FROM " + tableName + " 
FOR UPDATE")).andReturn(preparedStatement);
+        preparedStatement.setQueryTimeout(10);
+        expect(preparedStatement.execute()).andReturn(true);
+        preparedStatement.close();
+        
+        replay(connection, metaData, statement, preparedStatement, resultSet);
+        
+        boolean alive = lock.isAlive();
+        
+        verify(connection, metaData, statement, preparedStatement, resultSet);
+        assertTrue(alive);
+    }
+    
+    @Test
+    public void isAliveShouldReturnFalseIfItNotHoldsTheLock() throws Exception 
{
+        initShouldNotCreateTheSchemaIfItAlreadyExists();
+        reset(connection, metaData, statement, preparedStatement, resultSet);
+        
+        expect(connection.isClosed()).andReturn(false);
+        expect(connection.isClosed()).andReturn(false);
+        expect(connection.prepareStatement("SELECT * FROM " + tableName + " 
FOR UPDATE")).andReturn(preparedStatement);
+        preparedStatement.setQueryTimeout(10);
+        expect(preparedStatement.execute()).andThrow(new SQLException());
+        preparedStatement.close();
+        
+        replay(connection, metaData, statement, preparedStatement, resultSet);
+        
+        boolean alive = lock.isAlive();
+        
+        verify(connection, metaData, statement, preparedStatement, resultSet);
+        assertFalse(alive);
+    }
+}
\ No newline at end of file

Propchange: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/OracleJDBCLockTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/StatementsTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/StatementsTest.java?rev=946719&view=auto
==============================================================================
--- 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/StatementsTest.java
 (added)
+++ 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/StatementsTest.java
 Thu May 20 17:22:47 2010
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.karaf.main;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class StatementsTest {
+    
+    private static final String DEFAULT_CREATE_TABLE_STMT = "CREATE TABLE 
KARAF_LOCK (MOMENT BIGINT, NODE VARCHAR(20))";
+    private static final String DEFAULT_POPULATE_TABLE_STMT = "INSERT INTO 
KARAF_LOCK (MOMENT, NODE) VALUES (1, 'karaf')";
+    
+    private Statements statements;
+    
+    @Before
+    public void setUp() {
+        statements = new Statements();
+    }
+
+    @Test
+    public void getDefaultLockCreateSchemaStatements() {
+        assertArrayEquals(new String[] {DEFAULT_CREATE_TABLE_STMT, 
DEFAULT_POPULATE_TABLE_STMT}, statements.getLockCreateSchemaStatements(1));
+    }
+    
+    @Test
+    public void getCustomLockCreateSchemaStatements() {
+        customizeStatements();
+        String[] expectedCreateSchemaStmts = new String[] {
+                "CREATE TABLE test.LOCK_TABLE (MOMENT NUMBER(20), NODE 
VARCHAR2(30))", 
+                "INSERT INTO test.LOCK_TABLE (MOMENT, NODE) VALUES (2, 
'node_1')"};
+        
+        assertArrayEquals(expectedCreateSchemaStmts, 
statements.getLockCreateSchemaStatements(2));
+    }
+
+    @Test
+    public void getDefaultLockCreateStatement() {
+        assertEquals("SELECT * FROM KARAF_LOCK FOR UPDATE", 
statements.getLockCreateStatement());
+    }
+    
+    @Test
+    public void getCustomLockCreateStatement() {
+        customizeStatements();
+        
+        assertEquals("SELECT * FROM test.LOCK_TABLE FOR UPDATE", 
statements.getLockCreateStatement());
+    }
+
+    @Test
+    public void getDefaultLockUpdateStatement() {
+        assertEquals("UPDATE KARAF_LOCK SET MOMENT = 1", 
statements.getLockUpdateStatement(1));
+    }
+    
+    @Test
+    public void getCustomLockUpdateStatement() {
+        customizeStatements();
+        
+        assertEquals("UPDATE test.LOCK_TABLE SET MOMENT = 2", 
statements.getLockUpdateStatement(2));
+    }
+    
+    private void customizeStatements() {
+        statements.setTablePrefix("test.");
+        statements.setTableName("LOCK_TABLE");
+        statements.setNodeName("node_1");
+        statements.setMomentColumnDataType("NUMBER(20)");
+        statements.setNodeColumnDataType("VARCHAR2(30)");
+    }
+}
\ No newline at end of file

Propchange: 
felix/trunk/karaf/main/src/test/java/org/apache/felix/karaf/main/StatementsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to