-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from hprange/bugfix/validateForInsertOnCanBeSa…
…vedMatcher Invoke validateForInsert method on canBeSaved matcher
- Loading branch information
Showing
2 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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(); | ||
|