[Title] Add test case for QueryCommand & Refactoring Option class
authorBonyong.lee <bonyong.lee@samsung.com>
Wed, 18 Jul 2012 02:08:57 +0000 (03:08 +0100)
committerBonyong.lee <bonyong.lee@samsung.com>
Wed, 18 Jul 2012 02:08:57 +0000 (03:08 +0100)
[Type]      Enhancement
[Module]    Sub
[Priority]  Minor
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]
[TestCase]

org.tizen.common/src/org/tizen/common/core/command/prompter/AbstractOption.java [new file with mode: 0644]
org.tizen.common/src/org/tizen/common/core/command/prompter/AbstractPrompter.java
org.tizen.common/src/org/tizen/common/core/command/prompter/ChoiceOption.java [new file with mode: 0644]
org.tizen.common/src/org/tizen/common/core/command/prompter/GenericOption.java [new file with mode: 0644]
org.tizen.common/src/org/tizen/common/core/command/prompter/Option.java
org.tizen.common/test/src/org/tizen/common/core/command/CommandTest.java
org.tizen.common/test/src/org/tizen/common/core/command/prompter/GenericOptionTest.java [new file with mode: 0644]
org.tizen.common/test/src/org/tizen/common/core/command/prompter/OptionTest.java
org.tizen.common/test/src/org/tizen/common/core/command/prompter/SWTPrompterTest.java

diff --git a/org.tizen.common/src/org/tizen/common/core/command/prompter/AbstractOption.java b/org.tizen.common/src/org/tizen/common/core/command/prompter/AbstractOption.java
new file mode 100644 (file)
index 0000000..d60b177
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * 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;
+       }
+
+
+}
index e641c71..701a977 100644 (file)
@@ -39,7 +39,8 @@ import org.tizen.common.core.command.Prompter;
  * 
  * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
  */
-public class AbstractPrompter
+public class
+AbstractPrompter
 {
        
        /**
@@ -49,7 +50,7 @@ public class AbstractPrompter
 
        protected
        void
-       checkOptions( Option[] options )
+       checkOptions( ChoiceOption[] options )
        {
                if ( null == options )
                {
diff --git a/org.tizen.common/src/org/tizen/common/core/command/prompter/ChoiceOption.java b/org.tizen.common/src/org/tizen/common/core/command/prompter/ChoiceOption.java
new file mode 100644 (file)
index 0000000..267941b
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * 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 ) );
+       }
+       
+
+}
diff --git a/org.tizen.common/src/org/tizen/common/core/command/prompter/GenericOption.java b/org.tizen.common/src/org/tizen/common/core/command/prompter/GenericOption.java
new file mode 100644 (file)
index 0000000..caa86c2
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * 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;
+       }
+}
index a5e2ed8..c2c624c 100644 (file)
-/*
- * 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 );
 
 }
index b61d196..4e613e7 100644 (file)
@@ -110,12 +110,26 @@ CommandTest
        }
        
        /**
+        * 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() );
        }
        
        /**
diff --git a/org.tizen.common/test/src/org/tizen/common/core/command/prompter/GenericOptionTest.java b/org.tizen.common/test/src/org/tizen/common/core/command/prompter/GenericOptionTest.java
new file mode 100644 (file)
index 0000000..4094c3e
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * 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() );
+               
+       }
+
+}
index 0fb2e18..beb2027 100644 (file)
@@ -32,21 +32,21 @@ import org.junit.Test;
 /**
  * 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
@@ -57,7 +57,7 @@ OptionTest
                // 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 },
@@ -72,7 +72,7 @@ OptionTest
                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 )
                        {
index 348b7a3..f222f83 100644 (file)
@@ -46,11 +46,11 @@ public class
 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 {
@@ -71,8 +71,8 @@ SWTPrompterTest
                        };
                        
                        // 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 );
@@ -96,8 +96,8 @@ SWTPrompterTest
                        };
                        
                        // 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 );