Skip to content

Commit

Permalink
Merge pull request #1771 from cpovirk/primcon
Browse files Browse the repository at this point in the history
Migrate off constructors of boxed primitive types.
  • Loading branch information
kcooney committed Feb 22, 2024
2 parents 16228f3 + 62e16b4 commit 28fa2ca
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/test/java/junit/samples/SimpleTest.java
Expand Up @@ -60,7 +60,7 @@ public void testDivideByZero() {
public void testEquals() {
assertEquals(12, 12);
assertEquals(12L, 12L);
assertEquals(new Long(12), new Long(12));
assertEquals(Long.valueOf(12), Long.valueOf(12));

assertEquals("Size", 12, 13);
assertEquals("Capacity", 12.0, 11.99, 0.0);
Expand Down
33 changes: 28 additions & 5 deletions src/test/java/junit/tests/framework/AssertTest.java
Expand Up @@ -140,19 +140,19 @@ public void testAssertSame() {
Object o = new Object();
assertSame(o, o);
try {
assertSame(new Integer(1), new Integer(1));
assertSame(new MyInt(1), new MyInt(1));
} catch (AssertionFailedError e) {
return;
}
fail();
}

public void testAssertNotSame() {
assertNotSame(new Integer(1), null);
assertNotSame(null, new Integer(1));
assertNotSame(new Integer(1), new Integer(1));
assertNotSame(new MyInt(1), null);
assertNotSame(null, new MyInt(1));
assertNotSame(new MyInt(1), new MyInt(1));
try {
Integer obj = new Integer(1);
MyInt obj = new MyInt(1);
assertNotSame(obj, obj);
} catch (AssertionFailedError e) {
return;
Expand All @@ -168,4 +168,27 @@ public void testAssertNotSameFailsNull() {
}
fail();
}

private static final class MyInt {
private final int value;

MyInt(int value) {
this.value = value;
}

@Override
public boolean equals(Object obj) {
return obj instanceof MyInt && value == ((MyInt) obj).value;
}

@Override
public int hashCode() {
return value;
}

@Override
public String toString() {
return Integer.toString(value);
}
}
}
Expand Up @@ -106,7 +106,7 @@ public void isNotEqualToTestWithDifferentParameters() {
public void isNotEqualToObjectWithDifferentClass() {
TestWithParameters test = new TestWithParameters(DUMMY_NAME,
DUMMY_TEST_CLASS, DUMMY_PARAMETERS);
assertNotEquals(test, new Integer(3));
assertNotEquals(test, Integer.valueOf(3));
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/junit/samples/ListTest.java
Expand Up @@ -85,7 +85,7 @@ public void removeAll() {

@Test
public void removeElement() {
fFull.remove(new Integer(3));
fFull.remove(Integer.valueOf(3));
assertTrue(!fFull.contains(3));
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/junit/samples/SimpleTest.java
Expand Up @@ -38,7 +38,7 @@ public void divideByZero() {
public void testEquals() {
assertEquals(12, 12);
assertEquals(12L, 12L);
assertEquals(new Long(12), new Long(12));
assertEquals(Long.valueOf(12), Long.valueOf(12));

assertEquals("Size", 12, 13);
assertEquals("Capacity", 12.0, 11.99, 0.0);
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Expand Up @@ -217,7 +217,7 @@ public void oneDimensionalBooleanArraysAreNotEqual() {

@Test(expected = AssertionError.class)
public void IntegerDoesNotEqualLong() {
assertEquals(new Integer(1), new Long(1));
assertEquals(Integer.valueOf(1), Long.valueOf(1));
}

@Test
Expand Down Expand Up @@ -678,7 +678,7 @@ public void implicitTypecastEquality() {
@Test
public void errorMessageDistinguishesDifferentValuesWithSameToString() {
try {
assertEquals("4", new Integer(4));
assertEquals("4", Integer.valueOf(4));
} catch (AssertionError e) {
assertEquals("expected: java.lang.String<4> but was: java.lang.Integer<4>", e.getMessage());
return;
Expand Down Expand Up @@ -788,8 +788,8 @@ public void objectsWithDifferentReferencesAreNotEqual() {

@Test
public void assertNotEqualsIncludesCorrectMessage() {
Integer value1 = new Integer(1);
Integer value2 = new Integer(1);
Integer value1 = 1;
Integer value2 = 1;
String message = "The values should be different";

try {
Expand All @@ -804,8 +804,8 @@ public void assertNotEqualsIncludesCorrectMessage() {

@Test
public void assertNotEqualsIncludesTheValueBeingTested() {
Integer value1 = new Integer(1);
Integer value2 = new Integer(1);
Integer value1 = 1;
Integer value2 = 1;

try {
assertNotEquals(value1, value2);
Expand Down
Expand Up @@ -20,7 +20,7 @@ public void equalsIsCorrect() {
assertFalse(childless.equals(namedB));
assertEquals(childless, twoKids);
assertEquals(twoKids, anotherTwoKids);
assertFalse(twoKids.equals(new Integer(5)));
assertFalse(twoKids.equals(Integer.valueOf(5)));
}

@Test
Expand Down
Expand Up @@ -9,7 +9,7 @@
public class TestDescriptionTest {
@Test
public void equalsIsFalseForNonTestDescription() {
assertFalse(Description.createTestDescription(getClass(), "a").equals(new Integer(5)));
assertFalse(Description.createTestDescription(getClass(), "a").equals(Integer.valueOf(5)));
}

@Test
Expand Down
Expand Up @@ -206,4 +206,4 @@ public void dataPointsCollectionShouldBeRecognizedIgnoringStrangeTypes() throws

assertEquals(1, assignments.size());
}
}
}

0 comments on commit 28fa2ca

Please sign in to comment.