Package org.rhq.enterprise.communications.command.param

Examples of org.rhq.enterprise.communications.command.param.ParameterDefinition


     *
     * @throws Exception
     */
    public void testDynamicDefinition() throws Exception {
        GenericCommand gc = new GenericCommand(new CommandType("foo", 123), new ParameterDefinition[] {
            new ParameterDefinition("string", "java.lang.String", true, false, false, ""),
            new ParameterDefinition("int", "java.lang.Integer", true, true, false, "") });

        assertEquals(new CommandType("foo", 123), gc.getCommandType());
        assertEquals(2, gc.getParameterDefinitions().length);
        try {
            gc.checkParameterValidity(true);
View Full Code Here


        } catch (InvalidParameterValueException e) {
        }

        // restrict our command to only a certain set of parameters
        m_cmd.setParameterDefinitions(new ParameterDefinition[] {
            new ParameterDefinition("string", "java.lang.String", true, false, false, ""),
            new ParameterDefinition("int", "java.lang.Integer", true, true, false, "") });
        m_cmd.checkParameterValidity(false);

        // change nullable field
        m_cmd.setParameterDefinitions(new ParameterDefinition[] {
            new ParameterDefinition("string", "java.lang.String", true, false, false, ""),
            new ParameterDefinition("int", "java.lang.Integer", true, false, false, "") });
        try {
            m_cmd.checkParameterValidity(false);
            fail("Should not have allowed a null int param - InvalidParameterValueException should have been thrown");
        } catch (InvalidParameterValueException e) {
        }

        // add a new required field
        m_cmd.setParameterDefinitions(new ParameterDefinition[] {
            new ParameterDefinition("another", "java.lang.String", true, false, false, ""),
            new ParameterDefinition("string", "java.lang.String", true, false, false, ""),
            new ParameterDefinition("int", "java.lang.Integer", true, true, false, "") });
        try {
            m_cmd.checkParameterValidity(false);
            fail("Should have required 'another' param - InvalidParameterValueException should have been thrown");
        } catch (InvalidParameterValueException e) {
        }

        // change that new required field to optional
        m_cmd.setParameterDefinitions(new ParameterDefinition[] {
            new ParameterDefinition("another", "java.lang.String", false, false, false, ""),
            new ParameterDefinition("string", "java.lang.String", true, false, false, ""),
            new ParameterDefinition("int", "java.lang.Integer", true, true, false, "") });
        m_cmd.checkParameterValidity(false);

        // change back to allowing all params
        m_cmd.setParameterDefinitions(null);
        assertTrue(m_cmd.allowAnyParameter());
View Full Code Here

     *
     * @throws Exception
     */
    public void testSerializeCommands() throws Exception {
        GenericCommand gc = new GenericCommand();
        ParameterDefinition def = new ParameterDefinition("hello", String.class.getName(),
            new ParameterRenderingInformation("a", "b"));

        gc.setCommandType(new CommandType("foo", 3));
        gc.setParameterDefinitions(new ParameterDefinition[] { def });
        gc.setParameterValue("hello", "world");
View Full Code Here

     *
     * @throws Exception
     */
    public void testSerializeCommandsWithNoConfig() throws Exception {
        GenericCommand gc = new GenericCommand();
        ParameterDefinition def = new ParameterDefinition("hello", String.class.getName(),
            new ParameterRenderingInformation("a", "b"));

        gc.setCommandType(new CommandType("foo", 3));
        gc.setParameterDefinitions(new ParameterDefinition[] { def });
        gc.setParameterValue("hello", "world");
View Full Code Here

            existingParamNames = new HashSet();
        }

        // make sure the current set of parameters have all required parameters defined with the proper types
        for (Iterator iter = new RequiredParameterDefinitionIterator(m_parameterDefinitions.values()); iter.hasNext();) {
            ParameterDefinition paramDef = (ParameterDefinition) iter.next();
            if (!existingParamNames.contains(paramDef.getName())) {
                throw new InvalidParameterValueException(LOG.getMsgString(CommI18NResourceKeys.MISSING_REQUIRED_FIELD,
                    paramDef.getName(), this));
            }

            paramValue = getParameterValue(paramDef.getName());
            if (!paramDef.isValidValue(paramValue)) {
                boolean valid = false;

                if (convertIfNecessary) {
                    paramValue = paramDef.convertObject(paramValue);

                    // if we got this far, our invalid value was converted to a valid value
                    valid = true;

                    // overwrite the invalid value with the new valid one
                    m_commandParameters.put(paramDef.getName(), paramValue);
                }

                if (!valid) {
                    throw new InvalidParameterValueException(LOG.getMsgString(
                        CommI18NResourceKeys.BAD_REQUIRED_PARAM_TYPE, paramDef.getName(), paramDef.getType(), paramDef
                            .isNullable(), ((paramValue == null) ? "<null>" : paramValue.getClass().toString()), this));
                }
            }
        }

        // if the current set of parameters has optional parameters defined,
        // make sure their values are of the proper type
        for (Iterator iter = new OptionalParameterDefinitionIterator(m_parameterDefinitions.values()); iter.hasNext();) {
            ParameterDefinition paramDef = (ParameterDefinition) iter.next();
            if (existingParamNames.contains(paramDef.getName())) {
                paramValue = getParameterValue(paramDef.getName());
                if (!paramDef.isValidValue(paramValue)) {
                    boolean valid = false;
                    if (convertIfNecessary) {
                        paramValue = paramDef.convertObject(paramValue);

                        // if we got this far, our invalid value was converted to a valid value
                        valid = true;

                        // overwrite the invalid value with the new valid one
                        m_commandParameters.put(paramDef.getName(), paramValue);
                    }

                    if (!valid) {
                        throw new InvalidParameterValueException(LOG.getMsgString(
                            CommI18NResourceKeys.BAD_OPTIONAL_PARAM_TYPE, paramDef.getName(), paramDef.getType(),
                            paramDef.isNullable(),
                            ((paramValue == null) ? "<null>" : paramValue.getClass().toString()), this));
                    }
                }
            }
        }
View Full Code Here

            Map.Entry entry = (Map.Entry) iter.next();
            String paramName = (String) entry.getKey();
            Object paramValue = entry.getValue();

            try {
                ParameterDefinition def = getParameterDefinition(paramName);

                // if there is no definition defined, any value goes - don't bother trying to convert if no definition is available
                if (def != null) {
                    Object convertedParamValue = def.convertObject(paramValue);

                    // overwrite the old value with the new one of the proper, converted type
                    // the references will be identical if no conversion was performed (i.e. the original value was of the proper type)
                    if (convertedParamValue != paramValue) {
                        convertedParameters.put(paramName, convertedParamValue);
View Full Code Here

        if (parameterDefinitions != null) {
            // for easier retrieval, store each definition in a Map keyed on the parameter name
            m_parameterDefinitions = new HashMap<String, ParameterDefinition>();

            for (int i = 0; i < parameterDefinitions.length; i++) {
                ParameterDefinition paramDef = parameterDefinitions[i];
                String paramName = paramDef.getName();

                if (null != m_parameterDefinitions.put(paramName, paramDef)) {
                    // this is bad - it probably means the command is coded wrong and will never be able to be used
                    throw new InvalidParameterDefinitionException(LOG.getMsgString(CommI18NResourceKeys.DUP_DEFS,
                        paramName));
View Full Code Here

     * @see AbstractCommand#buildParameterDefinitions()
     */
    protected ParameterDefinition[] buildParameterDefinitions() {
        // this test command accepts some parameters
        return new ParameterDefinition[] {
            new ParameterDefinition("string", "java.lang.String", true, false, false, ""),
            new ParameterDefinition("int", "java.lang.Integer", false, true, false, ""),
            new ParameterDefinition("long", "java.lang.Long", false, false, false, ""),
            new ParameterDefinition("object", "java.lang.Object", false, true, false, "") };
    }
View Full Code Here

            fail("Should have thrown NoParameterDefinitionsException");
        } catch (NoParameterDefinitionsException npde) {
        }

        try {
            ParameterDefinition def = m_someParamTest.getParameterDefinition("notvalid");
            assertNull(def);

            def = m_someParamTest.getParameterDefinition("int");
            assertEquals("int", def.getName());
            assertEquals(Integer.class.getName(), def.getType());
            assertFalse(def.isRequired());
            assertTrue(def.isNullable());
            assertTrue(def.isValidValue(new Integer(Integer.MAX_VALUE)));
            assertFalse(def.isValidValue("0"));

            def = m_someParamTest.getParameterDefinition("string");
            assertEquals("string", def.getName());
            assertEquals(String.class.getName(), def.getType());
            assertTrue(def.isRequired());
            assertFalse(def.isNullable());
            assertTrue(def.isValidValue("my string"));
            assertFalse(def.isValidValue(new Integer(0)));
        } catch (Exception e) {
            fail("Should not have thrown exception: " + e);
        }
    }
View Full Code Here

TOP

Related Classes of org.rhq.enterprise.communications.command.param.ParameterDefinition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.