|
| 1 | +package picocli; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import picocli.CommandLine.ITypeConverter; |
| 7 | +import picocli.CommandLine.Option; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Base64; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 15 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 16 | +import static org.junit.Assert.*; |
| 17 | + |
| 18 | +public class Issue2270 { |
| 19 | + static class CmdObjectBoolean { |
| 20 | + @Option(names = "--test-Boolean", defaultValue = "dHJ1ZQ==" /* base64-encoded 'true' */) |
| 21 | + Boolean testBoolean; |
| 22 | + } |
| 23 | + static class CmdPrimitiveBoolean { |
| 24 | + @Option(names = "--test-boolean", defaultValue = "dHJ1ZQ==" /* base64-encoded 'true' */) |
| 25 | + boolean testboolean; |
| 26 | + } |
| 27 | + |
| 28 | + static class Base64BooleanTypeConverter implements ITypeConverter<Boolean> { |
| 29 | + |
| 30 | + static List<String> invocations = new ArrayList<>(); |
| 31 | + |
| 32 | + public Boolean convert(String value) { |
| 33 | + invocations.add(value); |
| 34 | + System.out.printf("converter invocation %s: called with value %s%n", |
| 35 | + invocations.size(), value); |
| 36 | + if (Boolean.parseBoolean(value)) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + return Boolean.parseBoolean(new String(Base64.getDecoder().decode(value))); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Before |
| 44 | + public void beforeTests() { |
| 45 | + CommandLine.tracer().setLevel(CommandLine.TraceLevel.DEBUG); |
| 46 | + } |
| 47 | + |
| 48 | + @After |
| 49 | + public void afterTests() { |
| 50 | + CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testObjectBoolean() { |
| 55 | + Base64BooleanTypeConverter.invocations.clear(); |
| 56 | + CmdObjectBoolean cmd = new CmdObjectBoolean(); |
| 57 | + new CommandLine(cmd) |
| 58 | + .registerConverter(Boolean.class, new Base64BooleanTypeConverter()) |
| 59 | + .parseArgs(); |
| 60 | + assertThat(Base64BooleanTypeConverter.invocations.size(), greaterThanOrEqualTo(1)); |
| 61 | + assertEquals(Arrays.asList("dHJ1ZQ==", "true"), Base64BooleanTypeConverter.invocations); |
| 62 | + |
| 63 | + assertTrue(cmd.testBoolean); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testPrimitiveBoolean() { |
| 68 | + Base64BooleanTypeConverter.invocations.clear(); |
| 69 | + CmdPrimitiveBoolean cmd = new CmdPrimitiveBoolean(); |
| 70 | + new CommandLine(cmd) |
| 71 | + .registerConverter(Boolean.TYPE, new Base64BooleanTypeConverter()) |
| 72 | + .parseArgs(); |
| 73 | + assertThat(Base64BooleanTypeConverter.invocations.size(), greaterThanOrEqualTo(1)); |
| 74 | + assertEquals(Arrays.asList("dHJ1ZQ==", "true"), Base64BooleanTypeConverter.invocations); |
| 75 | + |
| 76 | + assertTrue(cmd.testboolean); |
| 77 | + } |
| 78 | +} |
0 commit comments