/** * 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_read_and_write_when_copyElementForReadIfNeeded_with_Element_then_returns_different() { Element element = new Element("key", "value"); Element serial = new Element("key", new byte[] {}); when(copyStrategy.copyForRead(serial)).thenReturn(new Element("key", "value")); CopyStrategyHandler copyStrategyHandler = new CopyStrategyHandler(true, true, copyStrategy); assertThat(copyStrategyHandler.copyElementForReadIfNeeded(serial), is(element)); verify(copyStrategy).copyForRead(serial); }
@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); }