11/*
2- * Copyright 2019 Google Inc.
2+ * Copyright 2025 Google Inc.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55 * in compliance with the License. You may obtain a copy of the License at
1818import static org .junit .Assert .*;
1919
2020import com .google .common .collect .Range ;
21+ import com .google .common .testing .EqualsTester ;
2122import org .junit .Test ;
2223import org .junit .runner .RunWith ;
2324import org .junit .runners .JUnit4 ;
@@ -35,63 +36,26 @@ public void testCreateWithValidInput() {
3536
3637 @ Test
3738 public void testCreateWithNegativeStartPositionThrows () {
38- try {
39- Replacement .create (-1 , 5 , "text" );
40- fail ("Expected IllegalArgumentException for negative startPosition" );
41- } catch (IllegalArgumentException e ) {
42- assertThat (e ).hasMessageThat ().contains ("startPosition must be non-negative" );
43- }
39+ assertThrows (IllegalArgumentException .class , () -> Replacement .create (-1 , 5 , "text" ));
4440 }
4541
4642 @ Test
4743 public void testCreateWithStartPositionAfterEndPositionThrows () {
48- try {
49- Replacement .create (10 , 5 , "text" );
50- fail ("Expected IllegalArgumentException for startPosition after endPosition" );
51- } catch (IllegalArgumentException e ) {
52- assertThat (e ).hasMessageThat ().contains ("startPosition cannot be after endPosition" );
53- }
44+ assertThrows (IllegalArgumentException .class , () -> Replacement .create (10 , 5 , "text" ));
5445 }
5546
5647 @ Test
57- public void testEqualsAndHashCodeWithEqualReplacements () {
58- Replacement a = Replacement .create (0 , 4 , "abc" );
59- Replacement b = Replacement .create (0 , 4 , "abc" );
60- assertThat (a ).isEqualTo (b );
61- assertThat (a .hashCode ()).isEqualTo (b .hashCode ());
62- }
63-
64- @ Test
65- public void testEqualsWithDifferentReplaceRange () {
66- Replacement a = Replacement .create (0 , 4 , "abc" );
67- Replacement b = Replacement .create (1 , 4 , "abc" );
68- assertThat (a ).isNotEqualTo (b );
69- }
70-
71- @ Test
72- public void testEqualsWithDifferentReplacementString () {
73- Replacement a = Replacement .create (0 , 4 , "abc" );
74- Replacement b = Replacement .create (0 , 4 , "def" );
75- assertThat (a ).isNotEqualTo (b );
76- }
48+ public void testEqualsAndHashCode () {
49+ Replacement replacement = Replacement .create (0 , 4 , "abc" );
50+ Replacement replacementCopy = Replacement .create (0 , 4 , "abc" );
51+ Replacement differentStart = Replacement .create (1 , 4 , "abc" );
52+ Replacement differentText = Replacement .create (0 , 4 , "def" );
7753
78- @ Test
79- public void testEqualsWithNullAndDifferentType () {
80- Replacement a = Replacement .create (0 , 4 , "abc" );
81- assertThat (a ).isNotEqualTo (null );
82- assertThat (a ).isNotEqualTo ("NotAReplacement" );
83- }
84-
85- @ Test
86- public void testGetReplaceRangeReturnsCorrectRange () {
87- Replacement replacement = Replacement .create (5 , 10 , "text" );
88- assertThat (replacement .getReplaceRange ()).isEqualTo (Range .closedOpen (5 , 10 ));
89- }
90-
91- @ Test
92- public void testGetReplacementStringReturnsCorrectString () {
93- Replacement replacement = Replacement .create (5 , 10 , "text" );
94- assertThat (replacement .getReplacementString ()).isEqualTo ("text" );
54+ new EqualsTester ()
55+ .addEqualityGroup (replacement , replacementCopy )
56+ .addEqualityGroup (differentStart )
57+ .addEqualityGroup (differentText )
58+ .testEquals ();
9559 }
9660
9761}
0 commit comments