Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MockChannelListener with mocking object and improve testing logic #462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package org.apache.cayenne.access;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;

import org.apache.cayenne.DataChannel;
import org.apache.cayenne.DataChannelListener;
import org.apache.cayenne.ObjectContext;
Expand All @@ -32,169 +35,153 @@
import org.apache.cayenne.unit.di.server.UseServerRuntime;
import org.apache.cayenne.util.EventUtil;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.mockito.Mockito;

/**
* Tests that DataContext sends DataChannel events.
*/
@UseServerRuntime(CayenneProjects.TESTMAP_PROJECT)
public class DataContextDataChannelEventsIT extends ServerCaseContextsSync {

@Inject
private DataContext context;

@Inject
private DataContext peer;

@Inject
private ServerRuntime runtime;

@Test
public void testCommitEvent() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();

final MockChannelListener listener = new MockChannelListener();
EventUtil.listenForChannelEvents(context, listener);

a.setArtistName("Y");
context.commitChanges();

new ParallelTestContainer() {

@Override
protected void assertResult() throws Exception {
assertTrue(listener.graphCommitted);
assertFalse(listener.graphChanged);
assertFalse(listener.graphRolledBack);
}
}.runTest(10000);

}

@Test
public void testRollbackEvent() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();

final MockChannelListener listener = new MockChannelListener();
EventUtil.listenForChannelEvents(context, listener);

a.setArtistName("Y");
context.rollbackChanges();

new ParallelTestContainer() {

@Override
protected void assertResult() throws Exception {
assertFalse(listener.graphCommitted);
assertFalse(listener.graphChanged);
assertTrue(listener.graphRolledBack);
}
}.runTest(10000);
}

@Test
public void testChangeEventOnChildChange() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();

final MockChannelListener listener = new MockChannelListener();
EventUtil.listenForChannelEvents(context, listener);
@Inject
private DataContext context;

ObjectContext childContext = runtime.newContext(context);
@Inject
private DataContext peer;

Artist a1 = childContext.localObject(a);
@Inject
private ServerRuntime runtime;

a1.setArtistName("Y");
childContext.commitChangesToParent();
@Test
public void testCommitEvent() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();

// Construct mock object
final DataChannelListener listener = mock(DataChannelListener.class);
EventUtil.listenForChannelEvents(context, listener);

a.setArtistName("Y");
context.commitChanges();

new ParallelTestContainer() {
new ParallelTestContainer() {

@Override
protected void assertResult() throws Exception {
assertFalse(listener.graphCommitted);
assertTrue(listener.graphChanged);
assertFalse(listener.graphRolledBack);
}
}.runTest(10000);
}
@Override
protected void assertResult() throws Exception {
Mockito.verify(listener, Mockito.atLeastOnce()).graphFlushed(any(GraphEvent.class));
Mockito.verify(listener, Mockito.never()).graphChanged(any(GraphEvent.class));
Mockito.verify(listener, Mockito.never()).graphRolledback(any(GraphEvent.class));
}
}.runTest(10000);

@Test
public void testChangeEventOnPeerChange() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();
}

final MockChannelListener listener = new MockChannelListener();
EventUtil.listenForChannelEvents(context, listener);
@Test
public void testRollbackEvent() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();

Artist a1 = peer.localObject(a);
// Construct mock object
final DataChannelListener listener = mock(DataChannelListener.class);
EventUtil.listenForChannelEvents(context, listener);

a1.setArtistName("Y");
peer.commitChangesToParent();
a.setArtistName("Y");
context.rollbackChanges();

new ParallelTestContainer() {
new ParallelTestContainer() {

@Override
protected void assertResult() throws Exception {
assertFalse(listener.graphCommitted);
assertTrue(listener.graphChanged);
assertFalse(listener.graphRolledBack);
}
}.runTest(10000);
}
@Override
protected void assertResult() throws Exception {
Mockito.verify(listener, Mockito.never()).graphFlushed(any(GraphEvent.class));
Mockito.verify(listener, Mockito.never()).graphChanged(any(GraphEvent.class));
Mockito.verify(listener, Mockito.atLeastOnce()).graphRolledback(any(GraphEvent.class));
}
}.runTest(10000);
}

@Test
public void testChangeEventOnPeerChangeSecondNestingLevel() throws Exception {
ObjectContext childPeer1 = runtime.newContext(context);
@Test
public void testChangeEventOnChildChange() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();

Artist a = childPeer1.newObject(Artist.class);
a.setArtistName("X");
childPeer1.commitChanges();
// Construct mock object
final DataChannelListener listener = mock(DataChannelListener.class);
EventUtil.listenForChannelEvents(context, listener);

final MockChannelListener listener = new MockChannelListener();
EventUtil.listenForChannelEvents((DataChannel) childPeer1, listener);
ObjectContext childContext = runtime.newContext(context);

ObjectContext childPeer2 = runtime.newContext(context);
Artist a1 = childContext.localObject(a);

Artist a1 = childPeer2.localObject(a);
a1.setArtistName("Y");
childContext.commitChangesToParent();

a1.setArtistName("Y");
childPeer2.commitChangesToParent();
new ParallelTestContainer() {

new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
Mockito.verify(listener, Mockito.never()).graphFlushed(any(GraphEvent.class));
Mockito.verify(listener, Mockito.atLeastOnce()).graphChanged(any(GraphEvent.class));
Mockito.verify(listener, Mockito.never()).graphRolledback(any(GraphEvent.class));
}
}.runTest(10000);
}

@Override
protected void assertResult() throws Exception {
assertFalse(listener.graphCommitted);
assertTrue(listener.graphChanged);
assertFalse(listener.graphRolledBack);
}
}.runTest(10000);
}
@Test
public void testChangeEventOnPeerChange() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
context.commitChanges();

class MockChannelListener implements DataChannelListener {
// Construct mock object
final DataChannelListener listener = mock(DataChannelListener.class);
EventUtil.listenForChannelEvents(context, listener);

boolean graphChanged;
boolean graphCommitted;
boolean graphRolledBack;
Artist a1 = peer.localObject(a);

public void graphChanged(GraphEvent event) {
graphChanged = true;
}
a1.setArtistName("Y");
peer.commitChangesToParent();

new ParallelTestContainer() {

public void graphFlushed(GraphEvent event) {
graphCommitted = true;
}
@Override
protected void assertResult() throws Exception {
Mockito.verify(listener, Mockito.never()).graphFlushed(any(GraphEvent.class));
Mockito.verify(listener, Mockito.atLeastOnce()).graphChanged(any(GraphEvent.class));
Mockito.verify(listener, Mockito.never()).graphRolledback(any(GraphEvent.class));
}
}.runTest(10000);
}

@Test
public void testChangeEventOnPeerChangeSecondNestingLevel() throws Exception {
ObjectContext childPeer1 = runtime.newContext(context);

Artist a = childPeer1.newObject(Artist.class);
a.setArtistName("X");
childPeer1.commitChanges();

// Construct mock object
final DataChannelListener listener = mock(DataChannelListener.class);
EventUtil.listenForChannelEvents((DataChannel) childPeer1, listener);

ObjectContext childPeer2 = runtime.newContext(context);

Artist a1 = childPeer2.localObject(a);

a1.setArtistName("Y");
childPeer2.commitChangesToParent();

public void graphRolledback(GraphEvent event) {
graphRolledBack = true;
}
}
new ParallelTestContainer() {

@Override
protected void assertResult() throws Exception {
Mockito.verify(listener, Mockito.never()).graphFlushed(any(GraphEvent.class));
Mockito.verify(listener, Mockito.atLeastOnce()).graphChanged(any(GraphEvent.class));
Mockito.verify(listener, Mockito.never()).graphRolledback(any(GraphEvent.class));
}
}.runTest(10000);
}
}