Skip to content

Commit c3789ba

Browse files
rmannibucaumichael-o
authored andcommitted
ensure prompter does not double colon
1 parent 5e9568b commit c3789ba

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,18 @@ public class DefaultPrompter
4949
*/
5050
private InputHandler inputHandler;
5151

52-
public String prompt( String message )
52+
public DefaultPrompter()
53+
{
54+
super();
55+
}
56+
57+
public DefaultPrompter( OutputHandler outputHandler, InputHandler inputHandler )
58+
{
59+
this.outputHandler = outputHandler;
60+
this.inputHandler = inputHandler;
61+
}
62+
63+
public String prompt(String message )
5364
throws PrompterException
5465
{
5566
try
@@ -207,7 +218,7 @@ private String formatMessage( String message, List<String> possibleValues, Strin
207218

208219
if ( defaultReply != null )
209220
{
210-
formatted.append( ' ' ).append( defaultReply ).append( ": " );
221+
formatted.append( ' ' ).append( defaultReply );
211222
}
212223

213224
return formatted.toString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@
5151
</plugins>
5252
</build>
5353

54+
<dependencies>
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.13.2</version>
59+
<scope>test</scope>
60+
</dependency>
61+
</dependencies>
5462
</project>

0 commit comments

Comments
 (0)