Skip to content

1.x: Test all private Constructors #4125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'com.google.guava:guava:19.0'
testCompile 'com.pushtorefresh.java-private-constructor-checker:checker:1.2.0'

perfCompile 'org.openjdk.jmh:jmh-core:1.11.3'
perfCompile 'org.openjdk.jmh:jmh-generator-annprocess:1.11.3'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/util/unsafe/Pow2.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public static int roundToPowerOfTwo(final int value) {
public static boolean isPowerOfTwo(final int value) {
return (value & (value - 1)) == 0;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/rx/internal/util/unsafe/UnsafeAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ public static long addressOf(Class<?> clazz, String fieldName) {
throw ie;
}
}
}
}
27 changes: 27 additions & 0 deletions src/test/java/rx/EventStreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;

public class EventStreamTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(EventStream.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}
}
6 changes: 6 additions & 0 deletions src/test/java/rx/exceptions/ExceptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
Expand All @@ -33,6 +35,10 @@
import rx.subjects.PublishSubject;

public class ExceptionsTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(Exceptions.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test(expected = OnErrorNotImplementedException.class)
public void testOnErrorNotImplementedIsThrown() {
Expand Down
25 changes: 7 additions & 18 deletions src/test/java/rx/functions/ActionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package rx.functions;

import static org.junit.Assert.*;
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;

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

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class ActionsTest {

Expand Down Expand Up @@ -271,20 +273,7 @@ public void call(Object... args) {
}

@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();
}
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(Actions.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}
}
24 changes: 6 additions & 18 deletions src/test/java/rx/functions/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,19 @@
*/
package rx.functions;

import static org.junit.Assert.*;
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;

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

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

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();
}
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(Functions.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test(expected = RuntimeException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
*/
package rx.internal.operators;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;
import static org.junit.Assert.*;

public class BackpressureUtilsTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(BackpressureUtils.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test
public void testAddCap() {
assertEquals(2L, BackpressureUtils.addCap(1, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package rx.internal.operators;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
Expand All @@ -29,6 +31,11 @@
import rx.subjects.PublishSubject;

public class BlockingOperatorLatestTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(BlockingOperatorLatest.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test(timeout = 1000)
public void testSimple() {
TestScheduler scheduler = new TestScheduler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.junit.Assert.assertTrue;
import static rx.internal.operators.BlockingOperatorMostRecent.mostRecent;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

Expand All @@ -34,6 +36,11 @@
import rx.subjects.Subject;

public class BlockingOperatorMostRecentTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(BlockingOperatorMostRecent.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test
public void testMostRecentNull() {
assertEquals(null, Observable.<Void>never().toBlocking().mostRecent(null).iterator().next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package rx.internal.operators;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -70,6 +72,11 @@ public void run() {
}.start();
}

@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(BlockingOperatorNext.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test
public void testNext() {
Subject<String, String> obs = PublishSubject.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.junit.Assert.fail;
import static rx.internal.operators.BlockingOperatorToFuture.toFuture;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.CancellationException;
Expand All @@ -35,6 +37,10 @@
import rx.exceptions.TestException;

public class BlockingOperatorToFutureTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(BlockingOperatorToFuture.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test
public void testToFuture() throws InterruptedException, ExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.junit.Assert.assertEquals;
import static rx.internal.operators.BlockingOperatorToIterator.toIterator;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import java.util.Iterator;

import org.junit.Test;
Expand All @@ -30,6 +32,10 @@
import rx.internal.util.RxRingBuffer;

public class BlockingOperatorToIteratorTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(BlockingOperatorToIterator.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test
public void testToIterator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -29,6 +31,10 @@
import rx.schedulers.Schedulers;

public class OnSubscribeToObservableFutureTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(OnSubscribeToObservableFuture.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test
public void testSuccess() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;
import org.mockito.InOrder;

Expand All @@ -29,6 +31,10 @@
import rx.functions.Func2;

public class OperatorSequenceEqualTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(OperatorSequenceEqual.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

@Test
public void test1() {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/rx/internal/operators/SingleOperatorZipTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.internal.operators;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;

public class SingleOperatorZipTest {
@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(SingleOperatorZip.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}
}
22 changes: 3 additions & 19 deletions src/test/java/rx/internal/util/PlatformDependentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,13 @@
*/
package rx.internal.util;

import static org.junit.Assert.fail;

import java.lang.reflect.*;
import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

import org.junit.Test;

public class PlatformDependentTest {
@Test
public void testNotInstantiable() {
try {
Constructor<?> c = PlatformDependent.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();
}
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(PlatformDependent.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
}

}
Loading