Skip to content

Commit

Permalink
Issue 26: Add Answers to ValueFactory mock instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf.bubenik committed Dec 18, 2024
1 parent b45a26c commit e182e5e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@
* #L%
*/

import org.mockito.stubbing.Answer;

import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;

import java.util.Calendar;

import static de.ibmix.magkit.test.jcr.ValueMockUtils.mockValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

/**
Expand All @@ -33,11 +47,33 @@
*/
public final class ValueFactoryMockUtils {

private static final Answer<Value> MOCK_STRING_VALUE = invocationOnMock -> mockValue(invocationOnMock.getArgument(0, String.class));
private static final Answer<Value> MOCK_BOOLEAN_VALUE = invocationOnMock -> mockValue(invocationOnMock.getArgument(0, Boolean.class));
private static final Answer<Value> MOCK_DOUBLE_VALUE = invocationOnMock -> mockValue(invocationOnMock.getArgument(0, Double.class));
private static final Answer<Value> MOCK_LONG_VALUE = invocationOnMock -> mockValue(invocationOnMock.getArgument(0, Long.class));
private static final Answer<Value> MOCK_CALENDAR_VALUE = invocationOnMock -> mockValue(invocationOnMock.getArgument(0, Calendar.class));
private static final Answer<Value> MOCK_BINARY_VALUE = invocationOnMock -> mockValue(invocationOnMock.getArgument(0, Binary.class));
private static final Answer<Value> MOCK_NODE_VALUE = invocationOnMock -> mockValue(invocationOnMock.getArgument(0, Node.class));

private ValueFactoryMockUtils() {
}

/**
* Create a ValueFactory mock with Answers for the create methods tha return a Value mock for the given value object.
*
* @param stubbings the ValueFactoryStubbingOperation to specify the behaviour of the mock
* @return a new mock instance of ValueFactory
* @throws RepositoryException never
*/
public static ValueFactory mockValueFactory(ValueFactoryStubbingOperation... stubbings) throws RepositoryException {
ValueFactory result = mock(ValueFactory.class);
doAnswer(MOCK_STRING_VALUE).when(result).createValue(anyString());
doAnswer(MOCK_BOOLEAN_VALUE).when(result).createValue(anyBoolean());
doAnswer(MOCK_DOUBLE_VALUE).when(result).createValue(anyDouble());
doAnswer(MOCK_LONG_VALUE).when(result).createValue(anyLong());
doAnswer(MOCK_CALENDAR_VALUE).when(result).createValue(any(Calendar.class));
doAnswer(MOCK_BINARY_VALUE).when(result).createValue(any(Binary.class));
doAnswer(MOCK_NODE_VALUE).when(result).createValue(any(Node.class));
for (ValueFactoryStubbingOperation stubbing : stubbings) {
stubbing.of(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@

import org.junit.Test;

import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.ValueFactory;

import java.util.Calendar;

import static de.ibmix.magkit.test.jcr.NodeMockUtils.mockNode;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -39,7 +46,6 @@
*/
public class ValueFactoryMockUtilsTest {


/**
* Test of mockValueFactory method, of class ValueFactoryMockUtils.
*/
Expand All @@ -52,5 +58,16 @@ public void testMockValueFactory() throws RepositoryException {
assertThat(factory, notNullValue());
verify(op1, times(1)).of(factory);
verify(op2, times(1)).of(factory);

assertEquals("test", factory.createValue("test").getString());
assertEquals(2.3D, factory.createValue(2.3D).getDouble(), 0.0);
assertEquals(123, factory.createValue(123L).getLong());
assertTrue(factory.createValue(true).getBoolean());
Calendar now = Calendar.getInstance();
assertEquals(now, factory.createValue(now).getDate());
Binary binary = mock(Binary.class);
assertEquals(binary, factory.createValue(binary).getBinary());
Node node = mockNode("test");
assertEquals(node.getIdentifier(), factory.createValue(node).getString());
}
}

0 comments on commit e182e5e

Please sign in to comment.