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

AdaptedList tests + bugfix (attempt 3) #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -137,12 +137,14 @@ public boolean remove(Object value) {
}
return false;
} else {
return removeWithoutBackWardAdapter(value);
return removeWithBackwardAdapter(value);
}
}

@SuppressWarnings("unchecked")
protected boolean removeWithoutBackWardAdapter(Object value) {
protected boolean removeWithBackwardAdapter(Object value) {
if(backwardAdapter==null)
throw new UnsupportedOperationException("Cannot perform operation with a null backward Adapter.");
try {
currentlyCausingChange = true;
if (collection.remove(backwardAdapter.adapt((V)value))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nativelibs4java.jalico;

import org.junit.After;
import org.junit.Before;

public abstract class AbstractAdaptedCollectionBiDiTest extends
AbstractAdaptedCollectionTest {

@Before
public void setUp() throws Exception {
super.setUp();
}

@After
public void tearDown() throws Exception {
}

@Override
protected AdaptedCollection<Integer, String> constructSubject() {
return new AdaptedCollection<Integer,String>(mockedDelegate, FORWARD, BACKWARD);
}

}//end AbstractBiDiAdaptedCollectionTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nativelibs4java.jalico;

import org.junit.After;
import org.junit.Before;

public abstract class AbstractAdaptedCollectionForwardTest extends
AbstractAdaptedCollectionTest {

@Before
public void setUp() throws Exception {
super.setUp();
}

@After
public void tearDown() throws Exception {
}

@Override
protected AdaptedCollection<Integer, String> constructSubject() {
return new AdaptedCollection<Integer,String>(mockedDelegate,FORWARD);
}
}//end AbstractAdaptedCollectionForwardTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.nativelibs4java.jalico;

import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.mockito.Mock;


public abstract class AbstractAdaptedCollectionTest {
////FIELDS
protected AdaptedCollection<Integer,String> subject;
protected @Mock CollectionListener<String> mockedListener;
protected @Mock Collection<Integer> mockedDelegate;
protected Collection<Integer> iteratorSource;

//// ADAPTERS
protected static final Adapter<Integer,String> FORWARD = new Adapter<Integer,String>(){
public String adapt(Integer value) {
return value.toString();
}};
protected static final Adapter<String,Integer> BACKWARD = new Adapter<String,Integer>(){
public Integer adapt(String value) {
return Integer.parseInt(value);
}};

//// PREP
@Before
public void setUp() throws Exception {
subject = constructSubject();
subject . addCollectionListener (mockedListener);
//Going with traditional way instead of over-engineering with mocks.
iteratorSource = new ArrayList<Integer>(Arrays.asList(0,1,2));
when(mockedDelegate.iterator()).thenReturn(iteratorSource.iterator());
}//end setUp()

protected abstract AdaptedCollection<Integer,String> constructSubject();

}//end AbstractAdaptedCollectionTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.nativelibs4java.jalico;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.runners.MockitoJUnitRunner;

import com.nativelibs4java.jalico.CollectionEvent.EventType;

@RunWith(MockitoJUnitRunner.class)
public class AdaptedCollectionBiDiIteratorTest extends
AbstractAdaptedCollectionBiDiTest {
protected Iterator<String> subjectIterator;

@Before
public void setUp() throws Exception {
super.setUp();
subjectIterator = subject.iterator();
}

@Test
public void testHasNextTrue() {
assertTrue(subjectIterator.hasNext());
verifyZeroInteractions(mockedListener);
subjectIterator.next();
subjectIterator.next();
assertTrue(subjectIterator.hasNext());
}

@Test
public void testHasNextFalse() {
subjectIterator.next();
subjectIterator.next();
subjectIterator.next();
assertFalse(subjectIterator.hasNext());
verifyZeroInteractions(mockedListener);
}

@Test
public void testNext(){
assertEquals("0",subjectIterator.next());
assertEquals("1",subjectIterator.next());
assertEquals("2",subjectIterator.next());
verifyZeroInteractions(mockedListener);
}

@Test
public void testNextFail(){
assertEquals("0",subjectIterator.next());
assertEquals("1",subjectIterator.next());
assertEquals("2",subjectIterator.next());
}

@Test
public void testRemove(){
when(mockedDelegate.remove(0)).thenReturn(true);
subjectIterator .next();
subjectIterator .remove();
assertEquals(2,iteratorSource.size());
ArgumentCaptor<CollectionEvent> arg
= ArgumentCaptor.forClass(CollectionEvent.class);
verify(mockedListener).collectionChanged(arg.capture());
CollectionEvent evt = arg.getValue();
assertEquals(-1,evt.getFirstIndex());
assertEquals(-1,evt.getLastIndex());
assertTrue (evt.getElements().contains("0"));
assertEquals(1,evt.getElements().size());
assertEquals(subject,evt.getSource());
assertEquals(EventType.REMOVED,evt.getType());
}

@Test(expected=IllegalStateException.class)
public void testRemoveIllegalState(){
subjectIterator.remove();
}
}//end AdaptedCollectionIteratorTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.nativelibs4java.jalico;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.runners.MockitoJUnitRunner;

import com.nativelibs4java.jalico.CollectionEvent.EventType;

@RunWith(MockitoJUnitRunner.class)
public class AdaptedCollectionBiDiTest extends AbstractAdaptedCollectionTest {
@Test
public void testSize() {
when (mockedDelegate.size()).thenReturn(5);
assertEquals(5,subject .size());
verifyZeroInteractions(mockedListener);
}//end testSize()

@Test
public void testIsEmpty() {
when (mockedDelegate .isEmpty()).thenReturn(true);
assertTrue (subject .isEmpty());
verify (mockedDelegate).isEmpty();
when (mockedDelegate .isEmpty()).thenReturn(false);
assertFalse(subject .isEmpty());
verify (mockedDelegate, times(2)).isEmpty();
verifyZeroInteractions(mockedListener);
}//end testIsEmpty()

private CollectionEvent<String> expectEvent(){
ArgumentCaptor<CollectionEvent> arg
= ArgumentCaptor.forClass(CollectionEvent.class);
verify(mockedListener).collectionChanged(arg.capture());
return arg.getValue();
}

@Test
public void testClear() {
subject .clear();
verify (mockedDelegate).clear();
final CollectionEvent<String> evt = expectEvent();
assertEquals(0,evt.getFirstIndex());
assertEquals(2,evt.getLastIndex());
assertEquals(EventType.REMOVED,evt.getType());
assertEquals(subject,evt.getSource());
final Collection<String> elements = evt.getElements();
assertNotNull(elements);
assertEquals (3,elements.size());
assertTrue (elements.contains("0"));
assertTrue (elements.contains("1"));
assertTrue (elements.contains("2"));
}//end testClear()

@Test
public void testAdaptedCollectionCollectionOfUAdapterOfUVAdapterOfVU() {
assertNotNull(subject);
}

@Test
public void testGetForwardAdapter() {
assertEquals(subject.getForwardAdapter(),FORWARD);
}

@Test
public void testGetBackwardAdapter() {
assertEquals(subject.getBackwardAdapter(),BACKWARD);
}

@Test
public void testIterator() {
assertNotNull(subject.iterator());
}

@Test
public void testAddV() {
when (mockedDelegate.add(5)).thenReturn(true);
assertTrue(subject.add("5"));
verify (mockedDelegate).add(5);
final CollectionEvent<String> evt = expectEvent();
assertEquals (-1,evt.getFirstIndex());
assertEquals (-1,evt.getLastIndex());
assertEquals (EventType.ADDED,evt.getType());
assertEquals (subject,evt.getSource());
Collection<String> elements = evt.getElements();
assertEquals(1,elements.size());
assertTrue (elements.contains("5"));
}//end testAddV()

@Test
public void testSuccessfulRemoveObject() {
when (mockedDelegate.remove(5)).thenReturn(true);
assertTrue(subject.remove("5"));
verify (mockedDelegate).remove(5);
final CollectionEvent<String> evt = expectEvent();
assertEquals (-1,evt.getFirstIndex());
assertEquals (-1,evt.getLastIndex());
assertEquals (EventType.REMOVED,evt.getType());
assertEquals (subject,evt.getSource());
Collection<String> elements = evt.getElements();
assertEquals(1,elements.size());
assertTrue (elements.contains("5"));
}//end testSuccessfulRemoveObject()

@Test
public void testFailedRemoveObject() {
when (mockedDelegate.remove(5)).thenReturn(false);
assertFalse (subject.remove("5"));
verify (mockedDelegate).remove(5);
verifyZeroInteractions(mockedListener);
}//end testFailedRemoveObject()

@Test
public void testRemoveWithoutBackWardAdapter() {
when (mockedDelegate .remove(0)).thenReturn(true);
subject.removeWithBackwardAdapter("0");
verify (mockedDelegate).remove(0);
}//end testRemoveWithoutBackWardAdapter()

@Test
public void testContainsObject() {
when (mockedDelegate.contains(0)).thenReturn(true);
when (mockedDelegate.contains(7)).thenReturn(false);
assertTrue (subject.contains("0"));
assertFalse(subject.contains("7"));
verifyZeroInteractions(mockedListener);
}//end testContainsObject()

@Test
public void testRemoveCollectionListener() {
subject.removeCollectionListener(mockedListener);
subject.add("9");
verifyZeroInteractions(mockedListener);
}//end testRemoveCollectionListener()

@Override
protected AdaptedCollection<Integer, String> constructSubject() {
return new AdaptedCollection<Integer,String>(mockedDelegate, FORWARD, BACKWARD);
}
}//end AdaptedCollectionTest
Loading