| | |
| | | package com.forgerock.opendj.ldap.tools; |
| | | |
| | | import static org.fest.assertions.Assertions.*; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.MAX_LINE_WIDTH; |
| | | import static com.forgerock.opendj.cli.Utils.wrapText; |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | import static org.forgerock.util.Utils.*; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | |
| | | import java.io.PrintStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.opendj.ldap.ByteStringBuilder; |
| | | import org.testng.annotations.AfterMethod; |
| | | import org.testng.annotations.BeforeMethod; |
| | | import org.testng.annotations.DataProvider; |
| | | import org.testng.annotations.Test; |
| | | |
| | | @SuppressWarnings("javadoc") |
| | | public class MakeLDIFTestCase extends ToolsTestCase { |
| | | public class MakeLDIFITCase extends ToolsITCase { |
| | | |
| | | @DataProvider(name = "validArguments") |
| | | Object[][] createValidArguments() throws Exception { |
| | | private ByteStringBuilder out; |
| | | private ByteStringBuilder err; |
| | | private PrintStream outStream; |
| | | private PrintStream errStream; |
| | | |
| | | @BeforeMethod |
| | | private void refreshStreams() { |
| | | out = new ByteStringBuilder(); |
| | | err = new ByteStringBuilder(); |
| | | outStream = new PrintStream(out.asOutputStream()); |
| | | errStream = new PrintStream(err.asOutputStream()); |
| | | } |
| | | |
| | | @AfterMethod |
| | | private void closeStreams() { |
| | | closeSilently(outStream, errStream); |
| | | } |
| | | |
| | | @DataProvider |
| | | Object[][] validArguments() throws Exception { |
| | | return new Object[][] { |
| | | { // check that help message is displayed |
| | | args("-H"), |
| | | expectedOutput(INFO_MAKELDIF_TOOL_DESCRIPTION.get()) }, |
| | | |
| | | { args("-c", "numusers=1", "example.template"), |
| | | // 2 base entries + users |
| | | expectedErrOutput(INFO_MAKELDIF_PROCESSING_COMPLETE.get(3)) }, |
| | | expectedOutput(INFO_MAKELDIF_PROCESSING_COMPLETE.get(3)) }, |
| | | |
| | | { args("-c", "numusers=5", "example.template"), |
| | | // 2 base entries + users |
| | | expectedErrOutput(INFO_MAKELDIF_PROCESSING_COMPLETE.get(7)) }, |
| | | expectedOutput(INFO_MAKELDIF_PROCESSING_COMPLETE.get(7)) }, |
| | | }; |
| | | } |
| | | |
| | | @DataProvider(name = "invalidArguments") |
| | | Object[][] createInValidArguments() throws Exception { |
| | | @DataProvider |
| | | Object[][] invalidArguments() throws Exception { |
| | | return new Object[][] { |
| | | { // check that usage is written to output when arguments are invalid |
| | | args(), |
| | | expectedErrOutput(INFO_MAKELDIF_TOOL_DESCRIPTION.get()) }, |
| | | expectedOutput(INFO_MAKELDIF_TOOL_DESCRIPTION.get()) }, |
| | | |
| | | { // check that there is an argument error when no arg provided |
| | | args(), |
| | | expectedErrOutput(ERR_ERROR_PARSING_ARGS.get("")) }, |
| | | expectedOutput(ERR_ERROR_PARSING_ARGS.get("")) }, |
| | | |
| | | { args("-r", "unknown/path" , "example.template"), |
| | | expectedErrOutput(ERR_LDIF_GEN_TOOL_NO_SUCH_RESOURCE_DIRECTORY.get("unknown/path")) }, |
| | | expectedOutput(ERR_LDIF_GEN_TOOL_NO_SUCH_RESOURCE_DIRECTORY.get("unknown/path")) }, |
| | | |
| | | { args("-o", "unknown/path" , "example.template"), |
| | | expectedErrOutput(ERR_MAKELDIF_UNABLE_TO_CREATE_LDIF.get("unknown/path", "")) }, |
| | | expectedOutput(ERR_MAKELDIF_UNABLE_TO_CREATE_LDIF.get("unknown/path", "")) }, |
| | | |
| | | { args("-s", "non-numeric" , "example.template"), |
| | | expectedErrOutput(ERR_ERROR_PARSING_ARGS.get("")) }, |
| | | expectedOutput(ERR_ERROR_PARSING_ARGS.get("")) }, |
| | | }; |
| | | } |
| | | |
| | | @Test(dataProvider = "validArguments") |
| | | public void testRunValidArguments(String[] arguments, LocalizableMessage expectedErrOutput) |
| | | public void testMakeLDIFValidUseCases(final String[] arguments, final LocalizableMessage expectedOut) |
| | | throws Exception { |
| | | run(arguments, true, expectedErrOutput); |
| | | run(arguments, true, expectedOut); |
| | | } |
| | | |
| | | @Test(dataProvider = "invalidArguments") |
| | | public void testRunInvalidArguments(String[] arguments, LocalizableMessage expectedErrOutput) |
| | | public void testMakeLDIFInvalidUseCases(final String[] arguments, final LocalizableMessage expectedErr) |
| | | throws Exception { |
| | | run(arguments, false, expectedErrOutput); |
| | | run(arguments, false, expectedErr); |
| | | } |
| | | |
| | | private void run(String[] arguments, boolean expectsResults, LocalizableMessage expectedErrOutput) |
| | | throws UnsupportedEncodingException { |
| | | PrintStream outStream = null; |
| | | PrintStream errStream = null; |
| | | try { |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | | outStream = new PrintStream(out); |
| | | ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| | | errStream = new PrintStream(err); |
| | | |
| | | MakeLDIF makeLDIF = new MakeLDIF(outStream, errStream); |
| | | makeLDIF.run(arguments); |
| | | |
| | | assertThat(out.size()).isGreaterThan(0); |
| | | if (!expectsResults) { |
| | | assertThat(err.size()).isEqualTo(0); |
| | | } |
| | | ByteArrayOutputStream std = expectsResults || makeLDIF.isInteractive() ? out : err; |
| | | assertThat(std.toString("UTF-8")).contains(wrapText(expectedErrOutput, MAX_LINE_WIDTH)); |
| | | } finally { |
| | | closeSilently(outStream, errStream); |
| | | private void run(final String[] arguments, final boolean expectsSuccess, final LocalizableMessage expectedOutput) |
| | | throws Exception { |
| | | final MakeLDIF makeLDIF = new MakeLDIF(outStream, errStream); |
| | | int retCode = makeLDIF.run(arguments); |
| | | checkOuputStreams(out, err, expectedOutput, ""); |
| | | if (expectsSuccess) { |
| | | assertThat(retCode).isEqualTo(0); |
| | | } else { |
| | | assertThat(retCode).isNotEqualTo(0); |
| | | } |
| | | } |
| | | |
| | | /** Arguments passed to the command. */ |
| | | private String[] args(String...arguments) { |
| | | return arguments; |
| | | } |
| | | |
| | | /** A message the error output is expected to contain. */ |
| | | private LocalizableMessage expectedErrOutput(LocalizableMessage val) { |
| | | private LocalizableMessage expectedOutput(LocalizableMessage val) { |
| | | return val; |
| | | } |
| | | |
| | | |
| | | } |