-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConsistencyCheckToolTest.java
215 lines (187 loc) · 8.15 KB
/
ConsistencyCheckToolTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.consistency;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.mockito.ArgumentCaptor;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import org.neo4j.consistency.ConsistencyCheckTool.ToolFailureException;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.LogProvider;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.neo4j.graphdb.Label.label;
public class ConsistencyCheckToolTest
{
private final TestDirectory storeDirectory = TestDirectory.testDirectory();
private final EphemeralFileSystemRule fs = new EphemeralFileSystemRule();
@Rule
public RuleChain ruleChain = RuleChain.outerRule( storeDirectory ).around( fs );
@Test
public void runsConsistencyCheck() throws Exception
{
// given
File storeDir = storeDirectory.directory();
String[] args = {storeDir.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
PrintStream systemError = mock( PrintStream.class );
// when
runConsistencyCheckToolWith( service, systemError, args );
// then
verify( service ).runFullConsistencyCheck( eq( storeDir ), any( Config.class ),
any( ProgressMonitorFactory.class ), any( LogProvider.class ), any( FileSystemAbstraction.class ),
anyBoolean() );
}
@Test
public void appliesDefaultTuningConfigurationForConsistencyChecker() throws Exception
{
// given
File storeDir = storeDirectory.directory();
String[] args = {storeDir.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
PrintStream systemOut = mock( PrintStream.class );
// when
runConsistencyCheckToolWith( service, systemOut, args );
// then
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
verify( service ).runFullConsistencyCheck( eq( storeDir ), config.capture(),
any( ProgressMonitorFactory.class ), any( LogProvider.class ), any( FileSystemAbstraction.class ),
anyBoolean() );
assertFalse( config.getValue().get( ConsistencyCheckSettings.consistency_check_property_owners ) );
}
@Test
public void passesOnConfigurationIfProvided() throws Exception
{
// given
File storeDir = storeDirectory.directory();
File configFile = storeDirectory.file( "neo4j.conf" );
Properties properties = new Properties();
properties.setProperty( ConsistencyCheckSettings.consistency_check_property_owners.name(), "true" );
properties.store( new FileWriter( configFile ), null );
String[] args = {storeDir.getPath(), "-config", configFile.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
PrintStream systemOut = mock( PrintStream.class );
// when
runConsistencyCheckToolWith( service, systemOut, args );
// then
ArgumentCaptor<Config> config = ArgumentCaptor.forClass( Config.class );
verify( service ).runFullConsistencyCheck( eq( storeDir ), config.capture(),
any( ProgressMonitorFactory.class ), any( LogProvider.class ), any( FileSystemAbstraction.class ),
anyBoolean() );
assertTrue( config.getValue().get( ConsistencyCheckSettings.consistency_check_property_owners ) );
}
@Test
public void exitWithFailureIndicatingCorrectUsageIfNoArgumentsSupplied() throws Exception
{
// given
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
String[] args = {};
PrintStream systemError = mock( PrintStream.class );
try
{
// when
runConsistencyCheckToolWith( service, systemError, args );
fail( "should have thrown exception" );
}
catch ( ConsistencyCheckTool.ToolFailureException e )
{
// then
assertThat( e.getMessage(), containsString( "USAGE:" ) );
}
}
@Test
public void exitWithFailureIfConfigSpecifiedButConfigFileDoesNotExist() throws Exception
{
// given
File configFile = storeDirectory.file( "nonexistent_file" );
String[] args = {storeDirectory.directory().getPath(), "-config", configFile.getPath()};
ConsistencyCheckService service = mock( ConsistencyCheckService.class );
PrintStream systemOut = mock( PrintStream.class );
try
{
// when
runConsistencyCheckToolWith( service, systemOut, args );
fail( "should have thrown exception" );
}
catch ( ConsistencyCheckTool.ToolFailureException e )
{
// then
assertThat( e.getMessage(), containsString( "Could not read configuration file" ) );
assertThat( e.getCause(), instanceOf( IOException.class ) );
}
verifyZeroInteractions( service );
}
@Test( expected = ToolFailureException.class )
public void failWhenStoreWasNonCleanlyShutdown() throws Exception
{
createGraphDbAndKillIt();
runConsistencyCheckToolWith( fs.get(), storeDirectory.graphDbDir().getAbsolutePath() );
}
private void createGraphDbAndKillIt() throws Exception
{
final GraphDatabaseService db = new TestGraphDatabaseFactory()
.setFileSystem( fs.get() )
.newImpermanentDatabaseBuilder( storeDirectory.graphDbDir() )
.newGraphDatabase();
try ( Transaction tx = db.beginTx() )
{
db.createNode( label( "FOO" ) );
db.createNode( label( "BAR" ) );
tx.success();
}
fs.snapshot( () -> db.shutdown() );
}
private void runConsistencyCheckToolWith( FileSystemAbstraction fileSystem, String... args )
throws IOException, ToolFailureException
{
new ConsistencyCheckTool( mock( ConsistencyCheckService.class ), fileSystem, mock( PrintStream.class ) ).run( args );
}
private void runConsistencyCheckToolWith( ConsistencyCheckService
consistencyCheckService, PrintStream systemError, String... args ) throws ToolFailureException, IOException
{
try ( FileSystemAbstraction fileSystemAbstraction = new DefaultFileSystemAbstraction() )
{
new ConsistencyCheckTool( consistencyCheckService, fileSystemAbstraction, systemError ).run( args );
}
}
}