1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-22 20:47:57 +01:00

Make Objects.requireNonNull() throw IllegalArgumentException

and not NullPointerException. Altough this differs from
java.util.Objects behavior, throwing an IllegalArgumentException
appears more sensible and makes it easier to catch it in Smack's
parsing function.
This commit is contained in:
Florian Schmaus 2019-06-11 12:32:42 +02:00
parent 9bb36fc63c
commit 818ee8a727
5 changed files with 33 additions and 11 deletions

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2015-2018 Florian Schmaus
* Copyright 2015-2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,9 +20,26 @@ import java.util.Collection;
public class Objects {
public static <T> T requireNonNull(T obj, String message) {
/**
* Checks that the specified object reference is not <code>null</code> and throws a customized
* {@link IllegalArgumentException} if it is.
* <p>
* Note that unlike <code>java.util.Objects</code>, this method throws an {@link IllegalArgumentException} instead
* of an {@link NullPointerException}.
* </p>
*
* @param <T> the type of the reference.
* @param obj the object reference to check for nullity.
* @param message detail message to be used in the event that a {@link IllegalArgumentException} is thrown.
* @return <code>obj</code> if not null.
* @throws IllegalArgumentException in case <code>obj</code> is <code>null</code>.
*/
public static <T> T requireNonNull(T obj, String message) throws IllegalArgumentException {
if (obj == null) {
throw new NullPointerException(message);
if (message == null) {
message = "Can not provide null argument";
}
throw new IllegalArgumentException(message);
}
return obj;
}

View file

@ -20,10 +20,11 @@ import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;
public class PresenceTest {
@ -59,9 +60,11 @@ public class PresenceTest {
assertXmlSimilar(control, presenceTypeSet.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}
@Test(expected = NullPointerException.class)
@Test
public void setNullPresenceTypeTest() {
getNewPresence().setType(null);
assertThrows(IllegalArgumentException.class, () ->
getNewPresence().setType(null)
);
}
@Test
@ -112,9 +115,11 @@ public class PresenceTest {
assertXmlSimilar(control, presence.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setIllegalPriorityTest() {
getNewPresence().setPriority(Integer.MIN_VALUE);
assertThrows(IllegalArgumentException.class, () ->
getNewPresence().setPriority(Integer.MIN_VALUE)
);
}
@Test

View file

@ -98,7 +98,7 @@ public class ReferenceTest extends SmackTestSuite {
@Test
public void typeArgumentNullTest() throws URISyntaxException {
assertThrows(NullPointerException.class, () ->
assertThrows(IllegalArgumentException.class, () ->
new ReferenceElement(1, 2, null, null, new URI("xmpp:test@test.test")));
}

View file

@ -32,7 +32,7 @@ import org.junit.Test;
*/
public class JingleContentTest extends SmackTestSuite {
@Test(expected = NullPointerException.class)
@Test(expected = IllegalArgumentException.class)
public void emptyBuilderThrowsTest() {
JingleContent.Builder builder = JingleContent.getBuilder();
builder.build();

View file

@ -41,7 +41,7 @@ public class JingleTest extends SmackTestSuite {
builder.build();
}
@Test(expected = NullPointerException.class)
@Test(expected = IllegalArgumentException.class)
public void onlySessionIdBuilderTest() {
String sessionId = "testSessionId";