BindingSetStringConverter implements BindingSetConverter<String> { @Override public String convert(final BindingSet bindingSet, final VariableOrder varOrder) { requireNonNull(bindingSet); requireNonNull(varOrder); final List<String> bindingStrings = new ArrayList<>(); for(final String varName : varOrder) { if(bindingSet.hasBinding(varName)) { final Value value = bindingSet.getBinding(varName).getValue(); final RyaType ryaValue = RdfToRyaConversions.convertValue(value); final String bindingString = ryaValue.getData() + TYPE_DELIM + ryaValue.getDataType(); bindingStrings.add(bindingString); } else { bindingStrings.add(NULL_VALUE_STRING); } } return Joiner.on(BINDING_DELIM).join(bindingStrings); }  @Override String convert(final BindingSet bindingSet, final VariableOrder varOrder); @Override BindingSet convert(final String bindingSetString, final VariableOrder varOrder); static final String BINDING_DELIM; static final String TYPE_DELIM; static final String NULL_VALUE_STRING; }
@Test public void noBindings() throws BindingSetConversionException { final MapBindingSet original = new MapBindingSet(); final VariableOrder varOrder = new VariableOrder(); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final String bindingSetString = converter.convert(original, varOrder); final BindingSet converted = converter.convert(bindingSetString, varOrder); assertEquals(original, converted); }
@Test public void toString_URIs() throws BindingSetConversionException { final MapBindingSet originalBindingSet = new MapBindingSet(); originalBindingSet.addBinding("x", VF.createIRI("http: originalBindingSet.addBinding("y", VF.createIRI("http: originalBindingSet.addBinding("z", VF.createIRI("http: final VariableOrder varOrder = new VariableOrder("y", "z", "x"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final String bindingSetString = converter.convert(originalBindingSet, varOrder); final String expected = "http: "http: "http: assertEquals(expected, bindingSetString); }
@Test public void toString_Decimal() throws BindingSetConversionException { final MapBindingSet originalBindingSet = new MapBindingSet(); originalBindingSet.addBinding("x", VF.createLiteral(new BigDecimal(2.5))); final VariableOrder varOrder = new VariableOrder("x"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final String bindingSetString = converter.convert(originalBindingSet, varOrder); final String expected = "2.5<<~>>http: assertEquals(expected, bindingSetString); }
@Test public void toString_Boolean() throws BindingSetConversionException { final MapBindingSet originalBindingSet = new MapBindingSet(); originalBindingSet.addBinding("x", VF.createLiteral((true))); final VariableOrder varOrder = new VariableOrder("x"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final String bindingSetString = converter.convert(originalBindingSet, varOrder); final String expected = "true<<~>>http: assertEquals(expected, bindingSetString); }
@Test public void toString_Integer() throws BindingSetConversionException { final MapBindingSet originalBindingSet = new MapBindingSet(); originalBindingSet.addBinding("x", VF.createLiteral((BigInteger.valueOf(5)))); final VariableOrder varOrder = new VariableOrder("x"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final String bindingSetString = converter.convert(originalBindingSet, varOrder); final String expected = "5<<~>>http: assertEquals(expected, bindingSetString); }
@Test public void toString_bindingsMatchVarOrder() throws BindingSetConversionException { final MapBindingSet originalBindingSet = new MapBindingSet(); originalBindingSet.addBinding("x", VF.createIRI("http: originalBindingSet.addBinding("y", VF.createIRI("http: final VariableOrder varOrder = new VariableOrder("x", "y"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final String bindingSetString = converter.convert(originalBindingSet, varOrder); final String expected = "http: "http: assertEquals(expected, bindingSetString); }
@Test public void toString_bindingsSubsetOfVarOrder() throws BindingSetConversionException { final MapBindingSet originalBindingSet = new MapBindingSet(); originalBindingSet.addBinding("x", VF.createIRI("http: originalBindingSet.addBinding("y", VF.createIRI("http: final VariableOrder varOrder = new VariableOrder("x", "a", "y", "b"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final String bindingSetString = converter.convert(originalBindingSet, varOrder); final String expected = "http: BindingSetStringConverter.NULL_VALUE_STRING + ":::" + "http: BindingSetStringConverter.NULL_VALUE_STRING; assertEquals(expected, bindingSetString); }
@Test public void fromString() throws BindingSetConversionException { final String bindingSetString = "http: "http: "http: final VariableOrder varOrder = new VariableOrder("y", "z", "x"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final BindingSet bindingSet = converter.convert(bindingSetString, varOrder); final MapBindingSet expected = new MapBindingSet(); expected.addBinding("x", VF.createIRI("http: expected.addBinding("y", VF.createIRI("http: expected.addBinding("z", VF.createIRI("http: assertEquals(expected, bindingSet); }
@Test public void fromString_nullValues() throws BindingSetConversionException { final String bindingSetString = "http: BindingSetStringConverter.NULL_VALUE_STRING + ":::" + "http: BindingSetStringConverter.NULL_VALUE_STRING; final VariableOrder varOrder = new VariableOrder("x", "a", "y", "b"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); final BindingSet bindingSet = converter.convert(bindingSetString, varOrder); final MapBindingSet expected = new MapBindingSet(); expected.addBinding("x", VF.createIRI("http: expected.addBinding("y", VF.createIRI("http: assertEquals(expected, bindingSet); }
@Test public void fromString_Decimal() throws BindingSetConversionException { final String bindingSetString = "2.5<<~>>http: final BindingSetConverter<String> converter = new BindingSetStringConverter(); final BindingSet bindingSet = converter.convert(bindingSetString, new VariableOrder("x")); final MapBindingSet expected = new MapBindingSet(); expected.addBinding("x", VF.createLiteral((new BigDecimal(2.5)))); assertEquals(expected, bindingSet); }
@Test public void fromString_Boolean() throws BindingSetConversionException { final String bindingSetString = "true<<~>>http: final BindingSetConverter<String> converter = new BindingSetStringConverter(); final BindingSet bindingSet = converter.convert(bindingSetString, new VariableOrder("x")); final MapBindingSet expected = new MapBindingSet(); expected.addBinding("x", VF.createLiteral((true))); assertEquals(expected, bindingSet); }
@Test public void fromString_Integer() throws BindingSetConversionException { final String bindingSetString = "5<<~>>http: final BindingSetConverter<String> converter = new BindingSetStringConverter(); final BindingSet bindingSet = converter.convert(bindingSetString, new VariableOrder("x")); final MapBindingSet expected = new MapBindingSet(); expected.addBinding("x", VF.createLiteral((BigInteger.valueOf(5)))); assertEquals(expected, bindingSet); }
@Test(expected = IllegalArgumentException.class) public void fromString_varOrderTooShort() throws BindingSetConversionException { final String bindingSetString = "http: "http: final VariableOrder varOrder = new VariableOrder("x"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); converter.convert(bindingSetString, varOrder); }
@Test(expected = IllegalArgumentException.class) public void fromString_varOrderTooLong() throws BindingSetConversionException { final String bindingSetString = "http: "http: final VariableOrder varOrder = new VariableOrder("x", "y", "z"); final BindingSetConverter<String> converter = new BindingSetStringConverter(); converter.convert(bindingSetString, varOrder); }