private List<String> getStdOut() { List<String> lines = suppress.getOutputVoice().lines(); // Remove empty lines return lines.stream().filter( line -> !line.equals( "" ) ).collect( Collectors.toList() ); }
@BeforeClass public static void ensureServerNotRunning() throws Exception { System.setProperty( "org.neo4j.useInsecureCertificateGeneration", "true" ); suppressAll().call( (Callable<Void>) () -> { ServerHolder.ensureNotRunning(); return null; } ); } }
public static SuppressOutput suppress( Suppressible... suppressibles ) { return new SuppressOutput( suppressibles ); }
public <T> T call( Callable<T> callable ) throws Exception { captureVoices(); boolean failure = true; try { T result = callable.call(); failure = false; return result; } finally { releaseVoices( voices, failure ); } }
@Test void shouldUseArgumentsFoundInside_f_Argument() throws FileNotFoundException, CommandFailed, IncorrectUsage { // given File report = testDir.file( "report" ); ImportCommand importCommand = new ImportCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(), new RealOutsideWorld( System.out, System.err, new ByteArrayInputStream( new byte[0] ) ) ); File nodesFile = createTextFile( "nodes.csv", ":ID", "1", "2" ); String pathWithEscapedSpaces = escapeSpaces( nodesFile.getAbsolutePath() ); String reportEscapedPath = escapeSpaces( report.getAbsolutePath() ); File argFile = createTextFile( "args.txt", "--database=foo", "--nodes=" + pathWithEscapedSpaces, "--report-file=" + reportEscapedPath ); String[] arguments = {"-f", argFile.getAbsolutePath()}; // when importCommand.execute( arguments ); // then assertTrue( suppressOutput.getOutputVoice().containsMessage( "IMPORT DONE" ) ); assertTrue( suppressOutput.getErrorVoice().containsMessage( nodesFile.getAbsolutePath() ) ); assertTrue( suppressOutput.getOutputVoice().containsMessage( "2 nodes" ) ); }
@Test public void shouldPrintStackTraceOnInputExceptionIfToldTo() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; // WHEN data file contains more columns than header file int extraColumns = 3; try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData( false, config, nodeIds, TRUE, Charset.defaultCharset(), extraColumns ) .getAbsolutePath(), "--stacktrace" ); fail( "Should have thrown exception" ); } catch ( InputException e ) { // THEN assertTrue( suppressOutput.getErrorVoice().containsMessage( e.getClass().getName() ) ); assertTrue( e.getMessage().contains( "Extra column not present in header on line" ) ); } }
@Override protected SuppressOutput createField( ExtensionContext extensionContext ) { return SuppressOutput.suppressAll(); }
public void releaseVoices( boolean failure ) { releaseVoices( voices, failure ); }
public Voice getOutputVoice() { return getVoice( System.out ); }
public static SuppressOutput suppressAll() { return suppress( System.out, System.err, java_util_logging ); }
@Override public void beforeEach( ExtensionContext context ) { getStoredValue( context ).captureVoices(); } }
@Test public void shouldFailIfHeaderHasLessColumnsThanData() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.TABS; // WHEN data file contains more columns than header file int extraColumns = 3; try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--delimiter", "TAB", "--array-delimiter", String.valueOf( config.arrayDelimiter() ), "--nodes", nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData( false, config, nodeIds, TRUE, Charset.defaultCharset(), extraColumns ) .getAbsolutePath(), "--relationships", relationshipHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER + relationshipData( false, config, nodeIds, TRUE, true ).getAbsolutePath() ); fail( "Should have thrown exception" ); } catch ( InputException e ) { // THEN assertTrue( suppressOutput.getOutputVoice().containsMessage( "IMPORT FAILED" ) ); assertFalse( suppressOutput.getErrorVoice().containsMessage( e.getClass().getName() ) ); assertTrue( e.getMessage().contains( "Extra column not present in header on line" ) ); } }
@Test public void shouldPrintUserFriendlyMessageAboutUnsupportedMultilineFields() throws Exception { // GIVEN File data = data( ":ID,name", "1,\"one\ntwo\nthree\"", "2,four" ); try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--multiline-fields", "false" ); fail( "Should have failed" ); } catch ( InputException e ) { // THEN assertTrue( suppressOutput.getErrorVoice().containsMessage( "Detected field which spanned multiple lines" ) ); assertTrue( suppressOutput.getErrorVoice().containsMessage( "multiline-fields" ) ); } }
@Override public void evaluate() throws Throwable { captureVoices(); boolean failure = true; try { base.evaluate(); failure = false; } finally { releaseVoices( voices, failure ); } } };
public void captureVoices() { Voice[] voices = new Voice[suppressibles.length]; boolean ok = false; try { for ( int i = 0; i < voices.length; i++ ) { voices[i] = suppressibles[i].suppress(); } ok = true; } finally { if ( !ok ) { releaseVoices( voices, false ); } } this.voices = voices; }
public Voice getErrorVoice() { return getVoice( System.err ); }
@Test public void shouldNotThrowNullPointerExceptionIfConfigurationValidationFails() throws Exception { // given ServerBootstrapper serverBootstrapper = new ServerBootstrapper() { @Override protected GraphFactory createGraphFactory( Config config ) { return new CommunityGraphFactory(); } @Override protected NeoServer createNeoServer( GraphFactory graphFactory, Config config, GraphDatabaseDependencies dependencies ) { return mock( NeoServer.class ); } }; File dir = Files.createTempDirectory( "test-server-bootstrapper" ).toFile(); dir.deleteOnExit(); // when serverBootstrapper.start( dir, Optional.empty(), MapUtil.stringMap( database_path.name(), homeDir.absolutePath().getAbsolutePath() ) ); // then no exceptions are thrown and assertThat( suppress.getOutputVoice().lines(), not( empty() ) ); } }
@AfterClass public static void stopServer() throws Exception { suppressAll().call( (Callable<Void>) () -> { server.stop(); return null; } ); }
List<String> errorLines = suppressOutput.getErrorVoice().lines(); assertContains( errorLines, "Starting a database on these store files will likely fail or observe inconsistent records" );
@Override public void afterEach( ExtensionContext context ) { getStoredValue( context ).releaseVoices( context.getExecutionException().isPresent() ); removeStoredValue( context ); }