Skip to content
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

80% code coverage for StorIO-Common #461

Merged
merged 1 commit into from
Jul 27, 2015
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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ ext.libraries = [
// Libraries for tests and sample app
junit : 'junit:junit:4.12',
mockitoCore : 'org.mockito:mockito-core:1.10.19',
powerMockJUnit : 'org.powermock:powermock-module-junit4:1.6.2',
powerMockMockito : 'org.powermock:powermock-api-mockito:1.6.2',

equalsVerifier : 'nl.jqno.equalsverifier:equalsverifier:1.7.2',
guava : 'com.google.guava:guava:18.0',
robolectric : 'org.robolectric:robolectric:3.0-rc3',
Expand Down
4 changes: 4 additions & 0 deletions storio-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ dependencies {

testCompile libraries.junit
testCompile libraries.mockitoCore
testCompile libraries.powerMockJUnit
testCompile libraries.powerMockMockito
testCompile libraries.equalsVerifier
testCompile libraries.storIOTestCommon
}

Expand All @@ -40,4 +43,5 @@ task checkstyle(type: Checkstyle) {
classpath = files()
}

apply from: '../gradle/jacoco-android.gradle'
apply from: '../gradle/publish-android-lib.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
*/
public class StorIOException extends RuntimeException {

/**
* {@inheritDoc}
*/
public StorIOException() {
super();
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import rx.Observable;

import static com.pushtorefresh.storio.internal.Environment.RX_JAVA_IS_AVAILABLE;

/**
* FOR INTERNAL USAGE ONLY.
* <p>
Expand All @@ -15,9 +13,13 @@
public final class ChangesBus<T> {

@Nullable
private final RxChangesBus<T> rxChangesBus = RX_JAVA_IS_AVAILABLE
? new RxChangesBus<T>()
: null;
private final RxChangesBus<T> rxChangesBus;

public ChangesBus(boolean rxJavaIsInTheClassPath) {
rxChangesBus = rxJavaIsInTheClassPath
? new RxChangesBus<T>()
: null;
}

public void onNext(@NonNull T next) {
if (rxChangesBus != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public final class Checks {

private Checks() {
throw new IllegalStateException("No instances please");
throw new IllegalStateException("No instances please.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public final class Environment {
/**
* True if RxJava is on classpath, false otherwise
*/
public static final boolean RX_JAVA_IS_AVAILABLE = isRxJavaAvailable();
public static final boolean RX_JAVA_IS_IN_THE_CLASS_PATH = isRxJavaInTheClassPath();

private Environment() {
throw new IllegalStateException("No instances please");
}

// thanks Retrofit for that piece of code
private static boolean isRxJavaAvailable() {
private static boolean isRxJavaInTheClassPath() {
try {
Class.forName("rx.Observable");
return true;
Expand All @@ -32,7 +32,7 @@ private static boolean isRxJavaAvailable() {
* @param messagePrefix first part of exception message, for example: "Creating Observable"
*/
public static void throwExceptionIfRxJavaIsNotAvailable(@NonNull String messagePrefix) {
if (!RX_JAVA_IS_AVAILABLE) {
if (!RX_JAVA_IS_IN_THE_CLASS_PATH) {
throw new IllegalStateException(messagePrefix + " requires RxJava in classpath, please add it as compile dependency to the application");
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pushtorefresh.storio;

import org.junit.Test;

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

public class StorIOExceptionTest {

@Test
public void checkConstructorWithDetailMessage() {
StorIOException storIOException = new StorIOException("test detail message");
assertEquals("test detail message", storIOException.getMessage());
}

@Test
public void checkConstructorWithDetailMessageAndThrowable() {
Throwable testThrowable = new RuntimeException("yo");
StorIOException storIOException = new StorIOException("test detail message", testThrowable);

assertEquals("test detail message", storIOException.getMessage());
assertSame(testThrowable, storIOException.getCause());
}

@Test
public void checkConstructorWithThrowable() {
Throwable testThrowable = new RuntimeException("yo");
StorIOException storIOException = new StorIOException(testThrowable);

assertSame(testThrowable, storIOException.getCause());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.pushtorefresh.storio.internal;

import org.junit.Test;

import java.util.List;

import rx.Observable;
import rx.observers.TestSubscriber;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

public class ChangesBusTest {

@Test
public void asObservableShouldNotReturnNullIfRxJavaInClassPath() {
ChangesBus<String> changesBus = new ChangesBus<String>(true);
assertNotNull(changesBus.asObservable());
}

@Test
public void asObservableShouldReturnNullIfRxJavaIsNotInTheClassPath() {
ChangesBus<String> changesBus = new ChangesBus<String>(false);
assertNull(changesBus.asObservable());
}

@Test
public void onNextShouldNotThrowExceptionIfRxJavaIsNotInTheClassPath() {
ChangesBus<String> changesBus = new ChangesBus<String>(false);

try {
changesBus.onNext("don't crash me bro");
} catch (Exception e) {
fail("Yo, WTF dude?");
}
}

@Test
public void onNextShouldSendMessagesToObserverIfRxJavaIsInTheClassPath() {
ChangesBus<String> changesBus = new ChangesBus<String>(true);

TestSubscriber<String> testSubscriber = new TestSubscriber<String>();

Observable<String> observable = changesBus.asObservable();
assertNotNull(observable);

observable.subscribe(testSubscriber);

List<String> messages = asList("My", "life", "my", "rules", "please?");

for (String message: messages) {
changesBus.onNext(message);
}

testSubscriber.assertReceivedOnNext(messages);
testSubscriber.assertNoTerminalEvent();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pushtorefresh.storio.internal;

import com.pushtorefresh.storio.test.PrivateConstructorChecker;

import org.junit.Test;

import static com.pushtorefresh.storio.internal.Checks.checkNotEmpty;
Expand All @@ -9,6 +11,15 @@

public class ChecksTest {

@Test
public void constructorShouldBePrivateAndThrowException() {
PrivateConstructorChecker
.forClass(Checks.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances please.")
.check();
}

@Test
public void checkNotNullPositive() {
checkNotNull(new Object(), "No exceptions please");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.pushtorefresh.storio.internal;

import com.pushtorefresh.storio.test.PrivateConstructorChecker;

import org.junit.Test;

import java.lang.reflect.Field;

import static java.lang.reflect.Modifier.FINAL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class EnvironmentTest {

@Test
public void constructorShouldBePrivateAndThrowException() {
PrivateConstructorChecker
.forClass(Environment.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances please")
.check();
}

@Test
public void rxJavaShouldBeInClassPath() {
assertTrue(Environment.RX_JAVA_IS_IN_THE_CLASS_PATH);
}

@Test
public void shouldThrowExceptionIfRxJavaIsNotInTheClassPath() throws NoSuchFieldException, IllegalAccessException {
Field field = Environment.class.getDeclaredField("RX_JAVA_IS_IN_THE_CLASS_PATH");

field.setAccessible(true);

// Removing FINAL modifier
Field modifiersFieldOfTheField = Field.class.getDeclaredField("modifiers");
modifiersFieldOfTheField.setAccessible(true);
modifiersFieldOfTheField.setInt(field, field.getModifiers() & ~FINAL);

final Object prevValue = field.get(null);

field.set(null, false); // No Environment will think that RxJava is not in the ClassPath

try {
Environment.throwExceptionIfRxJavaIsNotAvailable("yolo");
fail();
} catch (IllegalStateException expected) {
assertEquals("yolo requires RxJava in classpath," +
" please add it as compile dependency to the application",
expected.getMessage()
);
} finally {
// Return previous value of the field
field.set(null, prevValue);

// Restoring FINAL modifier (for better tests performance)
modifiersFieldOfTheField.setInt(field, field.getModifiers() & FINAL);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why should it be faster withfinal? As for me it's not uniquely (http://stackoverflow.com/a/4279442)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, I think it will be faster because of JIT.
27 июля 2015 г. 8:59 AM пользователь "Dmitrii Nikitin" <
notifications@github.com> написал:

In
storio-common/src/test/java/com/pushtorefresh/storio/internal/EnvironmentTest.java
#461 (comment):

  •    field.set(null, false); // No Environment will think that RxJava is not in the ClassPath
    
  •    try {
    
  •        Environment.throwExceptionIfRxJavaIsNotAvailable("yolo");
    
  •        fail();
    
  •    } catch (IllegalStateException expected) {
    
  •        assertEquals("yolo requires RxJava in classpath," +
    
  •                        " please add it as compile dependency to the application",
    
  •                expected.getMessage()
    
  •        );
    
  •    } finally {
    
  •        // Return previous value of the field
    
  •        field.set(null, prevValue);
    
  •        // Restoring FINAL modifier (for better tests performance)
    
  •        modifiersFieldOfTheField.setInt(field, field.getModifiers() & FINAL);
    

Why should it be faster withfinal? As for me it's not uniquely (
http://stackoverflow.com/a/4279442)


Reply to this email directly or view it on GitHub
https://github.com/pushtorefresh/storio/pull/461/files#r35507387.

}
}

@Test
public void shouldNotThrowExceptionIfRxJavaIsInTheClassPath() {
// Because RxJava should be in the ClassPath for tests
Environment.throwExceptionIfRxJavaIsNotAvailable("no exceptions please");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pushtorefresh.storio.internal;

import com.pushtorefresh.storio.test.PrivateConstructorChecker;

import org.junit.Test;

import java.util.ArrayList;
Expand All @@ -18,6 +20,15 @@

public class QueriesTest {

@Test
public void constructorShouldBePrivateAndThrowException() {
PrivateConstructorChecker
.forClass(Queries.class)
.expectedTypeOfException(IllegalStateException.class)
.expectedExceptionMessage("No instances please")
.check();
}

//region Tests for Queries.unmodifiableNonNullListOfStrings()

@SuppressWarnings("ConstantConditions")
Expand Down
Loading