MultipleChoice extends ChoiceBase { public <T> List<T> select(T[] defaultValue, List<T> options) { if (null == defaultValue) { throw new NullPointerException("Default value MUST be always filled."); } Set<T> allDefaults = new HashSet<>(); List<T> defaults = new ArrayList<>(defaultValue.length); defaults.addAll(Arrays.asList(defaultValue)); allDefaults.addAll(defaults); options.forEach(defaults::remove); if (defaults.size() > 0) { throw new IllegalArgumentException("Defaults must be included in options, but these were not: " + defaults); } List<Option<T>> optionList = new ArrayList<>(options.size()); for (final T option : options) { Option<T> newOption = new Option<>(option); if (allDefaults.contains(option)) { newOption.setSelected(true); } optionList.add(newOption); } return optionsAsList(_select(optionList)); } MultipleChoice(final Builder builder); List<T> select(T[] defaultValue, List<T> options); @SafeVarargs final List<T> select(T[] defaultValue, T... options); @SafeVarargs final List<Option<T>> select(final Option<T>... options); @SafeVarargs final List<Option<T>> select(final String systemProperty, final Option<T>... options); List<Option<T>> select(final List<Option<T>> options); List<Option<T>> select(final String systemProperty, final List<Option<T>> options);  }
@Test public void testDefaultSimpleVarargs() throws Exception { System.setProperty("clichoice.useDefault", "true"); String[] defaults = new String[]{"a", "d"}; List<String> select = choice.select(defaults, "a", "b", "c", "d"); assertArrayEquals("Must select default value if in batch mode", defaults, select.toArray(new String[select.size()])); System.setProperty("clichoice.useDefault", "false"); }
@Test public void testDefaultSimpleVarargInteractive() throws Exception { System.setProperty("clichoice.forceInput", "true"); System.setProperty("clichoice.useDefault", "false"); PipedOutputStream outputStream = new PipedOutputStream(); PipedInputStream inputStream = new PipedInputStream(outputStream); InputStream original = System.in; System.setIn(inputStream); Thread t = new Thread(() -> { try { Thread.sleep(500); outputStream.write("b\r\n".getBytes()); outputStream.write("c\r\n".getBytes()); outputStream.write("d\r\n".getBytes()); outputStream.write("x\r\n".getBytes()); outputStream.write("45\r\n".getBytes()); outputStream.write("\r\n".getBytes()); outputStream.flush(); Thread.sleep(1000); } catch (Exception e) { System.err.println("Failed to write user input"); e.printStackTrace(); } }); t.start(); try { String[] defaults = new String[]{"a", "d"}; String[] expected = new String[]{"a", "b", "c"}; List<String> select = choice.select(defaults, "a", "b", "c", "d"); assertArrayEquals("Must select the correct values in interactive mode", expected, select.toArray(new String[select.size()])); } finally { System.setProperty("clichoice.forceInput", "false"); System.setIn(original); } }
@Test public void testDefaultSimpleVarargsQuiet() throws Exception { System.setProperty("clichoice.quiet", "true"); String[] defaults = new String[]{"a", "d"}; List<String> select = choice.select(defaults, "a", "b", "c", "d"); assertArrayEquals("Must select default value if in batch mode", defaults, select.toArray(new String[select.size()])); System.setProperty("clichoice.quiet", "false"); }
@Test public void testDefaultSimpleList() throws Exception { System.setProperty("clichoice.useDefault", "true"); String[] defaults = new String[]{"a", "d"}; List<String> options = Arrays.asList("a", "b", "c", "d"); List<String> select = choice.select(defaults, options); assertArrayEquals("Must select default value if in batch mode", defaults, select.toArray(new String[select.size()])); System.setProperty("clichoice.useDefault", "false"); }
@Test(expected = NullPointerException.class) public void testSimpleListNoDefaults() throws Exception { System.setProperty("clichoice.useDefault", "true"); List<String> options = Arrays.asList("a", "b", "c", "d"); List<String> select = choice.select(null, options); System.setProperty("clichoice.useDefault", "false"); }
@Test(expected = IllegalArgumentException.class) public void testSimpleListWrongDefaults() throws Exception { System.setProperty("clichoice.useDefault", "true"); String[] defaults = new String[]{"a", "x"}; List<String> options = Arrays.asList("a", "b", "c", "d"); List<String> select = choice.select(defaults, options); assertArrayEquals("Must select default value if in batch mode", defaults, select.toArray(new String[select.size()])); System.setProperty("clichoice.useDefault", "false"); }
@Test public void testDefaultOptionVarargs() throws Exception { System.setProperty("clichoice.useDefault", "true"); List<Option<String>> select = choice.select( new Option<>("a", "a", true), new Option<>("b"), new Option<>("c"), new Option<>("d", "d", true) ); List<Option<String>> defaults = new ArrayList<>(); defaults.add(new Option<>("a", "a", true)); defaults.add(new Option<>("d", "d", true)); assertEquals("Must select default value if in batch mode", defaults, select); System.setProperty("clichoice.useDefault", "false"); }
@Test public void testDefaultOptionList() throws Exception { System.setProperty("clichoice.useDefault", "true"); List<Option<String>> options = Arrays.asList(new Option<>("a", "a", true), new Option<>("b"), new Option<>("c"), new Option<>("d", "d", true)); List<Option<String>> select = choice.select(options); List<Option<String>> defaults = new ArrayList<>(); defaults.add(new Option<>("a", "a", true)); defaults.add(new Option<>("d", "d", true)); assertEquals("Must select default value if in batch mode", defaults, select); System.setProperty("clichoice.useDefault", "false"); }
@Test public void testDefaultOptionVarargsSysprop() throws Exception { System.setProperty("clichoice.useDefault", "true"); System.setProperty("clichoice.test.sysprop", "b,d"); List<Option<String>> select = choice.select( "clichoice.test.sysprop", new Option<>("a", "a", true), new Option<>("b"), new Option<>("c"), new Option<>("d", "d", true) ); List<Option<String>> defaults = new ArrayList<>(); defaults.add(new Option<>("b", "b", true)); defaults.add(new Option<>("d", "d", true)); assertEquals("Must select default value if in batch mode", defaults, select); System.setProperty("clichoice.test.sysprop", ""); System.setProperty("clichoice.useDefault", "false"); }
@Test public void testDefaultOptionListSysprop() throws Exception { System.setProperty("clichoice.useDefault", "true"); System.setProperty("clichoice.test.sysprop", "b,d"); List<Option<String>> options = Arrays.asList(new Option<>("a"), new Option<>("b"), new Option<>("c"), new Option<>("d")); List<Option<String>> select = choice.select("clichoice.test.sysprop", options); List<Option<String>> defaults = new ArrayList<>(); defaults.add(new Option<>("b", "b", true)); defaults.add(new Option<>("d", "d", true)); assertEquals("Must select default value if in batch mode", defaults, select); System.setProperty("clichoice.test.sysprop", ""); System.setProperty("clichoice.useDefault", "false"); }