--- /dev/null
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * BonYong Lee <bonyong.lee@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.core.command.prompter;
+
+import static org.tizen.common.util.StringUtil.isEmpty;
+import static org.tizen.common.util.StringUtil.trim;
+
+import org.tizen.common.util.ObjectUtil;
+
+/**
+ * <p>
+ * AbstractOption.
+ *
+ * Abstract(common) class for option
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+public class
+AbstractOption
+implements Option
+{
+ /**
+ * <p>
+ * Option name
+ *
+ * It is printed out
+ * </p>
+ */
+ protected final String name;
+
+ /**
+ * Flag if user input is handled as abbreviation
+ */
+ protected final boolean bPermitAbbreviation;
+
+ /**
+ * Flag if this option is default( choice when user doesn't select option )
+ */
+ protected final boolean bDefault;
+
+ /**
+ * Constructor with choice name, default flag and abbreviation flag
+ *
+ * @param name choice name
+ * @param bDefault default flag
+ * @param bPermitAbbreviation abbreviation flag
+ */
+ public
+ AbstractOption(
+ final String name,
+ final boolean bDefault,
+ final boolean bPermitAbbreviation
+ )
+ {
+ this.name = trim( name );
+
+ this.bDefault = bDefault;
+
+ this.bPermitAbbreviation = bPermitAbbreviation;
+ }
+
+ /**
+ * Return option name
+ *
+ * @return option name
+ */
+ public String getName()
+ {
+ return this.name;
+ }
+
+ /**
+ * Return whether this choice is default {@link ChoiceOption}
+ *
+ * @return boolean value if this choice is default
+ */
+ public boolean isDefault()
+ {
+ return this.bDefault;
+ }
+
+ /**
+ * Return whether user input, <code>value</code> is match option
+ *
+ * @param value user input
+ *
+ * @return boolean value If matching
+ */
+ public
+ boolean
+ isMatch(
+ final String value
+ )
+ {
+ if ( name.equalsIgnoreCase( trim( value ) ) )
+ {
+ return true;
+ }
+
+ if ( isEmpty( value ) )
+ {
+ return false;
+ }
+ if ( bPermitAbbreviation )
+ {
+ return name.substring( 0, 1 ).equalsIgnoreCase( value.substring( 0, 1 ) );
+ }
+
+ return false;
+
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public
+ int
+ hashCode()
+ {
+ return name.hashCode();
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public
+ boolean
+ equals(
+ final Object obj
+ )
+ {
+ if ( !( obj instanceof ChoiceOption ) )
+ {
+ return false;
+ }
+ final ChoiceOption other = (ChoiceOption) obj;
+ return ObjectUtil.equals( this.name, other.name );
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public
+ String
+ toString()
+ {
+ if ( isDefault() )
+ {
+ return "[" + name + "]";
+ }
+ return name;
+ }
+
+
+}
*
* @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
*/
-public class AbstractPrompter
+public class
+AbstractPrompter
{
/**
protected
void
- checkOptions( Option[] options )
+ checkOptions( ChoiceOption[] options )
{
if ( null == options )
{
--- /dev/null
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * BonYong Lee <bonyong.lee@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.core.command.prompter;
+
+import static org.tizen.common.util.StringUtil.hasText;
+
+import org.tizen.common.util.Assert;
+
+/**
+ * <p>
+ * Option.
+ *
+ * Option to choice
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+public class
+ChoiceOption
+extends AbstractOption
+{
+ /**
+ * Constructor with choice name
+ *
+ * @param name choice name
+ *
+ * #see {@link #Option(String, boolean)}
+ */
+ public
+ ChoiceOption(
+ final String name
+ )
+ {
+ this( name, false );
+ }
+
+ /**
+ * Constructor with choice name and default flag
+ *
+ * @param name choice name
+ * @param bDefault default flag
+ *
+ * @see #Option(String, boolean, boolean)
+ */
+ public
+ ChoiceOption(
+ final String name,
+ final boolean bDefault
+ )
+ {
+ this( name, bDefault, true );
+ }
+
+ /**
+ * Constructor with choice name, default flag and abbreviation flag
+ *
+ * @param name choice name
+ * @param bDefault default flag
+ * @param bPermitAbbreviation abbreviation flag
+ */
+ public
+ ChoiceOption(
+ final String name,
+ final boolean bDefault,
+ final boolean bPermitAbbreviation
+ )
+ {
+ super( name, bDefault, bPermitAbbreviation );
+ Assert.isTrue( hasText( name ) );
+ }
+
+
+}
--- /dev/null
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * BonYong Lee <bonyong.lee@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.core.command.prompter;
+
+/**
+ * <p>
+ * GenericOption.
+ *
+ * {@link ChoiceOption} for general input
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+public class
+GenericOption
+extends AbstractOption
+{
+
+ /**
+ * User input
+ */
+ protected String answer;
+
+ /**
+ * Default constructor
+ */
+ public
+ GenericOption()
+ {
+ super( "", false, false );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.core.command.prompter.AbstractOption#isMatch(java.lang.String)
+ */
+ @Override
+ public
+ boolean
+ isMatch(
+ final String value
+ )
+ {
+ this.answer = value;
+ return true;
+ }
+
+ /**
+ * Return user input
+ *
+ * @return user input
+ */
+ public String
+ getAnswer()
+ {
+ return this.answer;
+ }
+
+ @Override
+ public String toString() {
+ return this.answer;
+ }
+}
-/*
- * Common
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact:
- * BonYong Lee <bonyong.lee@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * Contributors:
- * - S-Core Co., Ltd
- *
- */
package org.tizen.common.core.command.prompter;
-import static org.tizen.common.util.StringUtil.hasText;
-import static org.tizen.common.util.StringUtil.isEmpty;
-import static org.tizen.common.util.StringUtil.trim;
+public interface Option
+{
+ String getName();
-import org.tizen.common.util.Assert;
-import org.tizen.common.util.ObjectUtil;
+ boolean isDefault();
-/**
- * <p>
- * Option.
- *
- * Option to choice
- * </p>
- *
- * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
- */
-public class
-Option
-{
- /**
- * <p>
- * Option name
- *
- * It is printed out
- * </p>
- */
- protected final String name;
-
- /**
- * Flag if user input is handled as abbreviation
- */
- protected final boolean bPermitAbbreviation;
-
- /**
- * Flag if this option is default( choice when user doesn't select option )
- */
- protected final boolean bDefault;
-
- /**
- * Constructor with choice name
- *
- * @param name choice name
- *
- * #see {@link #Option(String, boolean)}
- */
- public
- Option(
- final String name
- )
- {
- this( name, false );
- }
-
- /**
- * Constructor with choice name and default flag
- *
- * @param name choice name
- * @param bDefault default flag
- *
- * @see #Option(String, boolean, boolean)
- */
- public
- Option(
- final String name,
- final boolean bDefault
- )
- {
- this( name, bDefault, true );
- }
-
- /**
- * Constructor with choice name, default flag and abbreviation flag
- *
- * @param name choice name
- * @param bDefault default flag
- * @param bPermitAbbreviation abbreviation flag
- */
- public
- Option(
- final String name,
- final boolean bDefault,
- final boolean bPermitAbbreviation
- )
- {
- Assert.isTrue( hasText( name ) );
- this.name = trim( name );
-
- this.bDefault = bDefault;
-
- this.bPermitAbbreviation = bPermitAbbreviation;
- }
-
- /**
- * Return option name
- *
- * @return option name
- */
- public String getName()
- {
- return this.name;
- }
-
- /**
- * Return whether this choice is default {@link Option}
- *
- * @return boolean value if this choice is default
- */
- public boolean isDefault()
- {
- return this.bDefault;
- }
-
- /**
- * Return whether user input, <code>value</code> is match option
- *
- * @param value user input
- *
- * @return boolean value If matching
- */
- public
- boolean
- isMatch(
- final String value
- )
- {
- if ( name.equalsIgnoreCase( trim( value ) ) )
- {
- return true;
- }
-
- if ( isEmpty( value ) )
- {
- return false;
- }
- if ( bPermitAbbreviation )
- {
- return name.substring( 0, 1 ).equalsIgnoreCase( value.substring( 0, 1 ) );
- }
-
- return false;
-
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#hashCode()
- */
- @Override
- public
- int
- hashCode()
- {
- return name.hashCode();
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#equals(java.lang.Object)
- */
- @Override
- public
- boolean
- equals(
- final Object obj
- )
- {
- if ( !( obj instanceof Option ) )
- {
- return false;
- }
- final Option other = (Option) obj;
- return ObjectUtil.equals( this.name, other.name );
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public
- String
- toString()
- {
- if ( isDefault() )
- {
- return "[" + name + "]";
- }
- return name;
- }
+ boolean isMatch( final String value );
}
}
/**
+ * Create prompter for test
+ *
+ * @return {@link Prompter}
+ *
+ * @see NopPrompter
+ */
+ protected
+ Prompter
+ createPrompter()
+ {
+ return new NopPrompter();
+ }
+
+ /**
* Set up test
*/
@Before
public void setUp()
{
- context = new ExecutionContext( new PolicyRegistry(), new NopPrompter(), new VirtualFileHandler() );
+ context = new ExecutionContext( new PolicyRegistry(), createPrompter(), new VirtualFileHandler() );
}
/**
--- /dev/null
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * BonYong Lee <bonyong.lee@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.core.command.prompter;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+
+/**
+ * <p>
+ * GenericOptionTest.
+ *
+ * Test case for {@link GenericOption}
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ *
+ * @see GenericOption
+ */
+public class
+GenericOptionTest
+{
+ /**
+ * Test {@link GenericOption#isMatch(String)}
+ *
+ * @throws Exception in case of failure in test
+ *
+ * @see GenericOption#isMatch(String)
+ */
+ @Test
+ public
+ void
+ test_isMatch()
+ throws Exception
+ {
+
+ final GenericOption option = new GenericOption();
+
+ assertTrue( option.isMatch( "Hello" ) );
+ assertTrue( option.isMatch( "World" ) );
+
+
+ }
+
+ /**
+ * Test {@link GenericOption#getAnswer()}
+ *
+ * @throws Exception in case of failure in test
+ *
+ * @see GenericOption#getAnswer()
+ */
+ @Test
+ public
+ void
+ test_getAnswer()
+ throws Exception
+ {
+ final String userAnser = "Dummy answer";
+ NopPrompter prompter = new NopPrompter() {
+ @Override
+ public Option interact(String question, Option... options) {
+ options[0].isMatch( userAnser );
+ return options[0];
+ }
+ };
+ GenericOption option = new GenericOption();
+ final Option answer = prompter.interact( "Answer ?", option );
+
+ assertEquals( userAnser, ((GenericOption) answer).getAnswer() );
+ assertEquals( userAnser, answer.toString() );
+
+ }
+
+}
/**
* OptionTest
*
- * Test case for {@link Option}
+ * Test case for {@link ChoiceOption}
*
* @author bylee
*
- * @see Option
+ * @see ChoiceOption
*/
public class
OptionTest
{
/**
- * Test {@link Option#isMatch(String)}
+ * Test {@link ChoiceOption#isMatch(String)}
*
* @throws Exception in case of failure in test
*
- * @see Option#isMatch(String)
+ * @see ChoiceOption#isMatch(String)
*/
@Test
public
// Test cases
final Object[][] TEST_CASES = new Object[][] {
new Object[] {
- new Option( "test" ),
+ new ChoiceOption( "test" ),
new Object[][] {
new Object[] { "t", true },
new Object[] { "TEST", true },
for ( final Object[] TEST_CASE : TEST_CASES )
{
- final Option option = (Option) TEST_CASE[0];
+ final ChoiceOption option = (ChoiceOption) TEST_CASE[0];
final Object[][] TEST_CASE2 = (Object[][]) TEST_CASE[1];
for ( final Object[] TEST_CASE3 : TEST_CASE2 )
{
SWTPrompterTest
{
/**
- * Test {@link SWTPrompter#interact(String, Option...)}
+ * Test {@link SWTPrompter#interact(String, ChoiceOption...)}
*
* @throws Exception in case of failure in test
*
- * @see SWTPrompter#interact(String, Option...)
+ * @see SWTPrompter#interact(String, ChoiceOption...)
*/
@Test
public void test_interact() throws Exception {
};
// When
- Option opt1 = new Option( "Yes" );
- Option opt2 = new Option( "No", true );
+ ChoiceOption opt1 = new ChoiceOption( "Yes" );
+ ChoiceOption opt2 = new ChoiceOption( "No", true );
// Then
Option result = prompter.interact( "Yes or No", opt1, opt2 );
};
// When
- Option opt1 = new Option( "Yes" );
- Option opt2 = new Option( "No", true );
+ ChoiceOption opt1 = new ChoiceOption( "Yes" );
+ ChoiceOption opt2 = new ChoiceOption( "No", true );
// Then
Option result = prompter.interact( "Yes or No", opt1, opt2 );