Skip to content

feat(test): cowtowncoder#87 Add tests to jug #113

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

Merged
merged 4 commits into from
Jun 8, 2024
Merged
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
226 changes: 115 additions & 111 deletions src/main/java/com/fasterxml/uuid/Jug.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Jug
OPTIONS.put("verbose", "v");
}

protected static void printUsage()
protected void printUsage()
{
String clsName = Jug.class.getName();
System.err.println("Usage: java "+clsName+" [options] type");
Expand Down Expand Up @@ -75,7 +75,7 @@ protected static void printUsage()
System.err.println(" epoch-based / e: generate UUID based on current time (as 'epoch') and random number");
}

private static void printMap(Map<String,String> m, PrintStream out, boolean option)
private void printMap(Map<String,String> m, PrintStream out, boolean option)
{
int i = 0;
int len = m.size();
Expand All @@ -102,6 +102,10 @@ private static void printMap(Map<String,String> m, PrintStream out, boolean opti

public static void main(String[] args)
{
new Jug().run(args);
}

public void run(String[] args) {
Comment on lines 103 to +108
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively the core of this change on this file, I destaticised most of the methods, the only one that is still static is main.

Take note that I have tried my best to not touch too much of the main file, just shuffling the methods around so it is easier to test.

Also take note that Intellij appears to have applied its own automated styling guidelines, so I would like to apologise in advance for the changes.

if (args.length == 0) {
printUsage();
return;
Expand All @@ -123,7 +127,7 @@ public static void main(String[] args)
if (tmp == null) {
if (!TYPES.containsValue(type)) {
System.err.println("Unrecognized UUID generation type '"+
type+"'; currently available ones are:");
type+"'; currently available ones are:");
printMap(TYPES, System.err, false);
System.err.println();
System.exit(1);
Expand All @@ -136,7 +140,7 @@ public static void main(String[] args)

NoArgGenerator noArgGenerator = null; // random- or time-based
StringArgGenerator nameArgGenerator = null; // name-based

for (int i = 0; i < count; ++i) {
String opt = args[i];

Expand Down Expand Up @@ -170,46 +174,46 @@ public static void main(String[] args)
try {
String next;
switch (option) {
case 'c':
// Need a number now:
next = args[++i];
try {
genCount = Integer.parseInt(next);
} catch (NumberFormatException nex) {
System.err.println("Invalid number argument for option '"+opt+"', exiting.");
System.exit(1);
}
if (genCount < 1) {
System.err.println("Invalid number argument for option '"+opt+"'; negative numbers not allowed, ignoring (defaults to 1).");
}
break;
case 'e':
// Need the ethernet address:
next = args[++i];
try {
addr = EthernetAddress.valueOf(next);
} catch (NumberFormatException nex) {
System.err.println("Invalid ethernet address for option '"+opt+"', error: "+nex.toString());
System.exit(1);
}
break;
case 'h':
printUsage();
return;
case 'n':
// Need the name
name = args[++i];
break;
case 'p': // performance:
performance = true;
break;
case 's':
// Need the namespace id
nameSpace = args[++i];
break;
case 'v':
verbose = true;
break;
case 'c':
// Need a number now:
next = args[++i];
try {
genCount = Integer.parseInt(next);
} catch (NumberFormatException nex) {
System.err.println("Invalid number argument for option '"+opt+"', exiting.");
System.exit(1);
}
if (genCount < 1) {
System.err.println("Invalid number argument for option '"+opt+"'; negative numbers not allowed, ignoring (defaults to 1).");
}
break;
case 'e':
// Need the ethernet address:
next = args[++i];
try {
addr = EthernetAddress.valueOf(next);
} catch (NumberFormatException nex) {
System.err.println("Invalid ethernet address for option '"+opt+"', error: "+nex.toString());
System.exit(1);
}
break;
case 'h':
printUsage();
return;
case 'n':
// Need the name
name = args[++i];
break;
case 'p': // performance:
performance = true;
break;
case 's':
// Need the namespace id
nameSpace = args[++i];
break;
case 'v':
verbose = true;
break;
}
} catch (IndexOutOfBoundsException ie) {
// We get here when an arg is missing...
Expand All @@ -227,80 +231,80 @@ public static void main(String[] args)
boolean usesRnd = false;

switch (typeC) {
case 't': // time-based
case 'o': // reordered-time-based (Version 6)
// 30-Jun-2022, tatu: Is this true? My former self must have had his
// reasons so leaving as is but... odd.
usesRnd = true;
// No address specified? Need a dummy one...
if (addr == null) {
if (verbose) {
System.out.print("(no address specified, generating dummy address: ");
case 't': // time-based
case 'o': // reordered-time-based (Version 6)
// 30-Jun-2022, tatu: Is this true? My former self must have had his
// reasons so leaving as is but... odd.
usesRnd = true;
// No address specified? Need a dummy one...
if (addr == null) {
if (verbose) {
System.out.print("(no address specified, generating dummy address: ");
}
addr = EthernetAddress.constructMulticastAddress(new java.util.Random(System.currentTimeMillis()));
if (verbose) {
System.out.print(addr.toString());
System.out.println(")");
}
}
addr = EthernetAddress.constructMulticastAddress(new java.util.Random(System.currentTimeMillis()));
if (verbose) {
System.out.print(addr.toString());
System.out.println(")");
noArgGenerator = (typeC == 't')
? Generators.timeBasedGenerator(addr)
: Generators.timeBasedReorderedGenerator(addr);
break;
case 'r': // random-based
usesRnd = true;
{
SecureRandom r = new SecureRandom();
if (verbose) {
System.out.print("(using secure random generator, info = '"+r.getProvider().getInfo()+"')");
}
noArgGenerator = Generators.randomBasedGenerator(r);
}
}
noArgGenerator = (typeC == 't')
? Generators.timeBasedGenerator(addr)
: Generators.timeBasedReorderedGenerator(addr);
break;
case 'r': // random-based
usesRnd = true;
{
SecureRandom r = new SecureRandom();
if (verbose) {
System.out.print("(using secure random generator, info = '"+r.getProvider().getInfo()+"')");
break;
case 'e': // epoch-time-based
usesRnd = true;
{
SecureRandom r = new SecureRandom();
if (verbose) {
System.out.print("(using secure random generator, info = '"+r.getProvider().getInfo()+"')");
}
noArgGenerator = Generators.timeBasedEpochGenerator(r);
}
noArgGenerator = Generators.randomBasedGenerator(r);
}
break;
case 'e': // epoch-time-based
usesRnd = true;
{
SecureRandom r = new SecureRandom();
if (verbose) {
System.out.print("(using secure random generator, info = '"+r.getProvider().getInfo()+"')");
break;
case 'm': // random-epoch-time-based
usesRnd = true;
{
SecureRandom r = new SecureRandom();
if (verbose) {
System.out.print("(using secure random generator, info = '"+r.getProvider().getInfo()+"')");
}
noArgGenerator = Generators.timeBasedEpochRandomGenerator(r);
}
noArgGenerator = Generators.timeBasedEpochGenerator(r);
}
break;
case 'm': // random-epoch-time-based
usesRnd = true;
{
SecureRandom r = new SecureRandom();
if (verbose) {
System.out.print("(using secure random generator, info = '"+r.getProvider().getInfo()+"')");
break;
case 'n': // name-based
if (nameSpace == null) {
System.err.println("--name-space (-s) - argument missing when using method that requires it, exiting.");
System.exit(1);
}
noArgGenerator = Generators.timeBasedEpochRandomGenerator(r);
}
break;
case 'n': // name-based
if (nameSpace == null) {
System.err.println("--name-space (-s) - argument missing when using method that requires it, exiting.");
System.exit(1);
}
if (name == null) {
System.err.println("--name (-n) - argument missing when using method that requires it, exiting.");
System.exit(1);
}
if (typeC == 'n') {
String orig = nameSpace;
nameSpace = nameSpace.toLowerCase();
if (nameSpace.equals("url")) {
nsUUID = NameBasedGenerator.NAMESPACE_URL;
} else if (nameSpace.equals("dns")) {
nsUUID = NameBasedGenerator.NAMESPACE_DNS;
} else {
System.err.println("Unrecognized namespace '"+orig
+"'; only DNS and URL allowed for name-based generation.");
if (name == null) {
System.err.println("--name (-n) - argument missing when using method that requires it, exiting.");
System.exit(1);
}
}
nameArgGenerator = Generators.nameBasedGenerator(nsUUID);
break;
if (typeC == 'n') {
String orig = nameSpace;
nameSpace = nameSpace.toLowerCase();
if (nameSpace.equals("url")) {
nsUUID = NameBasedGenerator.NAMESPACE_URL;
} else if (nameSpace.equals("dns")) {
nsUUID = NameBasedGenerator.NAMESPACE_DNS;
} else {
System.err.println("Unrecognized namespace '"+orig
+"'; only DNS and URL allowed for name-based generation.");
System.exit(1);
}
}
nameArgGenerator = Generators.nameBasedGenerator(nsUUID);
break;
}

// And then let's rock:
Expand Down
Loading