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