package ingraph.driver.neo4j;

import ingraph.driver.CypherDriver;
import org.neo4j.driver.v1.AccessMode;
import org.neo4j.driver.v1.AuthToken;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.test.TestGraphDatabaseFactory;

public class Neo4jDriver extends CypherDriver {

	final Driver driver;
	final GraphDatabaseService graphDb;

	public Neo4jDriver() {
		GraphDatabaseSettings.BoltConnector bolt = GraphDatabaseSettings.boltConnector("0");

		String host = "localhost";
		int port = 7687; // this is the default Neo4j port - we start one higher than this to avoid collisions
		GraphDatabaseService graphDbTemp = null;
		String address = null;
		while (port < 65000) {
			port++;
			address = String.format("%s:%d", host, port);
			System.out.println("instantiating database listening on " + address);
			try {
				graphDbTemp = new TestGraphDatabaseFactory()
					.newImpermanentDatabaseBuilder()
					.setConfig(bolt.type, "BOLT")
					.setConfig(bolt.enabled, "true")
					.setConfig(bolt.address, address)
					.newGraphDatabase();
			} catch (RuntimeException e) {
				// this is usually a org.neo4j.kernel.lifecycle.LifecycleException
				// caused by org.neo4j.helpers.PortBindException
				e.printStackTrace();
				System.out.println("Cannot connect on port " + port + ", retrying on a higher port.");
				if (graphDbTemp != null)
					graphDbTemp.shutdown();

				continue;
			}
			break;
		}
		System.out.println(graphDbTemp);
		graphDb = graphDbTemp;

		driver = GraphDatabase.driver("bolt://" + address);
	}

	@Override
	public boolean isEncrypted() {
		return driver.isEncrypted();
	}

	@Override
	public Session session() {
		return driver.session();
	}

	@Override
	public Session session(final AccessMode mode) {
		return driver.session(mode);
	}

	@Override
	public Session session(final String bookmark) {
		throw new UnsupportedOperationException("unimplemented");
	}

	@Override
	public Session session(final AccessMode mode, final String bookmark) {
		throw new UnsupportedOperationException("unimplemented");
	}

	@Override
	public Session session(Iterable<String> bookmarks) {
		throw new UnsupportedOperationException("unimplemented");
	}

	@Override
	public Session session(AccessMode mode, Iterable<String> bookmarks) {
		return null;
	}

	@Override
	public void close() {
		driver.close();
		graphDb.shutdown();
	}

}

@@ -402,16 +402,38 @@
 ctory;%0A%0A
+import java.io.File;%0A%0A
 public c
@@ -680,13 +680,23 @@
 ;%0A%09%09
-int p
+final int baseP
 ort 
@@ -789,16 +789,48 @@
 lisions%0A
+%09%09final int maximumRetry = 100;%0A
 %09%09GraphD
@@ -895,40 +895,96 @@
 ;%0A%09%09
-while (port %3C 65000) %7B%0A%09%09%09port++
+for (int retryCount = 1; true; retryCount++) %7B%0A%09%09%09final int port = basePort + retryCount
 ;%0A%09%09
@@ -1197,16 +1197,73 @@
 Builder(
+new File(%22target/test-data/impermanent-db-%22 + retryCount)
 )%0A%09%09%09%09%09.
@@ -1548,24 +1548,75 @@
 ndException%0A
+%09%09%09%09if (retryCount %3E= maximumRetry)%0A%09%09%09%09%09throw e;%0A%0A
 %09%09%09%09e.printS

Change DB folder on retry in Neo4jDriver for tests