Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jug refactor: Separate information printing logic and its invocation #115

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 5 additions & 30 deletions src/main/java/com/fasterxml/uuid/Jug.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import java.util.*;

import com.fasterxml.uuid.impl.NameBasedGenerator;
import com.fasterxml.uuid.jug.UsageInfo;

/**
* Simple command-line interface to UUID generation functionality.
*/
public class Jug
{
private final UsageInfo usageInfo = new UsageInfo();

protected final static HashMap<String,String> TYPES = new HashMap<String,String>();
static {
TYPES.put("time-based", "t");
Expand All @@ -46,34 +49,6 @@ public class Jug
OPTIONS.put("performance", "p");
OPTIONS.put("verbose", "v");
}

protected void printUsage()
{
String clsName = Jug.class.getName();
System.err.println("Usage: java "+clsName+" [options] type");
System.err.println("Where options are:");
System.err.println(" --count / -c <number>: will generate <number> UUIDs (default: 1)");
System.err.println(" --ethernet-address / -e <ether-address>: defines the ethernet address");
System.err.println(" (in xx:xx:xx:xx:xx:xx notation, usually obtained using 'ifconfig' etc)");
System.err.println(" to use with time-based UUID generation");
System.err.println(" --help / -h: lists the usage (ie. what you see now)");
System.err.println(" --name / -n: specifies");
System.err.println(" o name for name-based UUID generation");
System.err.println(" o 'information' part of tag-URI for tag-URI UUID generation");
System.err.println(" --namespace / -s: specifies");
System.err.println(" o the namespace (DNS or URL) for name-based UUID generation");
System.err.println(" o 'authority' part of tag-URI for tag-URI UUID generation;");
System.err.println(" (fully-qualified domain name, email address)");
System.err.println(" --performance / -p: measure time it takes to generate UUID(s).");
System.err.println(" [note that UUIDs are not printed out unless 'verbose' is also specified]");
System.err.println(" --verbose / -v: lists additional information about UUID generation\n (by default only UUIDs are printed out (to make it usable in scripts)");
System.err.println("And type is one of:");
System.err.println(" time-based / t: generate UUID based on current time and optional\n location information (defined with -e option)");
System.err.println(" random-based / r: generate UUID based on the default secure random number generator");
System.err.println(" name-based / n: generate UUID based on MD5 hash of given String ('name')");
System.err.println(" reordered-time-based / o: generate UUID based on current time and optional\n location information (defined with -e option)");
System.err.println(" epoch-based / e: generate UUID based on current time (as 'epoch') and random number");
}

private void printMap(Map<String,String> m, PrintStream out, boolean option)
{
Expand Down Expand Up @@ -107,7 +82,7 @@ public static void main(String[] args)

public void run(String[] args) {
if (args.length == 0) {
printUsage();
usageInfo.print();
return;
}

Expand Down Expand Up @@ -198,7 +173,7 @@ public void run(String[] args) {
}
break;
case 'h':
printUsage();
usageInfo.print();
return;
case 'n':
// Need the name
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/fasterxml/uuid/impl/LoggerFacade.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.fasterxml.uuid.impl;

import com.fasterxml.uuid.Jug;
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Wrapper we (only) need to support CLI usage (see {@link com.fasterxml.uuid.Jug}
* Wrapper we (only) need to support CLI usage (see {@link Jug}
* wherein we do not actually have logger package included; in which case we
* will print warning(s) out to {@code System.err}.
* For normal embedded usage no benefits, except if someone forgot their SLF4j API
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/fasterxml/uuid/jug/UsageInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fasterxml.uuid.jug;

import com.fasterxml.uuid.Jug;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class UsageInfo
{

private static final List<String> usageMessages = new ArrayList<>();
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved

static {
usageMessages.add("Usage: java " + Jug.class.getName() + " [options] type");
usageMessages.add("Where options are:");
usageMessages.add(" --count / -c <number>: will generate <number> UUIDs (default: 1)");
usageMessages.add(" --ethernet-address / -e <ether-address>: defines the ethernet address");
usageMessages.add(" (in xx:xx:xx:xx:xx:xx notation, usually obtained using 'ifconfig' etc)");
usageMessages.add(" to use with time-based UUID generation");
usageMessages.add(" --help / -h: lists the usage (ie. what you see now)");
usageMessages.add(" --name / -n: specifies");
usageMessages.add(" o name for name-based UUID generation");
usageMessages.add(" o 'information' part of tag-URI for tag-URI UUID generation");
usageMessages.add(" --namespace / -s: specifies");
usageMessages.add(" o the namespace (DNS or URL) for name-based UUID generation");
usageMessages.add(" o 'authority' part of tag-URI for tag-URI UUID generation;");
usageMessages.add(" (fully-qualified domain name, email address)");
usageMessages.add(" --performance / -p: measure time it takes to generate UUID(s).");
usageMessages.add(" [note that UUIDs are not printed out unless 'verbose' is also specified]");
usageMessages.add(" --verbose / -v: lists additional information about UUID generation\n (by default only UUIDs are printed out (to make it usable in scripts)");
usageMessages.add("And type is one of:");
usageMessages.add(" time-based / t: generate UUID based on current time and optional\n location information (defined with -e option)");
usageMessages.add(" random-based / r: generate UUID based on the default secure random number generator");
usageMessages.add(" name-based / n: generate UUID based on MD5 hash of given String ('name')");
usageMessages.add(" reordered-time-based / o: generate UUID based on current time and optional\n location information (defined with -e option)");
usageMessages.add(" epoch-based / e: generate UUID based on current time (as 'epoch') and random number");
}

/**
* Uses default print mechanism
*/
public void print() {
print(System.err::println);
}

/**
* Uses specific print mechanism
*
* @param printer specific printer for messages
*/
public void print(Consumer<String> printer) {
usageMessages.forEach(printer);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.uuid;
package com.fasterxml.uuid.jug;

import com.fasterxml.uuid.Jug;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -175,4 +176,4 @@ public String toString() {
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.uuid;
package com.fasterxml.uuid.jug;

import com.fasterxml.uuid.Jug;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -214,4 +215,4 @@ public static List<String> useCases() {
"m"
);
}
}
}
73 changes: 73 additions & 0 deletions src/test/java/com/fasterxml/uuid/jug/UsageInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.fasterxml.uuid.jug;

import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

public class UsageInfoTest {
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

private final String expectedMessage = "Usage: java com.fasterxml.uuid.Jug [options] type\n" +
"Where options are:\n" +
" --count / -c <number>: will generate <number> UUIDs (default: 1)\n" +
" --ethernet-address / -e <ether-address>: defines the ethernet address\n" +
" (in xx:xx:xx:xx:xx:xx notation, usually obtained using 'ifconfig' etc)\n" +
" to use with time-based UUID generation\n" +
" --help / -h: lists the usage (ie. what you see now)\n" +
" --name / -n: specifies\n" +
" o name for name-based UUID generation\n" +
" o 'information' part of tag-URI for tag-URI UUID generation\n" +
" --namespace / -s: specifies\n" +
" o the namespace (DNS or URL) for name-based UUID generation\n" +
" o 'authority' part of tag-URI for tag-URI UUID generation;\n" +
" (fully-qualified domain name, email address)\n" +
" --performance / -p: measure time it takes to generate UUID(s).\n" +
" [note that UUIDs are not printed out unless 'verbose' is also specified]\n" +
" --verbose / -v: lists additional information about UUID generation\n" +
" (by default only UUIDs are printed out (to make it usable in scripts)\n" +
"And type is one of:\n" +
" time-based / t: generate UUID based on current time and optional\n" +
" location information (defined with -e option)\n" +
" random-based / r: generate UUID based on the default secure random number generator\n" +
" name-based / n: generate UUID based on MD5 hash of given String ('name')\n" +
" reordered-time-based / o: generate UUID based on current time and optional\n" +
" location information (defined with -e option)\n" +
" epoch-based / e: generate UUID based on current time (as 'epoch') and random number" +
"\n";

@Before
public void setup() {
PrintStream stubbedStream = new PrintStream(outContent);
System.setOut(stubbedStream);

PrintStream stubbedErrStream = new PrintStream(errContent);
System.setErr(stubbedErrStream);
}

/**
* This test contains trailed '\n' symbol.
*/
@Test
public void validateMessage_defaultStdError() {
new UsageInfo().print();

String actual = errContent.toString();

assertEquals(expectedMessage, actual);
}

@Test
public void validateMessage_defaultStdOut() {
new UsageInfo().print(System.out::println);

String actual = outContent.toString();

assertEquals(expectedMessage, actual);
}
}