mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Violette Roche-Montane
05.02.2014 e14ffab7771d005424baf5d7021ec9a2b3817efc
OPENDJ 1303 Checkpoint commit
- Modified classes for their future use in the opendj3-server-dev
- Migrate tests from server.

2 files added
1 files renamed
3 files modified
470 ■■■■■ changed files
opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentGroup.java 20 ●●●●● patch | view | raw | blame | history
opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java 35 ●●●● patch | view | raw | blame | history
opendj-cli/src/main/resources/com/forgerock/opendj/cli/cli.properties 3 ●●●●● patch | view | raw | blame | history
opendj-cli/src/test/java/com/forgerock/opendj/cli/CliTestCase.java 2 ●●● patch | view | raw | blame | history
opendj-cli/src/test/java/com/forgerock/opendj/cli/TestSubCommandArgumentParserTestCase.java 151 ●●●●● patch | view | raw | blame | history
opendj-cli/src/test/java/com/forgerock/opendj/cli/TestSubCommandTestCase.java 259 ●●●●● patch | view | raw | blame | history
opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentGroup.java
@@ -34,12 +34,10 @@
import org.forgerock.i18n.LocalizableMessage;
/**
 * Class for organizing options into logical groups when argument usage is
 * printed. To use an argument group, create an instance and use
 * {@code ArgumentParser#addArgument(Argument, ArgumentGroup)}
 * when adding arguments for to the parser.
 * Class for organizing options into logical groups when argument usage is printed. To use an argument group, create an
 * instance and use {@code ArgumentParser#addArgument(Argument, ArgumentGroup)} when adding arguments for to the parser.
 */
final class ArgumentGroup implements Comparable<ArgumentGroup> {
public final class ArgumentGroup implements Comparable<ArgumentGroup> {
    // Description for this group of arguments
    private LocalizableMessage description = null;
@@ -54,14 +52,12 @@
     * Creates a parameterized instance.
     *
     * @param description
     *            for options in this group that is printed before argument
     *            descriptions in usage output
     *            for options in this group that is printed before argument descriptions in usage output
     * @param priority
     *            number governing the position of this group within the usage
     *            statement. Groups with higher priority values appear before
     *            groups with lower priority.
     *            number governing the position of this group within the usage statement. Groups with higher priority
     *            values appear before groups with lower priority.
     */
    ArgumentGroup(final LocalizableMessage description, final int priority) {
    public ArgumentGroup(final LocalizableMessage description, final int priority) {
        this.description = description;
        this.priority = priority;
        this.args = new LinkedList<Argument>();
@@ -83,7 +79,7 @@
     *            to add
     * @return boolean where true indicates the add was successful
     */
    boolean addArgument(final Argument arg) {
    public boolean addArgument(final Argument arg) {
        boolean success = false;
        if (arg != null) {
            final Character newShort = arg.getShortIdentifier();
opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java
@@ -165,7 +165,7 @@
     * properties file, no-prompt etc. These will appear toward the bottom of
     * the usage statement.
     */
    private final ArgumentGroup ioArgGroup = new ArgumentGroup(INFO_DESCRIPTION_IO_ARGS.get(),
    protected final ArgumentGroup ioArgGroup = new ArgumentGroup(INFO_DESCRIPTION_IO_ARGS.get(),
            Integer.MIN_VALUE + 1);
    /**
@@ -369,7 +369,7 @@
     *             If the provided argument conflicts with another argument that
     *             has already been defined.
     */
    void addDefaultArgument(final Argument argument) throws ArgumentException {
    protected void addDefaultArgument(final Argument argument) throws ArgumentException {
        addArgument(argument, defaultArgGroup);
    }
@@ -397,7 +397,7 @@
     *             If the provided argument conflicts with another argument that
     *             has already been defined.
     */
    void addInputOutputArgument(final Argument argument) throws ArgumentException {
    public void addInputOutputArgument(final Argument argument) throws ArgumentException {
        addArgument(argument, ioArgGroup);
    }
@@ -522,7 +522,7 @@
     * @return The argument with the specified long identifier, or
     *         <CODE>null</CODE> if there is no such argument.
     */
    Argument getArgumentForLongID(final String longID) {
    public Argument getArgumentForLongID(final String longID) {
        return longIDMap.get(longID);
    }
@@ -545,7 +545,7 @@
     * @return The list of all arguments that have been defined for this
     *         argument parser.
     */
    LinkedList<Argument> getArgumentList() {
    public LinkedList<Argument> getArgumentList() {
        return argumentList;
    }
@@ -671,7 +671,7 @@
     * @return A string containing usage information based on the defined
     *         arguments.
     */
    String getUsage() {
    public String getUsage() {
        final StringBuilder buffer = new StringBuilder();
        getUsage(buffer);
@@ -825,7 +825,7 @@
     * @return <CODE>true</CODE> if the usage argument was provided and
     *         <CODE>false</CODE> otherwise.
     */
    boolean isUsageArgumentPresent() {
    public boolean isUsageArgumentPresent() {
        boolean isUsageArgumentPresent = false;
        if (usageArgument != null) {
            isUsageArgumentPresent = usageArgument.isPresent();
@@ -840,7 +840,7 @@
     * @return <CODE>true</CODE> if the version argument was provided and
     *         <CODE>false</CODE> otherwise.
     */
    boolean isVersionArgumentPresent() {
    public boolean isVersionArgumentPresent() {
        return versionPresent;
    }
@@ -1631,4 +1631,23 @@
            }
        }
    }
    /**
     * Get the password which has to be used for the command without prompting the user. If no password was specified,
     * return null.
     *
     * @param clearArg
     *            The password StringArgument argument.
     * @param fileArg
     *            The password FileBased argument.
     * @return The password stored into the specified file on by the command line argument, or null it if not specified.
     */
    public static String getBindPassword(StringArgument clearArg, FileBasedArgument fileArg) {
        if (clearArg.isPresent()) {
            return clearArg.getValue();
        } else if (fileArg.isPresent()) {
            return fileArg.getValue();
        }
        return null;
    }
}
opendj-cli/src/main/resources/com/forgerock/opendj/cli/cli.properties
@@ -696,4 +696,7 @@
ERR_NO_ROOT_PASSWORD=ERROR:  No password was provided \
 for the initial root user.  When performing a non-interactive installation, \
 this must be provided using either the %s or the %s argument
INFO_BACKUPDB_DESCRIPTION_BACKEND_ID=Backend ID for the backend to \
 archive
INFO_BACKUPDB_DESCRIPTION_BACKUP_ALL=Back up all backends in the server
 
opendj-cli/src/test/java/com/forgerock/opendj/cli/CliTestCase.java
File was renamed from opendj-cli/src/test/java/org/forgerock/opendj/cli/CliTestCase.java
@@ -24,7 +24,7 @@
 *      Copyright 2014 ForgeRock AS.
 */
package org.forgerock.opendj.cli;
package com.forgerock.opendj.cli;
import org.forgerock.testng.ForgeRockTestCase;
import org.testng.annotations.Test;
opendj-cli/src/test/java/com/forgerock/opendj/cli/TestSubCommandArgumentParserTestCase.java
New file
@@ -0,0 +1,151 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2008 Sun Microsystems, Inc.
 *      Portions Copyright 2014 ForgeRock AS
 */
package com.forgerock.opendj.cli;
import java.util.ArrayList;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.forgerock.i18n.LocalizableMessage;
import static com.forgerock.opendj.cli.CliMessages.*;
/**
 * Unit tests for the SubCommand class.
 */
public final class TestSubCommandArgumentParserTestCase extends CliTestCase {
    // The sub-command parser.
    private SubCommandArgumentParser parser;
    // First sub-command.
    private SubCommand sc1;
    // Second sub-command.
    private SubCommand sc2;
    /**
     * Create the sub-commands and parser.
     *
     * @throws Exception
     *             If something unexpected happened.
     */
    @BeforeClass
    public void setup() throws Exception {
        parser = new SubCommandArgumentParser(this.getClass().getName(), LocalizableMessage.raw("test description"),
                true);
        sc1 = new SubCommand(parser, "sub-command1", INFO_BACKUPDB_DESCRIPTION_BACKEND_ID.get());
        sc2 = new SubCommand(parser, "sub-command2", true, 2, 4, "args1 arg2 [arg3 arg4]",
                INFO_BACKUPDB_DESCRIPTION_BACKUP_ALL.get());
    }
    /**
     * Test the getSubCommand method.
     *
     * @throws Exception
     *             If something unexpected happened.
     */
    @Test
    public void testGetSubCommand() throws Exception {
        Assert.assertSame(parser.getSubCommand("sub-command1"), sc1);
        Assert.assertSame(parser.getSubCommand("sub-command2"), sc2);
        Assert.assertNull(parser.getSubCommand("sub-command3"));
    }
    /**
     * Provide valid command line args.
     *
     * @return Array of valid command line args.
     */
    @DataProvider(name = "validCommandLineArgs")
    public Object[][] createValidCommandLineArgs() {
        return new Object[][] { { new String[] {}, null }, { new String[] { "sub-command1" }, sc1 },
            { new String[] { "sub-command2", "one", "two" }, sc2 },
            { new String[] { "sub-command2", "one", "two", "three" }, sc2 },
            { new String[] { "sub-command2", "one", "two", "three", "four" }, sc2 }, };
    }
    /**
     * Test the parseArguments method with valid args.
     *
     * @param args
     *            The command line args.
     * @param sc
     *            The expected sub-command.
     * @throws Exception
     *             If something unexpected happened.
     */
    @Test(dataProvider = "validCommandLineArgs")
    public void testParseArgumentsWithValidArgs(String[] args, SubCommand sc) throws Exception {
        parser.parseArguments(args);
        // Check the correct sub-command was parsed.
        Assert.assertEquals(parser.getSubCommand(), sc);
        // Check that the trailing arguments were retrieved correctly and
        // in the right order.
        if (args.length > 1) {
            List<String> scargs = new ArrayList<String>();
            for (int i = 1; i < args.length; i++) {
                scargs.add(args[i]);
            }
            Assert.assertEquals(parser.getTrailingArguments(), scargs);
        } else {
            Assert.assertTrue(parser.getTrailingArguments().isEmpty());
        }
    }
    /**
     * Provide invalid command line args.
     *
     * @return Array of invalid command line args.
     */
    @DataProvider(name = "invalidCommandLineArgs")
    public Object[][] createInvalidCommandLineArgs() {
        return new Object[][] { { new String[] { "sub-command1", "one" } },
            { new String[] { "sub-command1", "one", "two" } }, { new String[] { "sub-command2" } },
            { new String[] { "sub-command2", "one" } },
            { new String[] { "sub-command2", "one", "two", "three", "four", "five" } }, };
    }
    /**
     * Test the parseArguments method with invalid args.
     *
     * @param args
     *            The command line args.
     * @throws Exception
     *             If something unexpected happened.
     */
    @Test(dataProvider = "invalidCommandLineArgs", expectedExceptions = ArgumentException.class)
    public void testParseArgumentsWithInvalidArgs(String[] args) throws Exception {
        parser.parseArguments(args);
    }
}
opendj-cli/src/test/java/com/forgerock/opendj/cli/TestSubCommandTestCase.java
New file
@@ -0,0 +1,259 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2008 Sun Microsystems, Inc.
 *      Portions Copyright 2014 ForgeRock AS
 */
package com.forgerock.opendj.cli;
import java.util.Arrays;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.forgerock.i18n.LocalizableMessage;
/**
 * Unit tests for the SubCommand class.
 */
public final class TestSubCommandTestCase extends CliTestCase {
    /**
     * Tests that allowsTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testAllowsTrailingArgumentsFalse1() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command1", LocalizableMessage.raw("XXX"));
        Assert.assertFalse(sc.allowsTrailingArguments());
    }
    /**
     * Tests that allowsTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testAllowsTrailingArgumentsFalse2() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", false, 0, 0, null, LocalizableMessage.raw("XXX"));
        Assert.assertFalse(sc.allowsTrailingArguments());
    }
    /**
     * Tests that allowsTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testAllowsTrailingArgumentsTrue() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", true, 2, 4, "args1 arg2 [arg3 arg4]",
                LocalizableMessage.raw("XXX"));
        Assert.assertTrue(sc.allowsTrailingArguments());
    }
    /**
     * Tests that getMaxTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetMaxTrailingArguments1() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command1", LocalizableMessage.raw("XXX"));
        Assert.assertEquals(sc.getMaxTrailingArguments(), 0);
    }
    /**
     * Tests that getMaxTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetMaxTrailingArguments2() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", false, 0, 0, null, LocalizableMessage.raw("XXX"));
        Assert.assertEquals(sc.getMaxTrailingArguments(), 0);
    }
    /**
     * Tests that getMaxTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetMaxTrailingArguments3() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", true, 2, 4, "args1 arg2 [arg3 arg4]",
                LocalizableMessage.raw("XXX"));
        Assert.assertEquals(sc.getMaxTrailingArguments(), 4);
    }
    /**
     * Tests that getMinTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetMinTrailingArguments1() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command1", LocalizableMessage.raw("XXX"));
        Assert.assertEquals(sc.getMinTrailingArguments(), 0);
    }
    /**
     * Tests that getMinTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetMinTrailingArguments2() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", false, 0, 0, null, LocalizableMessage.raw("XXX"));
        Assert.assertEquals(sc.getMinTrailingArguments(), 0);
    }
    /**
     * Tests that getMinTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetMinTrailingArguments3() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", true, 2, 4, "args1 arg2 [arg3 arg4]",
                LocalizableMessage.raw("XXX"));
        Assert.assertEquals(sc.getMinTrailingArguments(), 2);
    }
    /**
     * Tests that getTrailingArgumentsDisplayName works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetTrailingArgumentsDisplayName1() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command1", LocalizableMessage.raw("XXX"));
        Assert.assertNull(sc.getTrailingArgumentsDisplayName());
    }
    /**
     * Tests that getTrailingArgumentsDisplayName works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetTrailingArgumentsDisplayName2() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", false, 0, 0, null, LocalizableMessage.raw("XXX"));
        Assert.assertNull(sc.getTrailingArgumentsDisplayName());
    }
    /**
     * Tests that getTrailingArgumentsDisplayName works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetTrailingArgumentsDisplayName3() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", true, 2, 4, "args1 arg2 [arg3 arg4]",
                LocalizableMessage.raw("XXX"));
        Assert.assertEquals(sc.getTrailingArgumentsDisplayName(), "args1 arg2 [arg3 arg4]");
    }
    /**
     * Tests that getTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetTrailingArguments1() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command1", LocalizableMessage.raw("XXX"));
        parser.parseArguments(new String[] { "sub-command1" });
        Assert.assertTrue(sc.getTrailingArguments().isEmpty());
    }
    /**
     * Tests that getTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetTrailingArguments2() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", false, 0, 0, null, LocalizableMessage.raw("XXX"));
        parser.parseArguments(new String[] { "sub-command2" });
        Assert.assertTrue(sc.getTrailingArguments().isEmpty());
    }
    /**
     * Tests that getTrailingArguments works correctly.
     *
     * @throws Exception
     *             If an unexpected problem occurred.
     */
    @Test
    public void testGetTrailingArguments3() throws Exception {
        SubCommandArgumentParser parser = new SubCommandArgumentParser(this.getClass().getName(),
                LocalizableMessage.raw("test description"), true);
        SubCommand sc = new SubCommand(parser, "sub-command2", true, 2, 4, "args1 arg2 [arg3 arg4]",
                LocalizableMessage.raw("XXX"));
        parser.parseArguments(new String[] { "sub-command2", "arg1", "arg2", "arg3" });
        Assert.assertEquals(sc.getTrailingArguments(), Arrays.asList(new String[] { "arg1", "arg2", "arg3" }));
    }
}