[Title] Share RI for Prompter#batch(Collection)
authorBon-Yong Lee <bonyong.lee@samsung.com>
Tue, 13 Nov 2012 08:45:56 +0000 (17:45 +0900)
committerBon-Yong Lee <bonyong.lee@samsung.com>
Tue, 13 Nov 2012 08:45:56 +0000 (17:45 +0900)
[Desc.] Share first implement for collaboration
[Issue] #7420

org.tizen.cli/src/org/tizen/cli/exec/ConsolePrompter.java
org.tizen.cli/test/src/org/tizen/cli/exec/ConsolePrompterTest.java [new file with mode: 0755]

index 41a1b75..113659d 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Web IDE - Command Line Interface
+ * CLI - Command Line Interface
  *
  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
  *
@@ -24,6 +24,7 @@
  */
 package org.tizen.cli.exec;
 
+import static org.tizen.common.util.CollectionUtil.isEmpty;
 import static org.tizen.common.util.StringUtil.isEmpty;
 import static org.tizen.common.util.StringUtil.trim;
 
@@ -34,6 +35,7 @@ import java.io.PrintStream;
 import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.Map;
@@ -41,6 +43,8 @@ import java.util.Map;
 import org.tizen.common.core.command.Prompter;
 import org.tizen.common.core.command.UserField;
 import org.tizen.common.core.command.prompter.AbstractPrompter;
+import org.tizen.common.core.command.prompter.ChoiceOption;
+import org.tizen.common.core.command.prompter.GenericOption;
 import org.tizen.common.core.command.prompter.Option;
 import org.tizen.common.util.ArrayUtil;
 import org.tizen.common.util.Assert;
@@ -221,6 +225,9 @@ implements Prompter
                out.println( message );
        }
 
+    /* (non-Javadoc)
+     * @see org.tizen.common.core.command.Prompter#batch(java.util.Collection)
+     */
     @Override
     public
     Map<String, Object>
@@ -228,7 +235,128 @@ implements Prompter
         final Collection<UserField> userFields
     )
     {
-        return null;
+        final HashMap<String, Object> ret = new HashMap<String, Object>();
+        batch( 0, userFields, ret );
+        return ret;
+    }
+    
+    /**
+     * <p>
+     * Process <code>fields</code> for <code>depth</code> level
+     * 
+     * process result are stored in <code>answer</code>
+     * </p>
+     * @param depth process depth
+     * @param fields user input specifications
+     * @param answer user answer
+     */
+    protected
+    void
+    batch(
+        final int depth,
+        final Collection<UserField> fields,
+        final Map<String, Object> answer
+    )
+    {
+        for ( final UserField child : fields )
+        {
+            batch( depth, child, answer );
+        }
+    }
+    
+    /**
+     * <p>
+     * Process <code>field</code> for <code>depth</code> level
+     * 
+     * process result are stored in <code>answer</code>
+     * </p>
+     * @param depth process depth
+     * @param field user input specification
+     * @param answer user answer
+     */
+    protected
+    void
+    batch(
+        final int depth,
+        final UserField field,
+        final Map<String, Object> answer
+    )
+    {
+        final String id = field.getId();
+        final String msg = field.getMessage();
+        
+        final Collection<Object> supports = field.getSupports();
+        if ( null != supports && !supports.contains( "console" ) )
+        {
+            logger.warn( "{} is not supported in {}", field, this );
+            return ;
+        }
+        
+        final Class<?> type = field.getType();
+        
+        boolean bChild = true;
+        
+        if ( field.canModify() )
+        {
+            if ( String.class.equals( type ) )
+            {
+                final GenericOption option = new GenericOption();
+                interact( indent( depth, msg ), option );
+                if ( field.getValue() != null && isEmpty( option.getAnswer() ) )
+                {
+                    answer.put( id, field.getValue() );
+                }
+                else
+                {
+                    answer.put( id, option.getAnswer() );
+                }
+            }
+            else if ( char[].class.equals( type ) || Character[].class.equals( type ) )
+            {
+                answer.put( id, password( indent( depth, msg ) ) );
+            }
+            else if ( boolean.class.equals( type ) || Boolean.class.equals( type ) )
+            {
+                final boolean initValue = (null == field.getValue())?true:((Boolean) field.getValue());
+                final ChoiceOption yes = new ChoiceOption( "Yes", initValue );
+                final ChoiceOption no = new ChoiceOption( "No", !initValue );
+                final Object opt = interact( indent( depth, msg ), yes, no );
+                bChild = yes.equals( opt );
+                answer.put( id, bChild );
+            }
+        }
+        
+        final Collection<UserField> children = field.getChildren();
+        if ( bChild && !isEmpty( children ) )
+        {
+            notify( indent( depth, msg ) );
+            batch( depth + 1, children, answer );
+        }
+    }
+
+    /**
+     * Make indented message
+     * 
+     * @param depth message depth
+     * @param msg message
+     * 
+     * @return created message
+     */
+    protected
+    String
+    indent(
+        final int depth,
+        final String msg
+    )
+    {
+        final StringBuilder buffer = new StringBuilder();
+        
+        for ( int i = 0 ; i<depth ; ++i )
+        {
+            buffer.append( " " );
+        }
+        buffer.append( msg );
+        return buffer.toString();
     }
 
 }
diff --git a/org.tizen.cli/test/src/org/tizen/cli/exec/ConsolePrompterTest.java b/org.tizen.cli/test/src/org/tizen/cli/exec/ConsolePrompterTest.java
new file mode 100755 (executable)
index 0000000..229bcc8
--- /dev/null
@@ -0,0 +1,78 @@
+/*\r
+*  Command Line Interface\r
+*\r
+* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+*\r
+* Contact: \r
+* BonYong Lee <bonyong.lee@samsung.com>\r
+* \r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+* http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*\r
+* Contributors:\r
+* - S-Core Co., Ltd\r
+*\r
+*/\r
+package org.tizen.cli.exec;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.mockito.Mockito.mock;\r
+\r
+import java.io.PrintStream;\r
+import java.io.Reader;\r
+import java.io.StringReader;\r
+import java.util.ArrayList;\r
+import java.util.Map;\r
+\r
+import org.junit.Test;\r
+import org.tizen.common.core.command.UserField;\r
+\r
+/**\r
+ * ConsolePrompterTest\r
+ *\r
+ * Test case for {@link ConsolePrompter}\r
+ * \r
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)\r
+ * \r
+ * @see ConsolePrompter\r
+ */\r
+public class\r
+ConsolePrompterTest\r
+{\r
+\r
+    /**\r
+     * Test {@link ConsolePrompter#batch(java.util.Collection)}\r
+     * \r
+     * @throws Exception in case of failure in test\r
+     * \r
+     * @see ConsolePrompter#batch(java.util.Collection)\r
+     */\r
+    @Test\r
+    public void test_batch()\r
+    {\r
+        final PrintStream outMock = mock( PrintStream.class );\r
+        final Reader readerMock = new StringReader( "bylee\ny\nn\n" );\r
+        final ConsolePrompter prompter = new ConsolePrompter( outMock, readerMock );\r
+        \r
+        final ArrayList<UserField> fields = new ArrayList<UserField>();\r
+        fields.add( new UserField( "name", "Input your name?", String.class ) );\r
+        fields.add( new UserField( "sex", "Are you man?", boolean.class ) );\r
+        fields.add( new UserField( "tallerThan180", "Are you taller than 180cm?", Boolean.class ) );\r
+        \r
+        final Map<String, Object> answer = prompter.batch( fields );\r
+        \r
+        assertEquals( "bylee", answer.get( "name" ) );\r
+        assertEquals( true, answer.get( "sex" ) );\r
+        assertEquals( false, answer.get( "tallerThan180" ) );\r
+    }\r
+\r
+}\r