View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server;
    21
    22  import java.io.Closeable;
    23  import java.io.File;
    24  import java.io.FileInputStream;
    25  import java.io.FileOutputStream;
    26  import java.io.IOException;
    27  import java.io.InputStream;
    28  import java.io.OutputStream;
    29  import java.util.Map;
    30  import java.util.Properties;
    31  import java.util.Random;
    32
    33  import org.neo4j.kernel.AbstractGraphDatabase;
    34  import org.neo4j.kernel.EmbeddedGraphDatabase;
    35  import org.neo4j.server.database.GraphDatabaseFactory;
    36
    37  public class ServerTestUtils
    38  {
    39      public static final GraphDatabaseFactory EMBEDDED_GRAPH_DATABASE_FACTORY = new GraphDatabaseFactory()
    40      {
    41          @Override
    42          public AbstractGraphDatabase createDatabase( String databaseStoreDirectory,
    43                  Map<String, String> databaseProperties )
    44          {
    45              return new EmbeddedGraphDatabase( databaseStoreDirectory, databaseProperties );
    46          }
    47      };
    48
    49      public static File createTempDir() throws IOException
    50      {
    51          File d = File.createTempFile( "neo4j-test", "dir" );
    52          if ( !d.delete() )
    53          {
    54              throw new RuntimeException( "temp config directory pre-delete failed" );
    55          }
    56          if ( !d.mkdirs() )
    57          {
    58              throw new RuntimeException( "temp config directory not created" );
    59          }
    60          return d;
    61      }
    62
    63      public static File createTempPropertyFile() throws IOException
    64      {
    65          return createTempPropertyFile( createTempDir() );
    66      }
    67
    68      public static void writePropertiesToFile( String outerPropertyName, Map<String, String> properties, File propertyFile )
    69      {
    70          writePropertyToFile( outerPropertyName, asOneLine( properties ), propertyFile );
    71      }
    72
    73      private static String asOneLine( Map<String, String> properties )
    74      {
    75          StringBuilder builder = new StringBuilder();
    76          for ( Map.Entry<String, String> property : properties.entrySet() )
    77          {
    78              builder.append( (builder.length() > 0 ? "," : "") );
    79              builder.append( property.getKey() + "=" + property.getValue() );
    80          }
    81          return builder.toString();
    82      }
    83
    84      public static void writePropertyToFile( String name, String value, File propertyFile )
    85      {
    86          Properties properties = loadProperties( propertyFile );
    87          properties.setProperty( name, value );
    88          storeProperties( propertyFile, properties );
    89      }
    90
    91      private static void storeProperties( File propertyFile,
    92          Properties properties )
    93      {
    94          OutputStream out = null;
    95          try
    96          {
    97              out = new FileOutputStream( propertyFile );
    98              properties.store( out, "" );
    99          }
    100         catch ( IOException e )
    101         {
    102             throw new RuntimeException( e );
    103         }
    104         finally
    105         {
    106             safeClose( out );
    107         }
    108     }
    109
    110     private static Properties loadProperties( File propertyFile )
    111     {
    112         Properties properties = new Properties();
    113         if ( propertyFile.exists() )
    114         {
    115             InputStream in = null;
    116             try
    117             {
    118                 in = new FileInputStream( propertyFile );
    119                 properties.load( in );
    120             }
    121             catch ( IOException e )
    122             {
    123                 throw new RuntimeException( e );
    124             }
    125             finally
    126             {
    127                 safeClose( in );
    128             }
    129         }
    130         return properties;
    131     }
    132
    133     private static void safeClose( Closeable closeable )
    134     {
    135         if ( closeable != null )
    136         {
    137             try
    138             {
    139                 closeable.close();
    140             }
    141             catch ( IOException e )
    142             {
    143                 e.printStackTrace();
    144             }
    145         }
    146     }
    147
    148     public static File createTempPropertyFile( File parentDir ) throws IOException
    149     {
    150         return new File( parentDir, "test-" + new Random().nextInt() + ".properties" );
    151     }
    152 }