Skip to content

Commit

Permalink
Merge pull request #50 from hprange/bugfix/validateForInsertOnCanBeSa…
Browse files Browse the repository at this point in the history
…vedMatcher

Invoke validateForInsert method on canBeSaved matcher
  • Loading branch information
hprange authored Nov 5, 2017
2 parents ae16b68 + bd38a6c commit 9ceb9ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/wounit/matchers/CanBeSavedMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.wounit.matchers;

import static er.extensions.eof.ERXEOControlUtilities.isNewObject;

import org.hamcrest.Description;

import com.webobjects.eocontrol.EOEnterpriseObject;

/**
* Tests if the <code>EOEnterpriseObject</code> can be saved.
*
*
* @author <a href="mailto:[email protected]">Henrique Prange</a>
* @since 1.0
* @param <T>
Expand Down Expand Up @@ -73,6 +75,10 @@ public void describeTo(Description description) {

@Override
protected void matchesWithPossibleException(T eo) {
eo.validateForSave();
if (isNewObject(eo)) {
eo.validateForInsert();
} else {
eo.validateForUpdate();
}
}
}
43 changes: 37 additions & 6 deletions src/test/java/com/wounit/matchers/TestCanBeSavedMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import com.webobjects.eocontrol.EOEditingContext;
import com.webobjects.eocontrol.EOEnterpriseObject;
import com.webobjects.eocontrol.EOGlobalID;
import com.webobjects.foundation.NSValidation;

/**
* @author <a href="mailto:[email protected]">Henrique Prange</a>
*/
@RunWith(MockitoJUnitRunner.class)
public class TestCanBeSavedMatcher {
@Mock
private EOEditingContext editingContext;

@Mock
private EOGlobalID globalId;

private CanBeSavedMatcher<EOEnterpriseObject> matcher;

private StringDescription mockDescription;
Expand All @@ -44,7 +52,7 @@ public class TestCanBeSavedMatcher {

@Test
public void descriptionForCanBeSavedFailure() throws Exception {
Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForSave();
Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForUpdate();

matcher.matchesSafely(mockObject);
matcher.describeTo(mockDescription);
Expand All @@ -63,7 +71,7 @@ public void descriptionForCanBeSavedSuccess() throws Exception {
public void descriptionForCanBeSavedWithMessageFailure() throws Exception {
matcher = new CanBeSavedMatcher<EOEnterpriseObject>("An expected error");

Mockito.doThrow(new NSValidation.ValidationException("An unexpectedError")).when(mockObject).validateForSave();
Mockito.doThrow(new NSValidation.ValidationException("An unexpectedError")).when(mockObject).validateForUpdate();

matcher.matchesSafely(mockObject);
matcher.describeTo(mockDescription);
Expand All @@ -82,7 +90,16 @@ public void descriptionForCanBeSavedWithMessageSuccess() throws Exception {

@Test
public void matchesCanBeSaved() throws Exception {
Mockito.doNothing().when(mockObject).validateForSave();
Mockito.doNothing().when(mockObject).validateForUpdate();

boolean result = matcher.matchesSafely(mockObject);

assertThat(result, is(true));
}

@Test
public void matchesCanBeSavedIfExistingObjectAndExceptionOnInsertion() throws Exception {
Mockito.doThrow(new NSValidation.ValidationException("insertion error")).when(mockObject).validateForInsert();

boolean result = matcher.matchesSafely(mockObject);

Expand All @@ -91,7 +108,17 @@ public void matchesCanBeSaved() throws Exception {

@Test
public void matchesCannotBeSaved() throws Exception {
Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForSave();
Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForUpdate();

boolean result = matcher.matchesSafely(mockObject);

assertThat(result, is(false));
}

@Test
public void matchesCannotBeSavedIfNewObjectAndExceptionOnInsertion() throws Exception {
Mockito.when(globalId.isTemporary()).thenReturn(true);
Mockito.doThrow(new NSValidation.ValidationException("insertion error")).when(mockObject).validateForInsert();

boolean result = matcher.matchesSafely(mockObject);

Expand All @@ -102,7 +129,7 @@ public void matchesCannotBeSaved() throws Exception {
public void matchesCannotBeSavedWithMatchingMessage() throws Exception {
matcher = new CanBeSavedMatcher<EOEnterpriseObject>("error");

Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForSave();
Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForUpdate();

boolean result = matcher.matchesSafely(mockObject);

Expand All @@ -113,7 +140,7 @@ public void matchesCannotBeSavedWithMatchingMessage() throws Exception {
public void matchesCannotBeSavedWithNoMatchingMessage() throws Exception {
matcher = new CanBeSavedMatcher<EOEnterpriseObject>("another error");

Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForSave();
Mockito.doThrow(new NSValidation.ValidationException("error")).when(mockObject).validateForUpdate();

boolean result = matcher.matchesSafely(mockObject);

Expand All @@ -122,6 +149,10 @@ public void matchesCannotBeSavedWithNoMatchingMessage() throws Exception {

@Before
public void setup() {
Mockito.when(globalId.isTemporary()).thenReturn(false);
Mockito.when(editingContext.globalIDForObject(mockObject)).thenReturn(globalId);
Mockito.when(mockObject.editingContext()).thenReturn(editingContext);

matcher = new CanBeSavedMatcher<EOEnterpriseObject>();

mockDescription = new StringDescription();
Expand Down

0 comments on commit 9ceb9ee

Please sign in to comment.