/**
 * Copyright (c) 2002-2014 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */
package recovery;

import java.io.File;

import org.junit.Test;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.impl.MyRelTypes;
import org.neo4j.tooling.GlobalGraphOperations;

import static java.lang.Runtime.getRuntime;
import static java.lang.System.exit;
import static java.lang.System.getProperty;

import static org.junit.Assert.assertEquals;

import static org.neo4j.io.fs.FileUtils.deleteRecursively;
import static org.neo4j.test.TargetDirectory.forTest;

public class TestRecoveryMultipleDataSources
{
    private static final String dir = forTest( TestRecoveryMultipleDataSources.class ).makeGraphDbDir().getAbsolutePath();

    /**
     * Tests an issue where loading all relationship types and property indexes after
     * the neostore data source had been started internally. The db would be in a
     * state where it would need recovery for the neostore data source, as well as some
     * other data source. This would fail since eventually TxManager#getTransaction()
     * would be called, which would fail since it hadn't as of yet recovered fully.
     * Whereas that failure would happen in a listener and merely be logged, one effect
     * of it would be that there would seem to be no relationship types in the database.
     */
    @Test
    public void recoverNeoAndIndexHavingAllRelationshipTypesAfterRecovery() throws Exception
    {
        // Given (create transactions and kill process, leaving it needing for recovery)
        deleteRecursively( new File( dir ) );
        assertEquals( 0, getRuntime().exec( new String[] { "java", "-cp", getProperty( "java.class.path" ),
                getClass().getName() } ).waitFor() );

        // When
        GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( dir );

        // Then
        try(Transaction ignored = db.beginTx())
        {
            assertEquals( MyRelTypes.TEST.name(), GlobalGraphOperations.at( db ).getAllRelationshipTypes().iterator().next().name() );
        }
        finally
        {
            db.shutdown();
        }
    }

    public static void main( String[] args )
    {
        GraphDatabaseAPI db = (GraphDatabaseAPI) new GraphDatabaseFactory().newEmbeddedDatabase( dir );
        Transaction tx = db.beginTx();
        db.createNode().createRelationshipTo( db.createNode(), MyRelTypes.TEST );
        tx.success();
        tx.close();

        // TODO 2.2-future
//        db.getDependencyResolver().resolveDependency( XaDataSourceManager.class ).rotateLogicalLogs();
        tx = db.beginTx();
        db.index().forNodes( "index" ).add( db.createNode(), dir, db.createNode() );
        tx.success();
        tx.close();
        exit( 0 );
    }
}

@@ -834,32 +834,105 @@
 le;%0A
-%0Aimport org.junit.Test;%0A
+import java.io.IOException;%0A%0Aimport org.junit.Test;%0A%0Aimport org.neo4j.graphdb.DependencyResolver;
 %0Aimp
@@ -1151,16 +1151,147 @@
 lTypes;%0A
+import org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource;%0Aimport org.neo4j.kernel.impl.transaction.xaframework.PhysicalLogFile;%0A
 import o
@@ -3253,17 +3253,36 @@
 %5D args )
+ throws IOException
 %0A
-
     %7B%0A  
@@ -3559,36 +3559,41 @@
     
-// TODO 2.2-future%0A//       
+NeoStoreXaDataSource dataSource =
  db.
@@ -3635,16 +3635,24 @@
 ndency( 
+NeoStore
 XaDataSo
@@ -3659,23 +3659,16 @@
 urce
-Manager
 .class )
 .rot
@@ -3667,30 +3667,239 @@
 ss )
-.rotateLogicalLogs
+;%0A        DependencyResolver dependencyResolver = dataSource.getDependencyResolver();%0A        PhysicalLogFile physicalLogFile = dependencyResolver.resolveDependency( PhysicalLogFile.class );%0A        physicalLogFile.forceRotate
 ();%0A
+%0A
     

Fix todo in TestRecoveryMultipleDataSources