/** * Writes the given value to the JSON writer. currently the following conversions are done: * * <table> * <tr> * <th>JSR Property Type</th> * <th>JSON Value Type</th> * </tr> * <tr> * <td>BINARY</td> * <td>always 0 as long</td> * </tr> * <tr> * <td>DATE</td> * <td>converted date string as defined by ECMA</td> * </tr> * <tr> * <td>BOOLEAN</td> * <td>boolean</td> * </tr> * <tr> * <td>LONG</td> * <td>long</td> * </tr> * <tr> * <td>DOUBLE</td> * <td>double</td> * </tr> * <tr> * <td><i>all other</li> * </td> * <td>string</td> * </tr> * </table> * * <sup>1</sup> Currently not implemented and uses 0 as default. * * @param w json writer * @param v value to dump */ protected void dumpValue(JSONWriter w, Value v) throws ValueFormatException, IllegalStateException, RepositoryException, JSONException { switch (v.getType()) { case PropertyType.BINARY: w.value(0); break; case PropertyType.DATE: w.value(format(v.getDate())); break; case PropertyType.BOOLEAN: w.value(v.getBoolean()); break; case PropertyType.LONG: w.value(v.getLong()); break; case PropertyType.DOUBLE: w.value(v.getDouble()); break; default: w.value(v.getString()); } }
@Test public void testNextWithLiteralLong() throws Exception { when(mockValue.getType()).thenReturn(PropertyType.LONG); when(mockValue.getLong()).thenReturn(1L); final QuerySolution solution = testObj.next(); assertEquals(1L, solution.get("a").asLiteral().getLong()); }
@Test public void testScore() throws RepositoryException { Row row = createMock(Row.class); Value value = createMock(Value.class); expect(value.getLong()).andReturn(Long.parseLong("1554")); expect(row.getValue("jcr:score")).andReturn(value); replay(); long score = RowUtils.getScore(row); assertEquals("Score is not the same", 1554, score); }
/** Tests conversion from Date type to Long type. */ public void testGetLong() throws RepositoryException { Value val = PropertyUtil.getValue(prop); long l = val.getLong(); long mili = val.getDate().getTimeInMillis(); assertEquals( "Conversion from a Date value to a Long value " + "returns a different number of miliseconds.", mili, l); }
@Test public void ferrari_is_available_with_capacity_3500cc() throws RepositoryException { Node ferrari = session.getNode("/cars/ferrari"); Value[] capacities = ferrari.getProperty("catalog:enginecapacity").getValues(); boolean isFound = false; for (Value capacity : capacities) { isFound = capacity.getLong() == 3500 ? true : isFound; } assertThat(isFound, is(true)); }
/** * Utility method used to convert a JCR {@link Value} object into a valid graph property value. * * @param value the JCR value; may be null * @param jcrProperty the JCR property, used to access the session (if needed) * @return the graph representation of the value * @throws RepositoryException if there is an error working with the session */ public Object convert(Value value, javax.jcr.Property jcrProperty) throws RepositoryException { if (value == null) return null; try { switch (value.getType()) { case javax.jcr.PropertyType.BINARY: return factories.getBinaryFactory().create(value.getBinary().getStream(), 0); case javax.jcr.PropertyType.BOOLEAN: return factories.getBooleanFactory().create(value.getBoolean()); case javax.jcr.PropertyType.DATE: return factories.getDateFactory().create(value.getDate()); case javax.jcr.PropertyType.DOUBLE: return factories.getDoubleFactory().create(value.getDouble()); case javax.jcr.PropertyType.LONG: return factories.getLongFactory().create(value.getLong()); case javax.jcr.PropertyType.NAME: return factories.getNameFactory().create(value.getString()); case javax.jcr.PropertyType.PATH: return factories.getPathFactory().create(value.getString()); case javax.jcr.PropertyType.REFERENCE: return factories.getReferenceFactory().create(value.getString()); case javax.jcr.PropertyType.STRING: return factories.getStringFactory().create(value.getString()); case javax.jcr.PropertyType.UNDEFINED: // We don't know what type of values these are, but we need to convert any value // that depends on namespaces (e.g., names and paths) into Graph objects. try { // So first try a name ... return factories.getNameFactory().create(value.getString()); } catch (ValueFormatException e) { // Wasn't a name, so try a path ... try { return factories.getPathFactory().create(value.getString()); } catch (ValueFormatException e2) { // Wasn't a path, so finally treat as a string ... return factories.getStringFactory().create(value.getString()); } } } } catch (ValueFormatException e) { // There was an error converting the JCR value into the appropriate graph value String typeName = javax.jcr.PropertyType.nameFromValue(value.getType()); throw new RepositoryException( JcrConnectorI18n.errorConvertingJcrValueOfType.text(value.getString(), typeName)); } return null; }
protected void assertCanObtainValue(Value value, int expectedType) throws Exception { switch (expectedType) { case PropertyType.BINARY: Binary binary = value.getBinary(); try { InputStream stream = binary.getStream(); assertThat(stream, is(notNullValue())); try { stream.read(); } finally { stream.close(); } } finally { binary.dispose(); } break; case PropertyType.BOOLEAN: assertThat(value.getBoolean() || !value.getBoolean(), is(true)); break; case PropertyType.DATE: Calendar cal = value.getDate(); assertThat(cal, is(notNullValue())); break; case PropertyType.DOUBLE: double doubleValue = value.getDouble(); assertThat(doubleValue < 0.0d || doubleValue >= -1.0d, is(true)); break; case PropertyType.LONG: long longValue = value.getLong(); assertThat(longValue < 0L || longValue >= 0L, is(true)); break; case PropertyType.NAME: context.getValueFactories().getNameFactory().create(value.getString()); break; case PropertyType.PATH: context.getValueFactories().getPathFactory().create(value.getString()); break; case PropertyType.REFERENCE: UUID uuid = context.getValueFactories().getUuidFactory().create(value.getString()); assertThat(uuid, is(notNullValue())); break; case PropertyType.STRING: value.getString(); break; } }
private static Object stringValue(Value value) throws ValueFormatException, IllegalStateException, RepositoryException { switch (value.getType()) { case PropertyType.STRING: case PropertyType.NAME: case PropertyType.REFERENCE: case PropertyType.PATH: return value.getString(); case PropertyType.BOOLEAN: return value.getBoolean(); case PropertyType.LONG: return value.getLong(); case PropertyType.DOUBLE: return value.getDouble(); case PropertyType.DATE: return DateUtils.iso8601(value.getDate()); default: return value.toString(); } }