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.rrd;
21  
22  import org.apache.commons.configuration.Configuration;
23  import org.apache.commons.configuration.MapConfiguration;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.neo4j.kernel.ImpermanentGraphDatabase;
28  import org.neo4j.server.configuration.Configurator;
29  import org.rrd4j.core.RrdDb;
30  
31  import java.io.IOException;
32  import java.util.HashMap;
33  
34  import static org.hamcrest.CoreMatchers.is;
35  import static org.hamcrest.Matchers.endsWith;
36  import static org.hamcrest.Matchers.startsWith;
37  import static org.junit.Assert.assertThat;
38  
39  public class RrdFactoryTest
40  {
41      private Configuration config;
42      private ImpermanentGraphDatabase db;
43  
44      @Before
45      public void setUp() throws Exception
46      {
47          config = new MapConfiguration( new HashMap<String, String>() );
48          db = new ImpermanentGraphDatabase();
49      }
50  
51      @After
52      public void tearDown() throws Exception
53      {
54          try
55          {
56              db.shutdown();
57          } catch ( Exception e )
58          {
59              ;
60          }
61      }
62  
63      @Test
64      public void shouldTakeDirectoryLocationFromConfig() throws Exception
65      {
66          String expected = "target/rrd-test";
67          config.addProperty( Configurator.RRDB_LOCATION_PROPERTY_KEY, expected );
68          TestableRrdFactory factory = createRrdFactory();
69  
70          factory.createRrdDbAndSampler( db, new NullJobScheduler() );
71  
72          assertThat( factory.directoryUsed, is( expected ) );
73      }
74  
75  
76      @Test
77      public void shouldCreateRrdInAGoodDefaultPlace() throws Exception
78      {
79          TestableRrdFactory factory = createRrdFactory();
80  
81          RrdDb rrdDbAndSampler = factory.createRrdDbAndSampler( db, new NullJobScheduler() );
82  
83          assertThat( factory.directoryUsed, startsWith( db.getStoreDir() ) );
84          assertThat( factory.directoryUsed, endsWith( "rrd" ) );
85  
86          rrdDbAndSampler.close();
87      }
88  
89      private TestableRrdFactory createRrdFactory()
90      {
91          return new TestableRrdFactory( config );
92      }
93  
94      private static class TestableRrdFactory extends RrdFactory
95      {
96          public String directoryUsed;
97  
98          public TestableRrdFactory( Configuration config )
99          {
100             super( config );
101         }
102 
103         @Override
104         protected RrdDb createRrdb( String inDirectory, int stepSize, int stepsPerArchive,
105                                     Sampleable... sampleables ) throws IOException
106         {
107             directoryUsed = inDirectory;
108             return super.createRrdb( inDirectory, stepSize, stepsPerArchive, sampleables );
109         }
110     }
111 
112     private static class NullJobScheduler implements JobScheduler
113     {
114         @Override
115         public void scheduleToRunEveryXSeconds( Job job, int runEveryXSeconds )
116         {
117 
118         }
119     }
120 }