mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-25 21:42:07 +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:
parent
9bb36fc63c
commit
818ee8a727
5 changed files with 33 additions and 11 deletions
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2015-2018 Florian Schmaus
|
* Copyright 2015-2019 Florian Schmaus
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 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) {
|
if (obj == null) {
|
||||||
throw new NullPointerException(message);
|
if (message == null) {
|
||||||
|
message = "Can not provide null argument";
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException(message);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,11 @@ import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
public class PresenceTest {
|
public class PresenceTest {
|
||||||
|
@ -59,9 +60,11 @@ public class PresenceTest {
|
||||||
assertXmlSimilar(control, presenceTypeSet.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
|
assertXmlSimilar(control, presenceTypeSet.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test
|
||||||
public void setNullPresenceTypeTest() {
|
public void setNullPresenceTypeTest() {
|
||||||
getNewPresence().setType(null);
|
assertThrows(IllegalArgumentException.class, () ->
|
||||||
|
getNewPresence().setType(null)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -112,9 +115,11 @@ public class PresenceTest {
|
||||||
assertXmlSimilar(control, presence.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
|
assertXmlSimilar(control, presence.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test
|
||||||
public void setIllegalPriorityTest() {
|
public void setIllegalPriorityTest() {
|
||||||
getNewPresence().setPriority(Integer.MIN_VALUE);
|
assertThrows(IllegalArgumentException.class, () ->
|
||||||
|
getNewPresence().setPriority(Integer.MIN_VALUE)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -98,7 +98,7 @@ public class ReferenceTest extends SmackTestSuite {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void typeArgumentNullTest() throws URISyntaxException {
|
public void typeArgumentNullTest() throws URISyntaxException {
|
||||||
assertThrows(NullPointerException.class, () ->
|
assertThrows(IllegalArgumentException.class, () ->
|
||||||
new ReferenceElement(1, 2, null, null, new URI("xmpp:test@test.test")));
|
new ReferenceElement(1, 2, null, null, new URI("xmpp:test@test.test")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ import org.junit.Test;
|
||||||
*/
|
*/
|
||||||
public class JingleContentTest extends SmackTestSuite {
|
public class JingleContentTest extends SmackTestSuite {
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void emptyBuilderThrowsTest() {
|
public void emptyBuilderThrowsTest() {
|
||||||
JingleContent.Builder builder = JingleContent.getBuilder();
|
JingleContent.Builder builder = JingleContent.getBuilder();
|
||||||
builder.build();
|
builder.build();
|
||||||
|
|
|
@ -41,7 +41,7 @@ public class JingleTest extends SmackTestSuite {
|
||||||
builder.build();
|
builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void onlySessionIdBuilderTest() {
|
public void onlySessionIdBuilderTest() {
|
||||||
String sessionId = "testSessionId";
|
String sessionId = "testSessionId";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue