@Test public void shouldFindBestMatchDefinition() throws RepositoryException { /* * In cases where there is more than one valid definition for the same property, * the best match should be returned. */ Value doubleValue = session.getValueFactory().createValue(0.7); Value longValue = session.getValueFactory().createValue(10); Value stringValue = session.getValueFactory().createValue("Should not work"); JcrPropertyDefinition propDef; // Should prefer the double definition from NODE_TYPE_C since the value is of type double propDef = nodeTypes() .findPropertyDefinition( session, NODE_TYPE_C, Collections.<Name>emptyList(), SINGLE_PROP2, doubleValue, true, true); assertThat(propDef, is(notNullValue())); assertEquals(propDef.getRequiredType(), PropertyType.DOUBLE); // Should prefer the long definition from NODE_TYPE_C since the value is of type long propDef = nodeTypes() .findPropertyDefinition( session, NODE_TYPE_C, Collections.<Name>emptyList(), SINGLE_PROP2, longValue, true, true); assertThat(propDef, is(notNullValue())); assertEquals(propDef.getRequiredType(), PropertyType.LONG); // Should not allow a string though, since the NODE_TYPE_C definition narrows the acceptable // types to double and long propDef = nodeTypes() .findPropertyDefinition( session, NODE_TYPE_C, Collections.<Name>emptyList(), SINGLE_PROP2, stringValue, true, true); assertThat(propDef, is(nullValue())); }
@Test public void shouldUseNearestPropertyDefinition() { // If a property is defined at multiple points in the type hierarchy, the property definition // closest to the given type // should be used. JcrPropertyDefinition propDef; propDef = nodeTypes() .findPropertyDefinition( session, NODE_TYPE_A, Collections.<Name>emptyList(), SINGLE_PROP1, null, true, true, true); assertThat(propDef, is(notNullValue())); assertEquals(propDef.getRequiredType(), PropertyType.STRING); propDef = nodeTypes() .findPropertyDefinition( session, NODE_TYPE_B, Collections.<Name>emptyList(), SINGLE_PROP1, null, true, true, true); assertThat(propDef, is(notNullValue())); assertEquals(propDef.getRequiredType(), PropertyType.DOUBLE); propDef = nodeTypes() .findPropertyDefinition( session, NODE_TYPE_C, Collections.<Name>emptyList(), SINGLE_PROP1, null, true, true, true); assertThat(propDef, is(notNullValue())); assertEquals(propDef.getRequiredType(), PropertyType.LONG); }