/** Tests the shallow equals and copy methods. */ @Test public void testEquals() { Citation citation = Citations.EPSG; final PropertyAccessor accessor = createPropertyAccessor(citation); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, true)); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, false)); assertTrue(accessor.shallowEquals(citation, Citations.EPSG, false)); citation = new CitationImpl(); assertTrue(accessor.shallowCopy(Citations.EPSG, citation, true)); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, true)); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, false)); assertTrue(accessor.shallowEquals(citation, Citations.EPSG, false)); final int index = accessor.indexOf("identifiers"); final Object source = accessor.get(index, Citations.EPSG); final Object target = accessor.get(index, citation); assertNotNull(source); assertNotNull(target); assertNotSame(source, target); assertEquals(source, target); assertTrue(containsEPSG(target)); assertSame(target, accessor.set(index, citation, null)); final Object value = accessor.get(index, citation); assertNotNull(value); assertTrue(((Collection) value).isEmpty()); try { accessor.shallowCopy(citation, Citations.EPSG, true); fail("Citations.EPSG should be unmodifiable."); } catch (UnmodifiableMetadataException e) { // This is the expected exception. } }
/** * Copies all metadata from source to target. The source must implements the same metadata * interface than the target. * * @param source The metadata to copy. * @param target The target metadata. * @param skipNulls If {@code true}, only non-null values will be copied. * @throws ClassCastException if the source or target object don't implements a metadata interface * of the expected package. * @throws UnmodifiableMetadataException if the target metadata is unmodifiable, or if at least * one setter method was required but not found. * @see AbstractMap#AbstractMap(Object) */ public void shallowCopy(final Object source, final Object target, final boolean skipNulls) throws ClassCastException, UnmodifiableMetadataException { ensureNonNull("target", target); final PropertyAccessor accessor = getAccessor(target.getClass()); if (!accessor.type.isInstance(source)) { ensureNonNull("source", source); throw new ClassCastException( Errors.format(ErrorKeys.ILLEGAL_CLASS_$2, source.getClass(), accessor.type)); } if (!accessor.shallowCopy(source, target, skipNulls)) { throw new UnmodifiableMetadataException(Errors.format(ErrorKeys.UNMODIFIABLE_METADATA)); } }