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

Fix issue #60 on OutputFormat options #67

Merged
merged 1 commit into from
Aug 28, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public void testFileMonitorWithRegex(final boolean ignoreAlreadyUsed,
throws Exception {
WaarpLoggerFactory.setLogLevel(WaarpLogLevel.WARN);
logger.warn("Start test ignoreAlreadyUsed={} regex={} recursive={}",
ignoreAlreadyUsed, regex, recursive);
ignoreAlreadyUsed, regex, recursive);
final File statusFile = new File("/tmp/status.txt");
final File stopFile = new File("/tmp/stop.txt");
final File directory = new File("/tmp/monitor");
Expand All @@ -517,7 +517,8 @@ public void testFileMonitorWithRegex(final boolean ignoreAlreadyUsed,
final File fileTest3 = new File(directory2, "test.txt");
final File fileTest4 = new File(directory2, "test.csv");

final Set<String> filesSeen = Collections.synchronizedSet(new HashSet<String>());
final Set<String> filesSeen =
Collections.synchronizedSet(new HashSet<String>());

final AtomicInteger countNew = new AtomicInteger();
final FileMonitorCommandRunnableFuture commandValidFile =
Expand Down Expand Up @@ -558,8 +559,9 @@ public void run(FileItem unused) {
};
final FileMonitor fileMonitor =
new FileMonitor("testDaemon", statusFile, stopFile, directory, null,
SMALL_WAIT, new RegexFileFilter(regex), recursive, commandValidFile,
commandRemovedFile, commandCheckIteration);
SMALL_WAIT, new RegexFileFilter(regex), recursive,
commandValidFile, commandRemovedFile,
commandCheckIteration);
fileMonitor.setIgnoreAlreadyUsed(ignoreAlreadyUsed);
commandValidFile.setMonitor(fileMonitor);
fileMonitor.setCheckDelay(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
*/
package org.waarp.common.filemonitor;

import static org.junit.Assert.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
import static org.junit.Assert.*;

/**
* RegexFileFilterTest regroups unit tests for {@link RegexFileFilter}.
Expand All @@ -34,35 +34,39 @@ public class RegexFileFilterTest {
public void testRegexFilename() {
String rx = "test.csv";
RegexFileFilter rff = new RegexFileFilter(rx);
String[] shouldMatch = {"test.csv", "subdir/test.csv"};
String[] shouldNotMatch = {"foo.csv", "subdir/foo.csv"};
String[] shouldMatch = { "test.csv", "subdir/test.csv" };
String[] shouldNotMatch = { "foo.csv", "subdir/foo.csv" };

for (String s: shouldMatch) {
assertTrue("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldMatch) {
assertTrue(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}

for (String s: shouldNotMatch) {
assertFalse("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldNotMatch) {
assertFalse(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}
}

@Test
public void testRegexFilenameWildcard() {
String rx = ".*\\.csv";
RegexFileFilter rff = new RegexFileFilter(rx);
String[] shouldMatch = {"test.csv", "subdir/test.csv"};
String[] shouldNotMatch = {"foo.txt", "subdir/foo.txt"};
String[] shouldMatch = { "test.csv", "subdir/test.csv" };
String[] shouldNotMatch = { "foo.txt", "subdir/foo.txt" };

for (String s: shouldMatch) {
assertTrue("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldMatch) {
assertTrue(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}

for (String s: shouldNotMatch) {
assertFalse("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldNotMatch) {
assertFalse(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}
}

Expand All @@ -71,20 +75,21 @@ public void testRegexPathPrefix() {
String rx = "subdir/.*";
RegexFileFilter rff = new RegexFileFilter(rx);
String[] shouldMatch = {
"subdir/test.csv", "subdir/foo.csv",
"dir/subdir/foo.txt", "othersubdir/test.csv",
"subdir/otherdir/test.csv",
"subdir/test.csv", "subdir/foo.csv", "dir/subdir/foo.txt",
"othersubdir/test.csv", "subdir/otherdir/test.csv",
};
String[] shouldNotMatch = {"foo.csv", "otherdir/foo.csv"};
String[] shouldNotMatch = { "foo.csv", "otherdir/foo.csv" };

for (String s: shouldMatch) {
assertTrue("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldMatch) {
assertTrue(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}

for (String s: shouldNotMatch) {
assertFalse("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldNotMatch) {
assertFalse(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}
}

Expand All @@ -93,25 +98,23 @@ public void testRegexPathAbsolutePrefix() {
String rx = "^subdir/.*";
RegexFileFilter rff = new RegexFileFilter(rx);
String[] shouldMatch = {
"subdir/test.csv",
"subdir/foo.csv",
"subdir/otherdir/test.csv",
"subdir/test.csv", "subdir/foo.csv", "subdir/otherdir/test.csv",
};
String[] shouldNotMatch = {
"foo.csv",
"otherdir/foo.csv",
"dir/subdir/foo.txt",
"othersubdir/test.csv",
"foo.csv", "otherdir/foo.csv", "dir/subdir/foo.txt",
"othersubdir/test.csv",
};

for (String s: shouldMatch) {
assertTrue("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldMatch) {
assertTrue(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}

for (String s: shouldNotMatch) {
assertFalse("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldNotMatch) {
assertFalse(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}
}

Expand All @@ -120,23 +123,22 @@ public void testRegexWithFlags() {
String rx = "(?i)subdir/.*";
RegexFileFilter rff = new RegexFileFilter(rx);
String[] shouldMatch = {
"subdir/test.csv",
"SUBDIR/foo.csv",
"subDiR/otherdir/test.csv",
"subdir/test.csv", "SUBDIR/foo.csv", "subDiR/otherdir/test.csv",
};
String[] shouldNotMatch = {
"foo.csv",
"otherdir/foo.csv",
"foo.csv", "otherdir/foo.csv",
};

for (String s: shouldMatch) {
assertTrue("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldMatch) {
assertTrue(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}

for (String s: shouldNotMatch) {
assertFalse("RegexFileFilter with regex '"+rx+"' should match '"+ s +"'",
doTestOnPath(rff, s));
for (String s : shouldNotMatch) {
assertFalse(
"RegexFileFilter with regex '" + rx + "' should match '" + s + "'",
doTestOnPath(rff, s));
}
}

Expand All @@ -148,7 +150,7 @@ private boolean doTestOnPath(RegexFileFilter rff, String path) {
return rff.accept(new File(path));

} catch (IOException e) {
fail("cannot create stub test file '"+path+"':"+e);
fail("cannot create stub test file '" + path + "':" + e);
} finally {
tearDownStubFile(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.waarp.common.logging.SysErrLogger;
import org.waarp.common.logging.WaarpLogger;
import org.waarp.common.logging.WaarpLoggerFactory;
import org.waarp.openr66.client.utils.OutputFormat;
import org.waarp.openr66.client.utils.OutputFormat.OUTPUTFORMAT;
import org.waarp.openr66.database.data.DbTaskRunner;
import org.waarp.openr66.protocol.configuration.Configuration;
import org.waarp.openr66.protocol.configuration.Messages;
Expand Down Expand Up @@ -147,13 +149,48 @@ public class TransferArgs {
new OptionGroup().addOption(LOGWARN_OPTION).addOption(NOTLOGWARN_OPTION);
private static final OptionGroup DELAY_OPTIONS =
new OptionGroup().addOption(DELAY_OPTION).addOption(START_OPTION);

private static final String QUIET = "quiet";
private static final String CSV = "csv";
private static final String XML = "xml";
private static final String JSON = "json";
private static final String PROPERTY = "property";
public static final String QUIET_ARG = "-" + QUIET;
public static final String CSV_ARG = "-" + CSV;
public static final String XML_ARG = "-" + XML;
public static final String JSON_ARG = "-" + JSON;
public static final String PROPERTY_ARG = "-" + PROPERTY;
private static final Option QUIET_OPTION =
Option.builder(QUIET).required(false).hasArg(false).desc(
"meaning no output at all (logs are not changed, exit value still " +
"uses 0 as Success, 1 as Warning and others as Failure)").build();
private static final Option CSV_OPTION =
Option.builder(CSV).required(false).hasArg(false).desc(
"meaning output will be in CSV format (2 lines, 1 with title, 1 " +
"with content, separator is ';')").build();
private static final Option XML_OPTION =
Option.builder(XML).required(false).hasArg(false)
.desc("meaning output will be in XML").build();
private static final Option JSON_OPTION =
Option.builder(JSON).required(false).hasArg(false)
.desc("meaning output will be in JSON").build();
private static final Option PROPERTY_OPTION =
Option.builder(PROPERTY).required(false).hasArg(false)
.desc("meaning output will be in Property format (name=value)")
.build();
private static final OptionGroup OUTPUT_OPTIONS =
new OptionGroup().addOption(QUIET_OPTION).addOption(CSV_OPTION)
.addOption(XML_OPTION).addOption(JSON_OPTION)
.addOption(PROPERTY_OPTION);

private static final Options TRANSFER_OPTIONS =
new Options().addOption(FILE_OPTION).addOption(TO_OPTION)
.addOption(NO_FOLLOW_OPTION).addOption(RULE_OPTION)
.addOption(ID_OPTION).addOption(INFO_OPTION)
.addOption(HASH_OPTION).addOption(BLOCK_OPTION)
.addOptionGroup(DELAY_OPTIONS).addOption(NOTLOG_OPTION)
.addOptionGroup(LOGWARN_OPTIONS);
.addOptionGroup(LOGWARN_OPTIONS)
.addOptionGroup(OUTPUT_OPTIONS);

public static final String SEPARATOR_SEND = "--";

Expand Down Expand Up @@ -219,6 +256,7 @@ public static TransferArgs getParamsInternal(final int rank,
return null;
}
checkLog(transferArgs1, cmd);
checkOutput(cmd);
} catch (ParseException e) {
printHelp();
logger.error("Arguments are incorrect", e);
Expand Down Expand Up @@ -398,6 +436,25 @@ private static void checkLog(final TransferArgs transferArgs1,
}
}

/**
* Check if any output format specification is set
*
* @param cmd
*/
private static void checkOutput(final CommandLine cmd) {
if (cmd.hasOption(QUIET)) {
OutputFormat.setDefaultOutput(OUTPUTFORMAT.QUIET);
} else if (cmd.hasOption(CSV)) {
OutputFormat.setDefaultOutput(OUTPUTFORMAT.CSV);
} else if (cmd.hasOption(XML)) {
OutputFormat.setDefaultOutput(OUTPUTFORMAT.XML);
} else if (cmd.hasOption(JSON)) {
OutputFormat.setDefaultOutput(OUTPUTFORMAT.JSON);
} else if (cmd.hasOption(PROPERTY)) {
OutputFormat.setDefaultOutput(OUTPUTFORMAT.PROPERTY);
}
}

/**
* Check DELAY or START
*
Expand Down
Loading