|
| 1 | +package org.codehaus.plexus.components.interactivity; |
| 2 | + |
| 3 | +/* |
| 4 | + * The MIT License |
| 5 | + * |
| 6 | + * Copyright (c) 2005, The Codehaus |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 9 | + * this software and associated documentation files (the "Software"), to deal in |
| 10 | + * the Software without restriction, including without limitation the rights to |
| 11 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 12 | + * of the Software, and to permit persons to whom the Software is furnished to do |
| 13 | + * so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static java.util.Arrays.asList; |
| 32 | +import static java.util.Objects.requireNonNull; |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | + |
| 35 | +public class DefaultPrompterTest |
| 36 | +{ |
| 37 | + @Test |
| 38 | + public void promptSimple() throws PrompterException { |
| 39 | + final InMemoryOutput out = new InMemoryOutput(); |
| 40 | + final Prompter prompter = new DefaultPrompter( out, new InMemoryInput( "ok" ) ); |
| 41 | + prompter.prompt( "test" ); |
| 42 | + assertEquals( "test: ", out.builder.toString() ); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void promptOption() throws PrompterException { |
| 47 | + final InMemoryOutput out = new InMemoryOutput(); |
| 48 | + final Prompter prompter = new DefaultPrompter( out, new InMemoryInput( "ok" ) ); |
| 49 | + prompter.prompt( "test", "value" ); |
| 50 | + assertEquals("test value: ", out.builder.toString()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void promptOptions() throws PrompterException { |
| 55 | + final InMemoryOutput out = new InMemoryOutput(); |
| 56 | + final Prompter prompter = new DefaultPrompter( out, new InMemoryInput( "yes" ) ); |
| 57 | + prompter.prompt( "test", asList( "yes", "no" ), "value" ); |
| 58 | + assertEquals("test (yes/no) value: ", out.builder.toString()); |
| 59 | + } |
| 60 | + |
| 61 | + private static class InMemoryInput implements InputHandler |
| 62 | + { |
| 63 | + private final String line; |
| 64 | + |
| 65 | + private InMemoryInput( String line ) |
| 66 | + { |
| 67 | + this.line = requireNonNull(line); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String readLine() { |
| 72 | + return line; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String readPassword() |
| 77 | + { |
| 78 | + throw new UnsupportedOperationException(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public List<String> readMultipleLines() |
| 83 | + { |
| 84 | + throw new UnsupportedOperationException(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static class InMemoryOutput implements OutputHandler |
| 89 | + { |
| 90 | + private final StringBuilder builder = new StringBuilder(); |
| 91 | + |
| 92 | + @Override |
| 93 | + public void write( String line ) |
| 94 | + { |
| 95 | + builder.append( line ); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void writeLine( String line ) |
| 100 | + { |
| 101 | + builder.append( line ); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments