1616
1717package de .danielbechler .diff ;
1818
19+ import de .danielbechler .diff .mock .*;
1920import de .danielbechler .diff .path .*;
21+ import org .fest .assertions .api .*;
2022import org .testng .annotations .*;
2123
22- import static org .hamcrest .MatcherAssert .*;
24+ import static org .fest .assertions .api .Assertions .assertThat ;
25+ import static org .hamcrest .MatcherAssert .assertThat ;
2326import static org .hamcrest .core .Is .*;
2427import static org .testng .Assert .*;
2528
@@ -32,27 +35,7 @@ public class CircularReferenceDetectorTest
3235 public void setUp ()
3336 {
3437 circularReferenceDetector = new CircularReferenceDetector ();
35- }
36-
37- @ Test
38- public void testIsNew_returns_true_if_nothing_has_been_pushed () throws Exception
39- {
40- assertTrue (circularReferenceDetector .isNew ());
41- }
42-
43- @ Test
44- public void testIsNew_returns_false_if_instance_has_been_pushed () throws Exception
45- {
46- circularReferenceDetector .push ("foo" , PropertyPath .buildRootPath ());
47- assertFalse (circularReferenceDetector .isNew ());
48- }
49-
50- @ Test
51- public void testIsNew_returns_false_if_instance_has_been_pushed_but_later_removed () throws Exception
52- {
53- circularReferenceDetector .push ("foo" , PropertyPath .buildRootPath ());
54- circularReferenceDetector .remove ("foo" );
55- assertFalse (circularReferenceDetector .isNew ());
38+ circularReferenceDetector .setReferenceMatchingMode (CircularReferenceDetector .ReferenceMatchingMode .EQUALITY_OPERATOR );
5639 }
5740
5841 @ Test
@@ -69,14 +52,23 @@ public void testPush_adds_unknown_object_to_stack() throws Exception
6952 assertThat (circularReferenceDetector .size (), is (1 ));
7053 }
7154
72- @ Test ( expectedExceptions = CircularReferenceDetector . CircularReferenceException . class )
55+ @ Test
7356 public void testPush_throws_CircularReferenceException_on_known_object () throws Exception
7457 {
7558 circularReferenceDetector .push ("foo" , PropertyPath .buildRootPath ());
76- circularReferenceDetector .push ("foo" , PropertyPath .createBuilder ()
77- .withRoot ()
78- .withPropertyName ("test" )
79- .build ());
59+ try
60+ {
61+ circularReferenceDetector .push ("foo" , PropertyPath .createBuilder ()
62+ .withRoot ()
63+ .withPropertyName ("test" )
64+ .build ());
65+ Assertions .fail ("Expected CircularReferenceException wasn't thrown." );
66+ }
67+ catch (CircularReferenceDetector .CircularReferenceException e )
68+ {
69+ final PropertyPath propertyPath = e .getPropertyPath ();
70+ assertThat (propertyPath ).isEqualTo (PropertyPath .buildRootPath ());
71+ }
8072 }
8173
8274 @ Test
@@ -123,4 +115,29 @@ public void testKnows_returns_false_if_instance_has_been_pushed_but_later_remove
123115 circularReferenceDetector .remove ("foo" );
124116 assertFalse (circularReferenceDetector .knows ("foo" ));
125117 }
118+
119+ @ Test
120+ public void knowsEqualObject_whenReferenceMatchingModeIsSetToEqualsMethod ()
121+ {
122+ circularReferenceDetector .setReferenceMatchingMode (CircularReferenceDetector .ReferenceMatchingMode .EQUALS_METHOD );
123+ circularReferenceDetector .push (new ObjectWithString ("foo" ), PropertyPath .buildRootPath ());
124+ assertThat (circularReferenceDetector .knows (new ObjectWithString ("foo" ))).isTrue ();
125+ }
126+
127+ @ Test
128+ public void removesEqualObject_whenReferenceMatchingModeIsSetToEqualsMethod ()
129+ {
130+ circularReferenceDetector .setReferenceMatchingMode (CircularReferenceDetector .ReferenceMatchingMode .EQUALS_METHOD );
131+ circularReferenceDetector .push (new ObjectWithString ("foo" ), PropertyPath .buildRootPath ());
132+ circularReferenceDetector .remove (new ObjectWithString ("foo" ));
133+ assertThat (circularReferenceDetector .size ()).isEqualTo (0 );
134+ }
135+
136+ @ Test (expectedExceptions = CircularReferenceDetector .CircularReferenceException .class )
137+ public void throwsException_onAttemptToPushEqualObject_whenReferenceMatchingModeIsSetToEqualsMethod ()
138+ {
139+ circularReferenceDetector .setReferenceMatchingMode (CircularReferenceDetector .ReferenceMatchingMode .EQUALS_METHOD );
140+ circularReferenceDetector .push (new ObjectWithString ("foo" ), PropertyPath .buildRootPath ());
141+ circularReferenceDetector .push (new ObjectWithString ("foo" ), PropertyPath .buildRootPath ());
142+ }
126143}
0 commit comments