Search in sources :

Example 31 with TestGraphDatabaseFactory

use of org.neo4j.test.TestGraphDatabaseFactory in project neo4j by neo4j.

the class TestIndexDelectionFs method doBefore.

@BeforeClass
public static void doBefore() throws IOException {
    File directory = new File("target/test-data/deletion");
    FileUtils.deleteRecursively(directory);
    db = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newEmbeddedDatabase(directory);
}
Also used : TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 32 with TestGraphDatabaseFactory

use of org.neo4j.test.TestGraphDatabaseFactory in project neo4j by neo4j.

the class TestAutoIndexReopen method startDb.

@Before
public void startDb() {
    graphDb = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().setConfig(new HashMap<>()).newGraphDatabase();
    try (Transaction tx = graphDb.beginTx()) {
        // Create the node and relationship auto-indexes
        graphDb.index().getNodeAutoIndexer().setEnabled(true);
        graphDb.index().getNodeAutoIndexer().startAutoIndexingProperty("nodeProp");
        graphDb.index().getRelationshipAutoIndexer().setEnabled(true);
        graphDb.index().getRelationshipAutoIndexer().startAutoIndexingProperty("type");
        tx.success();
    }
    try (Transaction tx = graphDb.beginTx()) {
        Node node1 = graphDb.createNode();
        Node node2 = graphDb.createNode();
        Node node3 = graphDb.createNode();
        id1 = node1.getId();
        id2 = node2.getId();
        id3 = node3.getId();
        Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName("FOO"));
        rel.setProperty("type", "FOO");
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Before(org.junit.Before)

Example 33 with TestGraphDatabaseFactory

use of org.neo4j.test.TestGraphDatabaseFactory in project neo4j by neo4j.

the class LuceneRecoveryIT method testHardCoreRecovery.

@Test
public void testHardCoreRecovery() throws Exception {
    String path = "target/hcdb";
    File hcdb = new File(path);
    FileUtils.deleteRecursively(hcdb);
    Process process = Runtime.getRuntime().exec(new String[] { ProcessUtil.getJavaExecutable().toString(), "-cp", ProcessUtil.getClassPath(), Inserter.class.getName(), path });
    // Let it run for a while and then kill it, and wait for it to die
    awaitFile(new File(path, "started"));
    Thread.sleep(5000);
    process.destroy();
    process.waitFor();
    final GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(hcdb);
    try (Transaction transaction = db.beginTx()) {
        assertTrue(db.index().existsForNodes("myIndex"));
        Index<Node> index = db.index().forNodes("myIndex");
        for (Node node : db.getAllNodes()) {
            for (String key : node.getPropertyKeys()) {
                String value = (String) node.getProperty(key);
                boolean found = false;
                for (Node indexedNode : index.get(key, value)) {
                    if (indexedNode.equals(node)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throw new IllegalStateException(node + " has property '" + key + "'='" + value + "', but not in index");
                }
            }
        }
    } catch (Throwable e) {
        if (Exceptions.contains(e, CorruptIndexException.class) || exceptionContainsStackTraceElementFromPackage(e, "org.apache.lucene")) {
            // On some machines and during some circumstances a lucene index may become
            // corrupted during a crash. This is out of our control and since this test
            // is about a legacy (a.k.a. manual index) the db cannot just re-populate the
            // index automatically. We have to consider this an OK scenario and we cannot
            // verify the index any further if it happens.
            System.err.println("Lucene exception happened during recovery after a real crash. " + "It may be that the index is corrupt somehow and this is out of control and not " + "something this test can reall improve on right now. Printing the exception for reference");
            e.printStackTrace();
            return;
        }
        // This was another unknown exception, throw it so that the test fails with it
        throw e;
    }
    // Added due to a recovery issue where the lucene data source write wasn't released properly after recovery.
    Thread t = new Thread() {

        @Override
        public void run() {
            try (Transaction tx = db.beginTx()) {
                Index<Node> index = db.index().forNodes("myIndex");
                index.add(db.createNode(), "one", "two");
                tx.success();
            }
        }
    };
    t.start();
    t.join();
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Transaction(org.neo4j.graphdb.Transaction) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) File(java.io.File) Test(org.junit.Test)

Example 34 with TestGraphDatabaseFactory

use of org.neo4j.test.TestGraphDatabaseFactory in project neo4j by neo4j.

the class CypherSessionTest method shouldReturnASingleNode.

@Test
public void shouldReturnASingleNode() throws Throwable {
    GraphDatabaseFacade graphdb = (GraphDatabaseFacade) new TestGraphDatabaseFactory().newImpermanentDatabase();
    Database database = new WrappedDatabase(graphdb);
    CypherExecutor executor = new CypherExecutor(database, NullLogProvider.getInstance());
    executor.start();
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getScheme()).thenReturn("http");
    when(request.getRemoteAddr()).thenReturn("127.0.0.1");
    when(request.getRemotePort()).thenReturn(5678);
    when(request.getServerName()).thenReturn("127.0.0.1");
    when(request.getServerPort()).thenReturn(7474);
    when(request.getRequestURI()).thenReturn("/");
    try {
        CypherSession session = new CypherSession(executor, NullLogProvider.getInstance(), request);
        Pair<String, String> result = session.evaluate("create (a) return a");
        assertThat(result.first(), containsString("Node[0]"));
    } finally {
        graphdb.shutdown();
    }
}
Also used : CypherExecutor(org.neo4j.server.database.CypherExecutor) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Database(org.neo4j.server.database.Database) WrappedDatabase(org.neo4j.server.database.WrappedDatabase) WrappedDatabase(org.neo4j.server.database.WrappedDatabase) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) CypherSession(org.neo4j.server.rest.management.console.CypherSession) Test(org.junit.Test)

Example 35 with TestGraphDatabaseFactory

use of org.neo4j.test.TestGraphDatabaseFactory in project neo4j by neo4j.

the class RestfulGraphDatabaseTest method doBefore.

@BeforeClass
public static void doBefore() throws IOException {
    graph = (GraphDatabaseFacade) new TestGraphDatabaseFactory().newImpermanentDatabase();
    database = new WrappedDatabase(graph);
    helper = new GraphDbHelper(database);
    output = new EntityOutputFormat(new JsonFormat(), URI.create(BASE_URI), null);
    service = new TransactionWrappingRestfulGraphDatabase(graph, new RestfulGraphDatabase(new JsonFormat(), output, new DatabaseActions(new LeaseManager(Clocks.fakeClock()), true, database.getGraph()), new ConfigAdapter(Config.embeddedDefaults())));
}
Also used : GraphDbHelper(org.neo4j.server.rest.domain.GraphDbHelper) JsonFormat(org.neo4j.server.rest.repr.formats.JsonFormat) LeaseManager(org.neo4j.server.rest.paging.LeaseManager) ConfigAdapter(org.neo4j.server.plugins.ConfigAdapter) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) WrappedDatabase(org.neo4j.server.database.WrappedDatabase) EntityOutputFormat(org.neo4j.test.server.EntityOutputFormat) BeforeClass(org.junit.BeforeClass)

Aggregations

TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)156 Test (org.junit.Test)70 Transaction (org.neo4j.graphdb.Transaction)62 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)61 File (java.io.File)34 Node (org.neo4j.graphdb.Node)28 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)26 BeforeClass (org.junit.BeforeClass)25 Before (org.junit.Before)22 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)9 Relationship (org.neo4j.graphdb.Relationship)8 Result (org.neo4j.graphdb.Result)6 GraphDatabaseBuilder (org.neo4j.graphdb.factory.GraphDatabaseBuilder)6 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)6 GraphDatabaseShellServer (org.neo4j.shell.kernel.GraphDatabaseShellServer)6 HashMap (java.util.HashMap)5 DependencyResolver (org.neo4j.graphdb.DependencyResolver)5 PageCache (org.neo4j.io.pagecache.PageCache)5 WrappedDatabase (org.neo4j.server.database.WrappedDatabase)5 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4