Skip to content

Commit 5c74757

Browse files
vanniktechakarnokd
authored andcommitted
1.x: Test all private Constructors (ReactiveX#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());

src/test/java/rx/internal/operators/BlockingOperatorNextTest.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 org.junit.Assert;
1921
import org.junit.Test;
2022

@@ -70,6 +72,11 @@ public void run() {
7072
}.start();
7173
}
7274

75+
@Test
76+
public void constructorShouldBePrivate() {
77+
PrivateConstructorChecker.forClass(BlockingOperatorNext.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
78+
}
79+
7380
@Test
7481
public void testNext() {
7582
Subject<String, String> obs = PublishSubject.create();

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

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

23+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
24+
2325
import java.util.List;
2426
import java.util.NoSuchElementException;
2527
import java.util.concurrent.CancellationException;
@@ -35,6 +37,10 @@
3537
import rx.exceptions.TestException;
3638

3739
public class BlockingOperatorToFutureTest {
40+
@Test
41+
public void constructorShouldBePrivate() {
42+
PrivateConstructorChecker.forClass(BlockingOperatorToFuture.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
43+
}
3844

3945
@Test
4046
public void testToFuture() throws InterruptedException, ExecutionException {

src/test/java/rx/internal/operators/BlockingOperatorToIteratorTest.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.assertEquals;
1919
import static rx.internal.operators.BlockingOperatorToIterator.toIterator;
2020

21+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
22+
2123
import java.util.Iterator;
2224

2325
import org.junit.Test;
@@ -30,6 +32,10 @@
3032
import rx.internal.util.RxRingBuffer;
3133

3234
public class BlockingOperatorToIteratorTest {
35+
@Test
36+
public void constructorShouldBePrivate() {
37+
PrivateConstructorChecker.forClass(BlockingOperatorToIterator.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
38+
}
3339

3440
@Test
3541
public void testToIterator() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.mockito.Matchers.any;
2020
import static org.mockito.Mockito.*;
2121

22+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
23+
2224
import java.util.concurrent.*;
2325
import java.util.concurrent.atomic.AtomicBoolean;
2426

@@ -29,6 +31,10 @@
2931
import rx.schedulers.Schedulers;
3032

3133
public class OnSubscribeToObservableFutureTest {
34+
@Test
35+
public void constructorShouldBePrivate() {
36+
PrivateConstructorChecker.forClass(OnSubscribeToObservableFuture.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
37+
}
3238

3339
@Test
3440
public void testSuccess() throws Exception {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.mockito.Mockito.mock;
2121
import static org.mockito.Mockito.times;
2222

23+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
24+
2325
import org.junit.Test;
2426
import org.mockito.InOrder;
2527

@@ -29,6 +31,10 @@
2931
import rx.functions.Func2;
3032

3133
public class OperatorSequenceEqualTest {
34+
@Test
35+
public void constructorShouldBePrivate() {
36+
PrivateConstructorChecker.forClass(OperatorSequenceEqual.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
37+
}
3238

3339
@Test
3440
public void test1() {
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.internal.operators;
17+
18+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
19+
20+
import org.junit.Test;
21+
22+
public class SingleOperatorZipTest {
23+
@Test
24+
public void constructorShouldBePrivate() {
25+
PrivateConstructorChecker.forClass(SingleOperatorZip.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
26+
}
27+
}

src/test/java/rx/internal/util/PlatformDependentTest.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,13 @@
1515
*/
1616
package rx.internal.util;
1717

18-
import static org.junit.Assert.fail;
19-
20-
import java.lang.reflect.*;
18+
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
2119

2220
import org.junit.Test;
2321

2422
public class PlatformDependentTest {
2523
@Test
26-
public void testNotInstantiable() {
27-
try {
28-
Constructor<?> c = PlatformDependent.class.getDeclaredConstructor();
29-
c.setAccessible(true);
30-
Object instance = c.newInstance();
31-
fail("Could instantiate Actions! " + instance);
32-
} catch (NoSuchMethodException ex) {
33-
ex.printStackTrace();
34-
} catch (InvocationTargetException ex) {
35-
ex.printStackTrace();
36-
} catch (InstantiationException ex) {
37-
ex.printStackTrace();
38-
} catch (IllegalAccessException ex) {
39-
ex.printStackTrace();
40-
}
24+
public void constructorShouldBePrivate() {
25+
PrivateConstructorChecker.forClass(PlatformDependent.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
4126
}
42-
4327
}

0 commit comments

Comments
 (0)