Options { public List<String> processFields(@Nonnull String optionSpecsString, @Nullable Object... opts) throws OptionsException { throwOptionsExceptionIfConditionFails(nonNull(optionSpecsString), "options specs are required"); List<String> optsList = new ArrayList<>(); if (nonNull(opts)) { String[] optionSpecExpressions = optionSpecsString.split(SPACE); throwOptionsExceptionIfConditionFails(optionSpecExpressions.length == opts.length, "specs vs opts size mismatch in options processor"); String currentOptionSpecExpression = EMPTY; try { for (int i = 0; i < opts.length; i++) { currentOptionSpecExpression = optionSpecExpressions[i]; throwOptionsExceptionIfConditionFails(isNotBlank(currentOptionSpecExpression), "null options spec in options processor: %s", optionSpecsString); String[] optSpecParts = currentOptionSpecExpression.split(":"); throwOptionsExceptionIfConditionFails(optSpecParts.length == 2 || optSpecParts.length == 3, "null options spec in options processor: %s", optionSpecsString); String ruleName = EMPTY; if (optSpecParts.length >= 3) { ruleName = optSpecParts[2]; } String serverOptionStr = optSpecParts[1]; String optSpecType = optSpecParts[0]; OptionsSpecType p4CommandOptions = OptionsSpecType.of(optSpecType); optsList.addAll(p4CommandOptions.getP4CommandOptionFields(ruleName, serverOptionStr, opts[i])); } } catch (Exception exc) { throwOptionsException(exc, "bad conversion encountered in options processor with option string '%s': %s", currentOptionSpecExpression, exc.getLocalizedMessage()); } } return optsList; } Options();  Options(String... options); void setOptions(String... options); void setImmutable(boolean immutable); boolean isImmutable(); List<String> getOptions(); List<String> processFields(@Nonnull String optionSpecsString, @Nullable Object... opts); abstract List<String> processOptions(IServer server);  }
@Test public void testProcessFields_withExceptions() { Options options = new MockOptions(); assertThrows(OptionsException.class, () -> options.processFields(null, new Object[]{true, true})); String optionSpecsString = "b:f b:r b:d b:p"; assertThrows(OptionsException.class, () -> options.processFields(optionSpecsString, new Object[]{true, true})); String optionSpecsStringHasSpace = "b:f b:r"; assertThrows(OptionsException.class, () -> options.processFields(optionSpecsStringHasSpace, new Object[]{true, true})); String optionSpecsStringHasNull = "b:f null"; assertThrows(OptionsException.class, () -> options.processFields(optionSpecsStringHasNull, new Object[]{true, true})); String optionSpecsStringWithSpaceValue = "b: b:r"; assertThrows(OptionsException.class, () -> options.processFields(optionSpecsStringWithSpaceValue, new Object[]{true, true})); String optionSpecsStringHasMoreThan3Parts = "b:f:a:d b:r"; assertThrows(OptionsException.class, () -> options.processFields(optionSpecsStringHasMoreThan3Parts, new Object[]{true, true})); String unknownOptionType = "b:f:a:d d:r"; assertThrows(OptionsException.class, () -> options.processFields(unknownOptionType, new Object[]{true, true})); assertThrows(OptionsException.class, () -> options.processFields(unknownOptionType, new Object[]{true, true})); String optionSpecString = "b:f s:p l:g"; Object[] wrongValue = new Object[]{false, "test", "wrong long number"}; assertThrows(OptionsException.class, () -> options.processFields(optionSpecString, wrongValue)); String optionSpecString2 = "b:f s:p s[]:g"; Object[] isNotStringArray = new Object[]{"a", 1, "d"}; Object[] wrongValue2 = new Object[]{false, "test", isNotStringArray}; assertThrows(OptionsException.class, () -> options.processFields(optionSpecString2, wrongValue2)); }
@Test public void testProcessFields() throws OptionsException { Options options = new MockOptions(); String optionSpecsString = "b:f s:r i:d:gtz s[]:e"; String[] stringArrayOptions = new String[] { "myKey-*", "sshKey-*" }; List<String> processFields = options.processFields(optionSpecsString, false, "@2016/09/08", 15, stringArrayOptions); Assert.assertEquals(4, processFields.size()); Assert.assertEquals("-r@2016/09/08", processFields.get(0)); Assert.assertEquals("-d15", processFields.get(1)); Assert.assertEquals("-emyKey-*", processFields.get(2)); Assert.assertEquals("-esshKey-*", processFields.get(3)); }
@Test public void testProcessFields_withExceptions() { Options options = new MockOptions(); try { options.processFields(null, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String optionSpecsString = "b:f b:r b:d b:p"; try { options.processFields(optionSpecsString, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String optionSpecsStringHasSpace = "b:f b:r"; try { options.processFields(optionSpecsStringHasSpace, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String optionSpecsStringHasNull = "b:f null"; try { options.processFields(optionSpecsStringHasNull, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String optionSpecsStringWithSpaceValue = "b: b:r"; try { options.processFields(optionSpecsStringWithSpaceValue, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String optionSpecsStringHasMoreThan3Parts = "b:f:a:d b:r"; try { options.processFields(optionSpecsStringHasMoreThan3Parts, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String unknownOptionType = "b:f:a:d d:r"; try { options.processFields(unknownOptionType, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } try { options.processFields(unknownOptionType, new Object[] { true, true }); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String optionSpecString = "b:f s:p l:g"; Object[] wrongValue = new Object[] { false, "test", "wrong long number" }; try { options.processFields(optionSpecString, wrongValue); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } String optionSpecString2 = "b:f s:p s[]:g"; Object[] isNotStringArray = new Object[] { "a", 1, "d" }; Object[] wrongValue2 = new Object[] { false, "test", isNotStringArray }; try { options.processFields(optionSpecString2, wrongValue2); Assert.fail("No exception thrown!"); } catch (Throwable e) { Assert.assertEquals(OptionsException.class, e.getClass()); } }
@Test public void testProcessFields() throws OptionsException { Options options = new MockOptions(); String optionSpecsString = "b:f s:r i:d:gtz s[]:e"; String[] stringArrayOptions = new String[]{"myKey-*", "sshKey-*"}; List<String> processFields = options.processFields(optionSpecsString, false, "@2016/09/08", 15, stringArrayOptions); assertThat(processFields.size(), is(4)); assertThat(processFields.get(0), is("-r@2016/09/08")); assertThat(processFields.get(1), is("-d15")); assertThat(processFields.get(2), is("-emyKey-*")); assertThat(processFields.get(3), is("-esshKey-*")); }
@Test public void testProcessFields_withExceptions() { Options options = new MockOptions(); expectThrows(OptionsException.class, () -> options.processFields(null, new Object[]{true, true})); String optionSpecsString = "b:f b:r b:d b:p"; expectThrows(OptionsException.class, () -> options.processFields(optionSpecsString, new Object[]{true, true})); String optionSpecsStringHasSpace = "b:f b:r"; expectThrows(OptionsException.class, () -> options.processFields(optionSpecsStringHasSpace, new Object[]{true, true})); String optionSpecsStringHasNull = "b:f null"; expectThrows(OptionsException.class, () -> options.processFields(optionSpecsStringHasNull, new Object[]{true, true})); String optionSpecsStringWithSpaceValue = "b: b:r"; expectThrows(OptionsException.class, () -> options.processFields(optionSpecsStringWithSpaceValue, new Object[]{true, true})); String optionSpecsStringHasMoreThan3Parts = "b:f:a:d b:r"; expectThrows(OptionsException.class, () -> options.processFields(optionSpecsStringHasMoreThan3Parts, new Object[]{true, true})); String unknownOptionType = "b:f:a:d d:r"; expectThrows(OptionsException.class, () -> options.processFields(unknownOptionType, new Object[]{true, true})); expectThrows(OptionsException.class, () -> options.processFields(unknownOptionType, new Object[]{true, true})); String optionSpecString = "b:f s:p l:g"; Object[] wrongValue = new Object[]{false, "test", "wrong long number"}; expectThrows(OptionsException.class, () -> options.processFields(optionSpecString, wrongValue)); String optionSpecString2 = "b:f s:p s[]:g"; Object[] isNotStringArray = new Object[]{"a", 1, "d"}; Object[] wrongValue2 = new Object[]{false, "test", isNotStringArray}; expectThrows(OptionsException.class, () -> options.processFields(optionSpecString2, wrongValue2)); }