Skip to content

Refactor private constructor checker into a utility class #4133

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 1 commit 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
4 changes: 1 addition & 3 deletions src/test/java/rx/EventStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
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();
TestUtil.checkUtilityClass(EventStream.class);
}
}
39 changes: 39 additions & 0 deletions src/test/java/rx/TestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2014 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;

/**
* Common test utility methods.
*/
public enum TestUtil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - why use an enum here instead of a class? Is it just to forbid inheritance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly - but not my invention. I saw this in the JDK source and in fact, saves you testing a private constructor. However, we can't expose such enums through the public API so only some internal tools are defined this way.

;

/**
* Verifies that the given class has a private constructor that
* throws IllegalStateException("No instances!") upon instantiation.
* @param clazz the target class to check
*/
public static void checkUtilityClass(Class<?> clazz) {
PrivateConstructorChecker
.forClass(clazz)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances!")
.check();
}
}
16 changes: 4 additions & 12 deletions src/test/java/rx/exceptions/ExceptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,21 @@
*/
package rx.exceptions;

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

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

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import rx.Single;
import rx.SingleSubscriber;
import rx.Subscriber;
import rx.Observable;
import rx.Observer;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.*;
import rx.functions.*;
import rx.observables.GroupedObservable;
import rx.subjects.PublishSubject;

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

@Test(expected = OnErrorNotImplementedException.class)
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/rx/functions/ActionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package rx.functions;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

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

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

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

import rx.TestUtil;

public class ActionsTest {

Expand Down Expand Up @@ -274,6 +273,6 @@ public void call(Object... args) {

@Test
public void constructorShouldBePrivate() {
PrivateConstructorChecker.forClass(Actions.class).expectedTypeOfException(IllegalStateException.class).expectedExceptionMessage("No instances!").check();
TestUtil.checkUtilityClass(Actions.class);
}
}
10 changes: 5 additions & 5 deletions src/test/java/rx/functions/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/
package rx.functions;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

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

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

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

import rx.TestUtil;

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

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

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

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

import rx.TestUtil;

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@
*/
package rx.internal.operators;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

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

import org.junit.Assert;

import org.junit.Test;
import org.junit.*;

import rx.Observable;
import rx.TestUtil;
import rx.observables.BlockingObservable;
import rx.schedulers.TestScheduler;
import rx.subjects.PublishSubject;

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

@Test(timeout = 1000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,24 @@
*/
package rx.internal.operators;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static rx.internal.operators.BlockingOperatorMostRecent.mostRecent;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;

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

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

import rx.Observable;
import rx.*;
import rx.exceptions.TestException;
import rx.observables.BlockingObservable;
import rx.schedulers.TestScheduler;
import rx.subjects.PublishSubject;
import rx.subjects.Subject;
import rx.subjects.*;

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

@Test
Expand Down
29 changes: 9 additions & 20 deletions src/test/java/rx/internal/operators/BlockingOperatorNextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,21 @@
*/
package rx.internal.operators;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
import static org.junit.Assert.*;
import static rx.internal.operators.BlockingOperatorNext.next;

import org.junit.Assert;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.*;

import rx.*;
import rx.Observable;
import rx.Subscriber;
import rx.exceptions.TestException;
import rx.observables.BlockingObservable;
import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;
import rx.subjects.Subject;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static rx.internal.operators.BlockingOperatorNext.next;
import rx.subjects.*;

public class BlockingOperatorNextTest {

Expand Down Expand Up @@ -74,7 +63,7 @@ public void run() {

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,23 @@
*/
package rx.internal.operators;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
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;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.*;
import java.util.concurrent.*;

import org.junit.Test;

import rx.*;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.exceptions.TestException;

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@
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;

import rx.Observable;
import rx.*;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.exceptions.TestException;
import rx.internal.operators.BlockingOperatorToIterator.SubscriberIterator;
import rx.internal.util.RxRingBuffer;

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@
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;

import org.junit.Test;

import rx.*;
import rx.observers.*;
import rx.observers.TestSubscriber;
import rx.schedulers.Schedulers;

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@
package rx.internal.operators;

import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

import com.pushtorefresh.private_constructor_checker.PrivateConstructorChecker;
import static org.mockito.Mockito.*;

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

import rx.Observable;
import rx.Observer;
import rx.*;
import rx.exceptions.TestException;
import rx.functions.Func2;

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

@Test
Expand Down
Loading