From: Bon-Yong Lee Date: Tue, 13 Nov 2012 08:45:56 +0000 (+0900) Subject: [Title] Share RI for Prompter#batch(Collection) X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=467e1b0d3e598e0df06dce2699265a2841e71435;p=sdk%2Ftools%2Fcli.git [Title] Share RI for Prompter#batch(Collection) [Desc.] Share first implement for collaboration [Issue] #7420 --- diff --git a/org.tizen.cli/src/org/tizen/cli/exec/ConsolePrompter.java b/org.tizen.cli/src/org/tizen/cli/exec/ConsolePrompter.java index 41a1b75..113659d 100755 --- a/org.tizen.cli/src/org/tizen/cli/exec/ConsolePrompter.java +++ b/org.tizen.cli/src/org/tizen/cli/exec/ConsolePrompter.java @@ -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 @@ -228,7 +235,128 @@ implements Prompter final Collection userFields ) { - return null; + final HashMap ret = new HashMap(); + batch( 0, userFields, ret ); + return ret; + } + + /** + *

+ * Process fields for depth level + * + * process result are stored in answer + *

+ * @param depth process depth + * @param fields user input specifications + * @param answer user answer + */ + protected + void + batch( + final int depth, + final Collection fields, + final Map answer + ) + { + for ( final UserField child : fields ) + { + batch( depth, child, answer ); + } + } + + /** + *

+ * Process field for depth level + * + * process result are stored in answer + *

+ * @param depth process depth + * @param field user input specification + * @param answer user answer + */ + protected + void + batch( + final int depth, + final UserField field, + final Map answer + ) + { + final String id = field.getId(); + final String msg = field.getMessage(); + + final Collection 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 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 +* +* 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.cli.exec; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +import java.io.PrintStream; +import java.io.Reader; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Map; + +import org.junit.Test; +import org.tizen.common.core.command.UserField; + +/** + * ConsolePrompterTest + * + * Test case for {@link ConsolePrompter} + * + * @author BonYong Lee{@literal } (S-Core) + * + * @see ConsolePrompter + */ +public class +ConsolePrompterTest +{ + + /** + * Test {@link ConsolePrompter#batch(java.util.Collection)} + * + * @throws Exception in case of failure in test + * + * @see ConsolePrompter#batch(java.util.Collection) + */ + @Test + public void test_batch() + { + final PrintStream outMock = mock( PrintStream.class ); + final Reader readerMock = new StringReader( "bylee\ny\nn\n" ); + final ConsolePrompter prompter = new ConsolePrompter( outMock, readerMock ); + + final ArrayList fields = new ArrayList(); + fields.add( new UserField( "name", "Input your name?", String.class ) ); + fields.add( new UserField( "sex", "Are you man?", boolean.class ) ); + fields.add( new UserField( "tallerThan180", "Are you taller than 180cm?", Boolean.class ) ); + + final Map answer = prompter.batch( fields ); + + assertEquals( "bylee", answer.get( "name" ) ); + assertEquals( true, answer.get( "sex" ) ); + assertEquals( false, answer.get( "tallerThan180" ) ); + } + +}