/** * Perform copy on read on an element if configured * * @param element the element to copy for read * @return a copy of the element with the reconstructed original value */ protected Element copyElementForReadIfNeeded(Element element) { if (copyOnRead && copyOnWrite) { return copyStrategy.copyForRead(element); } else if (copyOnRead) { return copyStrategy.copyForRead(copyStrategy.copyForWrite(element)); } else { return element; } }
@Test public void given_copy_on_write_when_copyElementForWriteIfNeeded_with_Element_then_returns_different() { Element element = new Element("key", "value"); Element serial = new Element("key", new byte[] {}); when(copyStrategy.copyForWrite(element)).thenReturn(serial); when(copyStrategy.copyForRead(serial)).thenReturn(new Element("key", "value")); CopyStrategyHandler copyStrategyHandler = new CopyStrategyHandler(false, true, copyStrategy); assertThat( copyStrategyHandler.copyElementForWriteIfNeeded(element), allOf(not(sameInstance(element)), equalTo(element))); verify(copyStrategy).copyForWrite(element); verify(copyStrategy).copyForRead(serial); }
@Test public void given_copy_on_read_and_write_when_copyElementForRemovalIfNeeded_with_Element_then_returns_different() { Element element = new Element("key", "value"); Element serial = new Element("key", new byte[] {}); when(copyStrategy.copyForWrite(element)).thenReturn(new Element("key", new byte[] {})); CopyStrategyHandler copyStrategyHandler = new CopyStrategyHandler(true, true, copyStrategy); assertThat(copyStrategyHandler.copyElementForRemovalIfNeeded(element), is(serial)); verify(copyStrategy).copyForWrite(element); }
/** * Perform copy for the element If both copy on read and copy on write are set to true * * @param element the element to copy for removal * @return a copy of the element with a storage-ready value */ private Element copyElementForRemovalIfNeeded(Element element) { if (element == null) { return null; } if (copyOnRead && copyOnWrite) { return copyStrategy.copyForWrite(element); } else { return element; } }