Skip to content

Commit

Permalink
Import tool help/usage message should provide usage example
Browse files Browse the repository at this point in the history
Update import tool help message to be printed out when no input parameters were provided.
Text updated to include usage example.
  • Loading branch information
MishaDemianenko committed Sep 15, 2015
1 parent 015f32b commit 25ba936
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 16 deletions.
Expand Up @@ -29,6 +29,7 @@
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Args;
import org.neo4j.helpers.Strings;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.kernel.configuration.Config;
Expand Down Expand Up @@ -128,7 +129,7 @@ private void attemptRecoveryOrCheckStateOfLogicalLogs( Args arguments, String st
{
if ( recoveryChecker.recoveryNeededAt( new File( storeDir ) ) )
{
systemError.print( lines(
systemError.print( Strings.joinAsLines(
"Active logical log detected, this might be a source of inconsistencies.",
"Consider allowing the database to recover before running the consistency check.",
"Consistency checking will continue, abort if you wish to perform recovery first.",
Expand All @@ -154,7 +155,8 @@ private String determineStoreDirectory( Args arguments ) throws ToolFailureExcep
String storeDir = unprefixedArguments.get( 0 );
if ( !new File( storeDir ).isDirectory() )
{
throw new ToolFailureException( lines( String.format( "'%s' is not a directory", storeDir ) ) + usage() );
throw new ToolFailureException( Strings.joinAsLines( String.format( "'%s' is not a directory", storeDir ) ) +
usage() );
}
return storeDir;
}
Expand Down Expand Up @@ -183,7 +185,7 @@ private Config readTuningConfiguration( String storeDir, Args arguments ) throws

private String usage()
{
return lines(
return Strings.joinAsLines(
Args.jarUsage( getClass(), "[-propowner] [-recovery] [-config <neo4j.properties>] <storedir>" ),
"WHERE: <storedir> is the path to the store to check",
" -recovery to perform recovery on the store before checking",
Expand All @@ -192,15 +194,6 @@ private String usage()
);
}

private static String lines( String... content )
{
StringBuilder result = new StringBuilder();
for ( String line : content )
{
result.append( line ).append( System.getProperty( "line.separator" ) );
}
return result.toString();
}

class ToolFailureException extends Exception
{
Expand Down
Expand Up @@ -33,6 +33,8 @@
import org.neo4j.function.Function2;
import org.neo4j.helpers.Args;
import org.neo4j.helpers.Args.Option;
import org.neo4j.helpers.ArrayUtil;
import org.neo4j.helpers.Strings;
import org.neo4j.helpers.collection.IterableWrapper;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
Expand Down Expand Up @@ -62,10 +64,10 @@

import static java.lang.System.out;
import static java.nio.charset.Charset.defaultCharset;

import static org.neo4j.graphdb.factory.GraphDatabaseSettings.store_dir;
import static org.neo4j.helpers.Exceptions.launderedException;
import static org.neo4j.helpers.Format.bytes;
import static org.neo4j.helpers.Strings.TAB;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.kernel.impl.util.Converters.withDefault;
import static org.neo4j.unsafe.impl.batchimport.Configuration.BAD_FILE_NAME;
Expand Down Expand Up @@ -168,7 +170,7 @@ enum Options
+ "nodes within the same group having the same id, the first encountered will be imported "
+ "whereas consecutive such nodes will be skipped. "
+ "Skipped nodes will be logged"
+ ", containing at most number of entites specified by " + BAD_TOLERANCE.key() + "." );
+ ", containing at most number of entities specified by " + BAD_TOLERANCE.key() + "." );

private final String key;
private final Object defaultValue;
Expand Down Expand Up @@ -259,7 +261,7 @@ public static void main( String[] incomingArguments )
public static void main( String[] incomingArguments, boolean defaultSettingsSuitableForTests )
{
Args args = Args.parse( incomingArguments );
if ( asksForUsage( args ) )
if ( ArrayUtil.isEmpty( incomingArguments ) || asksForUsage( args ) )
{
printUsage( System.out );
return;
Expand Down Expand Up @@ -513,13 +515,20 @@ private static void printUsage( PrintStream out )
+ "See the chapter \"Import Tool\" in the Neo4j Manual for details on the CSV file format "
+ "- a special kind of header is required.", 80 ) )
{
out.println( "\t" + line );
out.println( TAB + line );
}
out.println( "Usage:" );
for ( Options option : Options.values() )
{
option.printUsage( out );
}

out.println( "Example:");
out.print( Strings.joinAsLines(
TAB + "bin/neo4j-import --into retail.db --id-type string --nodes:Customer customers.csv ",
TAB + "--nodes products.csv --nodes orders_header.csv,orders1.csv,orders2.csv ",
TAB + "--relationships:CONTAINS order_details.csv ",
TAB + "--relationships:ORDERED customer_orders_header.csv,orders1.csv,orders2.csv" ) );
}

private static boolean asksForUsage( Args args )
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.junit.Rule;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -93,6 +94,20 @@ public class ImportToolTest
public final Mute mute = Mute.mute( Mute.System.values() );
private int dataIndex;

@Test
public void usageMessageIncludeExample() throws Exception
{
String output = executeImportAndCatchOutput( "?" );
assertTrue( "Usage message should include example section, but was:" + output, output.contains( "Example:" ) );
}

@Test
public void usageMessagePrintedOnEmptyInputParameters()
{
String output = executeImportAndCatchOutput();
assertTrue( "Output should include usage section, but was:" + output, output.contains( "Example:" ) );
}

@Test
public void shouldImportWithAsManyDefaultsAsAvailable() throws Exception
{
Expand Down Expand Up @@ -1095,6 +1110,23 @@ public boolean accept( int line )
};
}

private String executeImportAndCatchOutput(String... arguments)
{
PrintStream originalStream = System.out;
ByteArrayOutputStream outputStreamBuffer = new ByteArrayOutputStream();
PrintStream replacement = new PrintStream( outputStreamBuffer );
System.setOut( replacement );
try
{
importTool( arguments );
return new String( outputStreamBuffer.toByteArray() );
}
finally
{
System.setOut( originalStream );
}
}

private void importTool( String... arguments )
{
ImportTool.main( arguments, true );
Expand Down
10 changes: 10 additions & 0 deletions community/kernel/src/main/java/org/neo4j/helpers/ArrayUtil.java
Expand Up @@ -372,6 +372,16 @@ public static <T> T[] union( T[] first, T[] other )
return union;
}

/**
* Check if provided array is empty
* @param array - array to check
* @return true if array is null or empty
*/
public static boolean isEmpty( Object[] array )
{
return array == null || (array.length == 0);
}

/**
* Convert an array to a String using a custom delimiter.
*
Expand Down
20 changes: 20 additions & 0 deletions community/kernel/src/main/java/org/neo4j/helpers/Strings.java
Expand Up @@ -27,6 +27,9 @@
*/
public final class Strings
{

public static final String TAB = "\t";

private Strings()
{
}
Expand Down Expand Up @@ -145,6 +148,23 @@ public static String escape( String arg )
return builder.toString();
}

/**
* Joining independent lines from provided elements into one line with {@link java.lang.System#lineSeparator} after
* each element
* @param elements - lines to join
* @return joined line
*/
public static String joinAsLines( String... elements )
{
StringBuilder result = new StringBuilder();
for ( String line : elements )
{
result.append( line ).append( System.lineSeparator() );
}
return result.toString();
}


public static void escape( Appendable output, String arg ) throws IOException
{
int len = arg.length();
Expand Down
Expand Up @@ -43,6 +43,14 @@ public void shouldProduceUnionOfTwoArrays() throws Exception
asSet( union ) );
}

@Test
public void emptyArray()
{
assertTrue( ArrayUtil.isEmpty( null ) );
assertTrue( ArrayUtil.isEmpty( new String[] {} ) );
assertFalse( ArrayUtil.isEmpty( new Long[] { 1L } ) );
}

@Test
public void shouldProduceUnionWhereFirstIsNull() throws Exception
{
Expand Down
Expand Up @@ -79,4 +79,11 @@ public void testEscape()
assertEquals( "a\\bbc", Strings.escape( "a\bbc" ) );
assertEquals( "a\\fbc", Strings.escape( "a\fbc" ) );
}

@Test
public void testJoiningLines()
{
assertEquals( "a" + System.lineSeparator() + "b" + System.lineSeparator() + "c" + System.lineSeparator(),
Strings.joinAsLines( "a", "b", "c" ) );
}
}

0 comments on commit 25ba936

Please sign in to comment.