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

Display a warning message for Beta and Exerimental tools. #4429

Merged
merged 3 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.text.DecimalFormat;
import java.lang.Thread;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -175,6 +174,8 @@ public Object instanceMainPostParseArgs() {
printStartupMessage(startDateTime);
}

printProductionWarning();

try {
return runTool();
} finally {
Expand Down Expand Up @@ -278,6 +279,61 @@ protected void printStartupMessage(final ZonedDateTime startDateTime) {
catch (final Exception e) { /* Unpossible! */ }
}

/**
* If this tool is either Experimental or Beta, return a warning message advising against use in production
* environment.
* @param useTerminalColor true if the message should include highlighting terminal colorization
* @return a warning message if the tool is Beta or Experimental, otherwise null
*/
protected String getProductionWarning(final boolean useTerminalColor) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

getToolStatusWarning() might be clearer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

final String KNRM = "\u001B[0m"; // reset
final String BOLDRED = "\u001B[1m\u001B[31m";
final int BORDER_LENGTH = 60;

String warningMessage = null;
if (isBetaFeature()) {
warningMessage = String.format(
"\n\n%s %s\n\n Warning: %s is a BETA tool and is not yet ready for use in production\n\n %s%s\n\n",
useTerminalColor ? BOLDRED : "",
Utils.dupChar('!', BORDER_LENGTH),
this.getClass().getSimpleName(),
Utils.dupChar('!', BORDER_LENGTH),
useTerminalColor ? KNRM : ""
);
}
else if (isExperimentalFeature()) {
warningMessage = String.format(
"\n\n%s %s\n\n Warning: %s is an EXPERIMENTAL tool and should not be used for production\n\n %s%s\n\n",
useTerminalColor ? BOLDRED : "",
Utils.dupChar('!', BORDER_LENGTH),
this.getClass().getSimpleName(),
Utils.dupChar('!', BORDER_LENGTH),
useTerminalColor ? KNRM : ""
);
}
return warningMessage;
}

/**
* If a tool is either Experimental or Beta, log a warning against use in production a environment.
*/
protected void printProductionWarning() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe warnAboutToolStatus() would be clearer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

final String warningMessage = getProductionWarning(!(System.console() == null));
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this check isn't 100% reliable, I would take it out. Printing the literal codes in non-interactive use is not that big of a deal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

if (warningMessage != null) {
logger.warn(warningMessage);
}
}

/**
* @return true if this tool has {@code BetaFeature} status.
*/
public boolean isBetaFeature() { return this.getClass().getAnnotation(BetaFeature.class) != null; }

/**
* @return true if this tool has {@code ExperimentalFeature} status.
*/
public boolean isExperimentalFeature() { return this.getClass().getAnnotation(ExperimentalFeature.class) != null; }

/**
* @return The name of this toolkit. The default implementation uses "Implementation-Title" from the
* jar manifest, or (if that's not available) the package name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,69 @@ private File createTempExpansionTestFile(final String extension, final String fi
}
return tempFile;
}

@CommandLineProgramProperties(
summary = "ExperimentalTool",
oneLineSummary = "ExperimentalTool",
programGroup = TestProgramGroup.class
)
@ExperimentalFeature
private static class TestExperimentalTool extends CommandLineProgram {

@Override
protected Object doWork() {
return null;
}
}

@CommandLineProgramProperties(
summary = "BetaTool",
oneLineSummary = "BetaTool",
programGroup = TestProgramGroup.class
)
@BetaFeature
private static class TestBetaTool extends CommandLineProgram {
@Override
protected Object doWork() {
return null;
}
}

@CommandLineProgramProperties(
summary = "ProductionTool",
oneLineSummary = "ProductionTool",
programGroup = TestProgramGroup.class
)
private static class TestProductionTool extends CommandLineProgram {
@Override
protected Object doWork() {
return null;
}
}

@DataProvider(name = "toolMaturityLevels")
public Object[][] getToolMaturityLevels() {
return new Object[][]{
{new TestExperimentalTool(), true, false, "EXPERIMENTAL"},
{new TestBetaTool(), false, true, "BETA"},
{new TestProductionTool(), false, false, null}
};
}

@Test(dataProvider = "toolMaturityLevels")
public void testToolMaturityLevel(
final CommandLineProgram clp,
final boolean isExperimental,
final boolean isBeta,
final String warningSentinel)
{
Assert.assertEquals(clp.isBetaFeature(), isBeta);
Assert.assertEquals(clp.isExperimentalFeature(), isExperimental);
if (warningSentinel == null) {
Assert.assertEquals(clp.getProductionWarning(true), null);
} else {
Assert.assertTrue(clp.getProductionWarning(true).contains(warningSentinel));
}
}

}