jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r633005 - in /jackrabbit/branches/1.3/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence: DatabaseConnectionFailureTest.java ext/
Date Mon, 03 Mar 2008 09:41:33 GMT
Author: jukka
Date: Mon Mar  3 01:41:33 2008
New Revision: 633005

URL: http://svn.apache.org/viewvc?rev=633005&view=rev
Log:
JCR-1400: Backport JCR-940: add db connection autoConnect for BundleDbPersistenceManager
    - 1.3: Use the less fragile thread-based test case

Removed:
    jackrabbit/branches/1.3/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/ext/
Modified:
    jackrabbit/branches/1.3/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/DatabaseConnectionFailureTest.java

Modified: jackrabbit/branches/1.3/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/DatabaseConnectionFailureTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.3/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/DatabaseConnectionFailureTest.java?rev=633005&r1=633004&r2=633005&view=diff
==============================================================================
--- jackrabbit/branches/1.3/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/DatabaseConnectionFailureTest.java
(original)
+++ jackrabbit/branches/1.3/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/DatabaseConnectionFailureTest.java
Mon Mar  3 01:41:33 2008
@@ -16,10 +16,20 @@
  */
 package org.apache.jackrabbit.core.persistence;
 
+import java.io.File;
+
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+
+import junit.framework.TestCase;
+
+import org.apache.derby.drda.NetworkServerControl;
+import org.apache.jackrabbit.core.RepositoryImpl;
+import org.apache.jackrabbit.core.config.RepositoryConfig;
+import org.apache.jackrabbit.test.config.PersistenceManagerConf;
+import org.apache.jackrabbit.test.config.RepositoryConf;
 
-import org.apache.jackrabbit.core.persistence.ext.ExternalDerbyTest;
 
 /**
  * <code>DatabaseConnectionFailureTest</code> tests various situations in
@@ -27,87 +37,166 @@
  * is lost. If the server restarts properly, Jackrabbit should re-connect
  * automatically and in the best case there is no data lost.
  */
-public class DatabaseConnectionFailureTest extends ExternalDerbyTest {
+public class DatabaseConnectionFailureTest extends TestCase {
+
+    private static final String DRIVER = "org.apache.derby.jdbc.ClientDriver";
+
+    private static final String USER = "cloud";
+
+    private static final String PASSWORD = "scape";
+
+    private static final int SLEEP = 1000; // ms
+
+    private File directory;
+
+    private NetworkServerControl server;
+
+    private RepositoryImpl repository;
+
+    protected void setUp() throws Exception {
+        directory = File.createTempFile("jackrabbit", "test");
+        directory.delete();
+        directory.mkdir();
+
+        server = new NetworkServerControl();
+        startDerby();
+
+        RepositoryConf conf = new RepositoryConf();
+
+        // set jdbc urls on PMs for external derby
+        // workspaces
+        PersistenceManagerConf pmc = conf.getWorkspaceConfTemplate().getPersistenceManagerConf();
+        pmc.setParameter("url", "jdbc:derby://localhost/${wsp.home}/version/db/itemState;create=true");
+        pmc.setParameter("driver", DRIVER);
+        pmc.setParameter("user", USER);
+        pmc.setParameter("password", PASSWORD);
+        // false is the default value anyway, but we want to make sure, the code does not
block forever
+        pmc.setParameter("blockOnConnectionLoss", "false");
 
-	// this test takes about 2 mins and is not very important (it verifies
-	// that an RepositoryException is thrown if the database server behind
-	// the persistence manager is killed)
+        // versioning
+        pmc = conf.getVersioningConf().getPersistenceManagerConf();
+        pmc.setParameter("url", "jdbc:derby://localhost/${rep.home}/db/itemState;create=true");
+        pmc.setParameter("driver", DRIVER);
+        pmc.setParameter("user", USER);
+        pmc.setParameter("password", PASSWORD);
+        // false is the default value anyway, but we want to make sure, the code does not
block forever
+        pmc.setParameter("blockOnConnectionLoss", "false");
+
+        RepositoryConfig config = conf.createConfig(directory.getPath());
+        config.init();
+        repository = RepositoryImpl.create(config);
+    }
+
+    protected void tearDown() throws Exception {
+        repository.shutdown();
+        stopDerby();
+        delete(directory);
+    }
+
+    private void delete(File file) {
+        File[] files = file.listFiles();
+        for (int i = 0; files != null && i < files.length; i++) {
+            delete(files[i]);
+        }
+        file.delete();
+    }
+
+    private void startDerby() throws Exception {
+        server.start(null);
+
+        // Make sure that the server has started
+        Thread.sleep(SLEEP);
+        server.ping();
+    }
+
+    private void stopDerby() throws Exception {
+        server.shutdown();
+
+        // Make sure that the server has stopped
+        Thread.sleep(SLEEP);
+    }
+
+    public void testConnectionBrokenAndReconnect() throws Exception {
+        Session session = repository.login(
+                new SimpleCredentials("admin", "admin".toCharArray()));
+        try {
+            // do something jcr-like
+            session.getRootNode().addNode("test1");
+            session.save();
+
+            // RESTART derby
+            stopDerby();
+            startDerby();
+
+            // do something jcr-like => works again, maybe data corrupted
+            session.getRootNode().addNode("test2");
+            try {
+                // an exception here means that the PM does not properly re-connect to the
database
+                session.save();
+            } catch (RepositoryException e) {
+                fail("JCR-940: add db connection autoConnect for BundleDbPersistenceManager");
+            }
+        } finally {
+            session.logout();
+        }
+    }
+
+    // this test takes about 2 mins and is not very important (it verifies
+    // that an RepositoryException is thrown if the database server behind
+    // the persistence manager is killed)
 /*
-	// external derby process
-	public void testConnectionBroken() throws Exception {
-		startExternalDerby();
-		
-		startJackrabbitWithExternalDerby();
-		Session session = helper.getSuperuserSession();
-		
-		// do something jcr-like
-		jcrWorkA(session);
-		session.save();
-		
-		killExternalDerby();
-		
-		// do something jcr-like => expect RepositoryException
-		jcrWorkB(session);
-		
-		long start = System.currentTimeMillis();
-		try {
-			// with the auto-reconnect feature in Bundle PMs, this save will trigger
-			// a loop of connection trials that will all fail, because we killed
-			// the server. this typically takes about 2 mins before finally a
-			// RepositoryException is thrown.
-			session.save();
-			
-			assertTrue("RepositoryException was expected (waiting some time is normal)", false);
-		} catch (RepositoryException e) {
-			// fine, exception is expected
-		}
-		long end = System.currentTimeMillis();
-		logger.debug("time taken: " + (end - start));
-	}
+        // external derby process
+        public void testConnectionBroken() throws Exception {
+                startExternalDerby();
+                
+                startJackrabbitWithExternalDerby();
+                Session session = helper.getSuperuserSession();
+                
+                // do something jcr-like
+                jcrWorkA(session);
+                session.save();
+                
+                killExternalDerby();
+                
+                // do something jcr-like => expect RepositoryException
+                jcrWorkB(session);
+                
+                long start = System.currentTimeMillis();
+                try {
+                        // with the auto-reconnect feature in Bundle PMs, this save will
trigger
+                        // a loop of connection trials that will all fail, because we killed
+                        // the server. this typically takes about 2 mins before finally a
+                        // RepositoryException is thrown.
+                        session.save();
+                        
+                        assertTrue("RepositoryException was expected (waiting some time is
normal)", false);
+                } catch (RepositoryException e) {
+                        // fine, exception is expected
+                }
+                long end = System.currentTimeMillis();
+                logger.debug("time taken: " + (end - start));
+        }
 */
-	
-	// external derby process
-	public void testConnectionBrokenAndReconnect() throws Exception {
-		startExternalDerby();
-		
-		startJackrabbitWithExternalDerby();
-		Session session = helper.getSuperuserSession();
-		
-		// do something jcr-like
-		jcrWorkA(session);
-		session.save();
-		
-		killExternalDerby();
-		
-		// RESTART derby
-		startExternalDerby();
-		
-		// do something jcr-like => works again, maybe data corrupted
-		jcrWorkB(session);
-		
-		// an exception here means that the PM does not properly re-connect to the database
-		session.save();
-	}
-
-	// The following test cases are just ideas for testing an embedded derby
-/*	
-	// embedded derby
-	public void testConnectionClosed() throws Exception {
-		// start derby
-		// start jackrabbit + derby pm/file system
-		// do something jcr-like
-		// SHUTDOWN derby
-		// do something jcr-like => expect RepositoryException
-	}
-
-	// embedded derby
-	public void testConnectionClosedAndReconnect() throws Exception {
-		// start derby
-		// start jackrabbit + derby pm/file system
-		// do something jcr-like
-		// SHUTDOWN derby
-		// RESTART derby
-		// do something jcr-like => everything should work normally
-	}
+
+        // The following test cases are just ideas for testing an embedded derby
+/*      
+        // embedded derby
+        public void testConnectionClosed() throws Exception {
+                // start derby
+                // start jackrabbit + derby pm/file system
+                // do something jcr-like
+                // SHUTDOWN derby
+                // do something jcr-like => expect RepositoryException
+        }
+
+        // embedded derby
+        public void testConnectionClosedAndReconnect() throws Exception {
+                // start derby
+                // start jackrabbit + derby pm/file system
+                // do something jcr-like
+                // SHUTDOWN derby
+                // RESTART derby
+                // do something jcr-like => everything should work normally
+        }
 */
 }



Mime
View raw message