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.database;
21  
22  import static org.hamcrest.Matchers.containsString;
23  import static org.junit.Assert.assertThat;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.neo4j.graphdb.TransactionFailureException;
33  import org.neo4j.server.ServerTestUtils;
34  import org.neo4j.server.logging.InMemoryAppender;
35  
36  public class DatabaseTest {
37  
38      private File databaseDirectory;
39      private Database theDatabase;
40      private boolean deletionFailureOk;
41  
42      @Before
43      public void setup() throws Exception {
44          databaseDirectory = ServerTestUtils.createTempDir();
45          theDatabase = new Database( ServerTestUtils.EMBEDDED_GRAPH_DATABASE_FACTORY,
46                  databaseDirectory.getAbsolutePath() );
47      }
48  
49      @After
50      public void shutdownDatabase() throws IOException
51      {
52          this.theDatabase.shutdown();
53  
54          try
55          {
56              FileUtils.forceDelete( databaseDirectory );
57          }
58          catch ( IOException e )
59          {
60              // TODO Removed this when EmbeddedGraphDatabase startup failures closes its
61              // files properly.
62              if ( !deletionFailureOk )
63              {
64                  throw e;
65              }
66          }
67      }
68  
69      @Test
70      public void shouldLogOnSuccessfulStartup() {
71          InMemoryAppender appender = new InMemoryAppender(Database.log);
72  
73          theDatabase.startup();
74  
75          assertThat(appender.toString(), containsString("Successfully started database"));
76      }
77  
78      @Test
79      public void shouldShutdownCleanly() {
80          InMemoryAppender appender = new InMemoryAppender(Database.log);
81  
82          theDatabase.startup();
83          theDatabase.shutdown();
84  
85          assertThat(appender.toString(), containsString("Successfully shutdown database"));
86      }
87  
88      @Test(expected = TransactionFailureException.class)
89      public void shouldComplainIfDatabaseLocationIsAlreadyInUse() {
90          deletionFailureOk = true;
91          new Database( ServerTestUtils.EMBEDDED_GRAPH_DATABASE_FACTORY, theDatabase.getLocation() );
92      }
93  }