Skip to content

Commit

Permalink
Switch to PrivateConstructorChecker for "testNotInstantiable" tests
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zinnatullin committed Aug 12, 2015
1 parent 2e44d56 commit 61a8cbe
Show file tree
Hide file tree
Showing 6 changed files with 422 additions and 61 deletions.
23 changes: 7 additions & 16 deletions src/test/java/rx/functions/ActionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

import static org.junit.Assert.*;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

import org.junit.Test;
import rx.util.PrivateConstructorChecker;

public class ActionsTest {

Expand Down Expand Up @@ -269,22 +269,13 @@ public void call(Object... args) {
assertEquals(i, value.get());
}
}

@Test
public void testNotInstantiable() {
try {
Constructor<?> c = Actions.class.getDeclaredConstructor();
c.setAccessible(true);
Object instance = c.newInstance();
fail("Could instantiate Actions! " + instance);
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
PrivateConstructorChecker
.forClass(Actions.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances!")
.check();
}
}
21 changes: 6 additions & 15 deletions src/test/java/rx/functions/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,20 @@

import static org.junit.Assert.*;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

import org.junit.Test;
import rx.util.PrivateConstructorChecker;

public class FunctionsTest {
@Test
public void testNotInstantiable() {
try {
Constructor<?> c = Functions.class.getDeclaredConstructor();
c.setAccessible(true);
Object instance = c.newInstance();
fail("Could instantiate Actions! " + instance);
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
PrivateConstructorChecker
.forClass(Functions.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances!")
.check();
}

@Test(expected = RuntimeException.class)
Expand Down
21 changes: 6 additions & 15 deletions src/test/java/rx/observers/ObserversTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,22 @@
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.lang.reflect.*;
import java.util.concurrent.atomic.*;

import org.junit.Test;

import rx.exceptions.*;
import rx.functions.*;
import rx.util.PrivateConstructorChecker;

public class ObserversTest {
@Test
public void testNotInstantiable() {
try {
Constructor<?> c = Observers.class.getDeclaredConstructor();
c.setAccessible(true);
Object instance = c.newInstance();
fail("Could instantiate Actions! " + instance);
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
PrivateConstructorChecker
.forClass(Observers.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances!")
.check();
}

@Test
Expand Down
21 changes: 6 additions & 15 deletions src/test/java/rx/observers/SubscribersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,22 @@
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.lang.reflect.*;
import java.util.concurrent.atomic.*;

import org.junit.Test;

import rx.exceptions.*;
import rx.functions.*;
import rx.util.PrivateConstructorChecker;

public class SubscribersTest {
@Test
public void testNotInstantiable() {
try {
Constructor<?> c = Subscribers.class.getDeclaredConstructor();
c.setAccessible(true);
Object instance = c.newInstance();
fail("Could instantiate Actions! " + instance);
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
PrivateConstructorChecker
.forClass(Subscribers.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances!")
.check();
}

@Test
Expand Down
243 changes: 243 additions & 0 deletions src/test/java/rx/util/PrivateConstructionCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package rx.util;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class PrivateConstructionCheckerTest {

@Test
public void builderShouldThrowExceptionIfNullWasPassedAsExpectedTypeOfException() {
try {
PrivateConstructorChecker
.forClass(Object.class)
.expectedTypeOfException(null);

fail();
} catch (IllegalArgumentException expected) {
assertEquals("expectedTypeOfException can not be null", expected.getMessage());
}
}

@Test
public void builderShouldThrowExceptionIfNullWasPassedAsExpectedExceptionMessage() {
try {
PrivateConstructorChecker
.forClass(Object.class)
.expectedExceptionMessage(null);

fail();
} catch (IllegalArgumentException expected) {
assertEquals("expectedExceptionMessage can not be null", expected.getMessage());
}
}

static class ClassWithoutDefaultConstructor {
private ClassWithoutDefaultConstructor(String someParam) {
}
}

@Test
public void shouldThrowExceptionIfClassHasNonDefaultConstructor() {
try {
PrivateConstructorChecker
.forClass(ClassWithoutDefaultConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals(
"Class has non-default constructor with some parameters",
expected.getMessage()
);
}
}

static class ClassWithPrivateConstructor {
private ClassWithPrivateConstructor() {
}
}

@Test
public void shouldAssertThatConstructorIsPrivateAndDoesNotThrowExceptions() {
PrivateConstructorChecker
.forClass(ClassWithPrivateConstructor.class)
.check();
}

static class ClassWithDefaultProtectedConstructor {
ClassWithDefaultProtectedConstructor() {
}
}

@Test
public void shouldThrowExceptionBecauseConstructorHasDefaultModifier() {
try {
PrivateConstructorChecker
.forClass(ClassWithDefaultProtectedConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor must be private", expected.getMessage());
}
}

static class ClassWithProtectedConstructor {
protected ClassWithProtectedConstructor() {
}
}

@Test
public void shouldThrowExceptionBecauseConstructorHasProtectedModifier() {
try {
PrivateConstructorChecker
.forClass(ClassWithProtectedConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor must be private", expected.getMessage());
}
}

static class ClassWithPublicConstructor {
public ClassWithPublicConstructor() {
}
}

@Test
public void shouldThrowExceptionBecauseConstructorHasPublicModifier() {
try {
PrivateConstructorChecker
.forClass(ClassWithPublicConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor must be private", expected.getMessage());
}
}

static class ClassWithConstructorThatThrowsException {
private ClassWithConstructorThatThrowsException() {
throw new IllegalStateException("test exception");
}
}

@Test
public void shouldCheckThatConstructorThrowsExceptionWithoutCheckingMessage() {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalStateException.class)
.check();
}

@Test
public void shouldCheckThatConstructorThrowsExceptionWithExpectedMessage() {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("test exception")
.check();
}

@Test
public void shouldCheckThatConstructorThrowsExceptionWithExpectedMessageButWithoutExpectedExceptionType() {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedExceptionMessage("test exception")
.check(); // without checking exception's type
}

@Test
public void shouldThrowExceptionBecauseTypeOfExpectedExceptionDoesNotMatch() {
try {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalArgumentException.class) // Incorrect type
.check();

fail();
} catch (IllegalStateException expected) {
assertEquals("Expected exception of type = class java.lang.IllegalArgumentException, " +
"but was exception of type = class java.lang.IllegalStateException",
expected.getMessage()
);
}
}

@Test
public void shouldThrowExceptionBecauseExpectedMessageDoesNotMatch() {
try {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalStateException.class) // Correct type
.expectedExceptionMessage("lol, not something that you've expected?") // Incorrect message
.check();

fail();
} catch (IllegalStateException expected) {
assertEquals("Expected exception message = 'lol, not something that you've expected?', " +
"but was = 'test exception'",
expected.getMessage()
);
}
}

@Test
public void shouldThrowExceptionBecauseConstructorThrownUnexpectedException() {
try {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.check(); // We don't expect exception, but it will be thrown

fail();
} catch (IllegalStateException expected) {
assertEquals("No exception was expected", expected.getMessage());
}
}

static class ClassWith2Constructors {
// This is good constructor for the checker
private ClassWith2Constructors() {

}

// This is bad constructor
private ClassWith2Constructors(String str) {

}
}

@Test
public void shouldThrowExceptionBecauseClassHasMoreThanOneConstructor() {
try {
PrivateConstructorChecker
.forClass(ClassWith2Constructors.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Class has more than one constructor", expected.getMessage());
}
}

static class ClassWithoutDeclaredConstructor {

}

@Test
public void shouldThrowExceptionBecauseClassDoesNotHaveDeclaredConstructors() {
try {
PrivateConstructorChecker
.forClass(ClassWithoutDeclaredConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor must be private", expected.getMessage());
}
}
}
Loading

0 comments on commit 61a8cbe

Please sign in to comment.