1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }