Skip to content

Commit 5c74757

Browse files
vanniktechakarnokd
authored andcommitted
1.x: Test all private Constructors (#4125)
* 1.x: Test all private Constructors * Revert exception type and message back
1 parent 66815b4 commit 5c74757

24 files changed

+214
-106
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
testCompile 'junit:junit:4.12'
2828
testCompile 'org.mockito:mockito-core:1.10.19'
2929
testCompile 'com.google.guava:guava:19.0'
30+
testCompile 'com.pushtorefresh.java-private-constructor-checker:checker:1.2.0'
3031

3132
perfCompile 'org.openjdk.jmh:jmh-core:1.11.3'
3233
perfCompile 'org.openjdk.jmh:jmh-generator-annprocess:1.11.3'

src/main/java/rx/internal/util/unsafe/Pow2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ public static int roundToPowerOfTwo(final int value) {
4141
public static boolean isPowerOfTwo(final int value) {
4242
return (value & (value - 1)) == 0;
4343
}
44-
}
44+
}

src/main/java/rx/internal/util/unsafe/UnsafeAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ public static long addressOf(Class<?> clazz, String fieldName) {
117117
throw ie;
118118
}
119119
}
120-
}
120+
}

src/test/java/rx/EventStreamTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx;
17+
18+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
19+
20+
import org.junit.Test;
21+
22+
public class EventStreamTest {
23+
@Test
24+
public void constructorShouldBePrivate() {
25+
PrivateConstructorChecker.forClass(EventStream.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
26+
}
27+
}

src/test/java/rx/exceptions/ExceptionsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static org.junit.Assert.assertTrue;
1919
import static org.junit.Assert.fail;
2020

21+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
22+
2123
import java.util.concurrent.atomic.AtomicInteger;
2224

2325
import org.junit.Test;
@@ -33,6 +35,10 @@
3335
import rx.subjects.PublishSubject;
3436

3537
public class ExceptionsTest {
38+
@Test
39+
public void constructorShouldBePrivate() {
40+
PrivateConstructorChecker.forClass(Exceptions.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
41+
}
3642

3743
@Test(expected = OnErrorNotImplementedException.class)
3844
public void testOnErrorNotImplementedIsThrown() {

src/test/java/rx/functions/ActionsTest.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package rx.functions;
1717

18-
import static org.junit.Assert.*;
18+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
19+
20+
import org.junit.Test;
1921

20-
import java.lang.reflect.*;
2122
import java.util.Arrays;
2223
import java.util.concurrent.atomic.AtomicLong;
2324

24-
import org.junit.Test;
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertNull;
2527

2628
public class ActionsTest {
2729

@@ -271,20 +273,7 @@ public void call(Object... args) {
271273
}
272274

273275
@Test
274-
public void testNotInstantiable() {
275-
try {
276-
Constructor<?> c = Actions.class.getDeclaredConstructor();
277-
c.setAccessible(true);
278-
Object instance = c.newInstance();
279-
fail("Could instantiate Actions! " + instance);
280-
} catch (NoSuchMethodException ex) {
281-
ex.printStackTrace();
282-
} catch (InvocationTargetException ex) {
283-
ex.printStackTrace();
284-
} catch (InstantiationException ex) {
285-
ex.printStackTrace();
286-
} catch (IllegalAccessException ex) {
287-
ex.printStackTrace();
288-
}
276+
public void constructorShouldBePrivate() {
277+
PrivateConstructorChecker.forClass(Actions.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
289278
}
290279
}

src/test/java/rx/functions/FunctionsTest.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,19 @@
1515
*/
1616
package rx.functions;
1717

18-
import static org.junit.Assert.*;
18+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
19+
20+
import org.junit.Test;
1921

20-
import java.lang.reflect.*;
2122
import java.util.Arrays;
2223
import java.util.concurrent.atomic.AtomicLong;
2324

24-
import org.junit.Test;
25+
import static org.junit.Assert.assertEquals;
2526

2627
public class FunctionsTest {
2728
@Test
28-
public void testNotInstantiable() {
29-
try {
30-
Constructor<?> c = Functions.class.getDeclaredConstructor();
31-
c.setAccessible(true);
32-
Object instance = c.newInstance();
33-
fail("Could instantiate Actions! " + instance);
34-
} catch (NoSuchMethodException ex) {
35-
ex.printStackTrace();
36-
} catch (InvocationTargetException ex) {
37-
ex.printStackTrace();
38-
} catch (InstantiationException ex) {
39-
ex.printStackTrace();
40-
} catch (IllegalAccessException ex) {
41-
ex.printStackTrace();
42-
}
29+
public void constructorShouldBePrivate() {
30+
PrivateConstructorChecker.forClass(Functions.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
4331
}
4432

4533
@Test(expected = RuntimeException.class)

src/test/java/rx/internal/operators/BackpressureUtilsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515
*/
1616
package rx.internal.operators;
1717

18+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
19+
1820
import org.junit.Test;
1921
import static org.junit.Assert.*;
2022

2123
public class BackpressureUtilsTest {
24+
@Test
25+
public void constructorShouldBePrivate() {
26+
PrivateConstructorChecker.forClass(BackpressureUtils.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
27+
}
28+
2229
@Test
2330
public void testAddCap() {
2431
assertEquals(2L, BackpressureUtils.addCap(1, 1));

src/test/java/rx/internal/operators/BlockingOperatorLatestTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package rx.internal.operators;
1717

18+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
19+
1820
import java.util.Iterator;
1921
import java.util.NoSuchElementException;
2022
import java.util.concurrent.TimeUnit;
@@ -29,6 +31,11 @@
2931
import rx.subjects.PublishSubject;
3032

3133
public class BlockingOperatorLatestTest {
34+
@Test
35+
public void constructorShouldBePrivate() {
36+
PrivateConstructorChecker.forClass(BlockingOperatorLatest.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
37+
}
38+
3239
@Test(timeout = 1000)
3340
public void testSimple() {
3441
TestScheduler scheduler = new TestScheduler();

src/test/java/rx/internal/operators/BlockingOperatorMostRecentTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.junit.Assert.assertTrue;
2121
import static rx.internal.operators.BlockingOperatorMostRecent.mostRecent;
2222

23+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
24+
2325
import java.util.Iterator;
2426
import java.util.concurrent.TimeUnit;
2527

@@ -34,6 +36,11 @@
3436
import rx.subjects.Subject;
3537

3638
public class BlockingOperatorMostRecentTest {
39+
@Test
40+
public void constructorShouldBePrivate() {
41+
PrivateConstructorChecker.forClass(BlockingOperatorMostRecent.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
42+
}
43+
3744
@Test
3845
public void testMostRecentNull() {
3946
assertEquals(null, Observable.<Void>never().toBlocking().mostRecent(null).iterator().next());

0 commit comments

Comments
 (0)