From 5be17e7ded279af00b9a38553e9e4a19a14397f5 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 8 Apr 2020 05:44:23 +0530 Subject: [PATCH] Add unit tests for BOINCUtils. Also add Apache Commons IO as a test dependency and update Apache Commons Lang to 3.10. --- android/BOINC/app/build.gradle | 8 +- .../berkeley/boinc/utils/BOINCUtilsTest.java | 93 +++++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 android/BOINC/app/src/test/java/edu/berkeley/boinc/utils/BOINCUtilsTest.java diff --git a/android/BOINC/app/build.gradle b/android/BOINC/app/build.gradle index f39cceff23..9539d4bc0e 100644 --- a/android/BOINC/app/build.gradle +++ b/android/BOINC/app/build.gradle @@ -90,20 +90,20 @@ android { } ext.powermock_version = '2.0.5' -ext.lombok_version = '1.18.12' dependencies { implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' - implementation "androidx.core:core-ktx:1.2.0" - implementation 'org.apache.commons:commons-lang3:3.9' + implementation 'androidx.core:core-ktx:1.2.0' + implementation 'org.apache.commons:commons-lang3:3.10' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'org.eclipse.collections:eclipse-collections:10.2.0' + testImplementation 'com.google.guava:guava-testlib:28.2-jre' + testImplementation 'commons-io:commons-io:2.6' testImplementation 'junit:junit:4.13' testImplementation "org.powermock:powermock-module-junit4:$powermock_version" testImplementation "org.powermock:powermock-api-mockito2:$powermock_version" - testImplementation 'com.google.guava:guava-testlib:28.2-jre' testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" } repositories { diff --git a/android/BOINC/app/src/test/java/edu/berkeley/boinc/utils/BOINCUtilsTest.java b/android/BOINC/app/src/test/java/edu/berkeley/boinc/utils/BOINCUtilsTest.java new file mode 100644 index 0000000000..55d7e41781 --- /dev/null +++ b/android/BOINC/app/src/test/java/edu/berkeley/boinc/utils/BOINCUtilsTest.java @@ -0,0 +1,93 @@ +/* + * This file is part of BOINC. + * http://boinc.berkeley.edu + * Copyright (C) 2020 University of California + * + * BOINC is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * BOINC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with BOINC. If not, see . + */ +package edu.berkeley.boinc.utils; + +import org.apache.commons.io.input.CharSequenceReader; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.io.Reader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class BOINCUtilsTest { + private StringBuilder stringBuilder; + private Reader reader; + + @Before + public void setUp() { + stringBuilder = new StringBuilder("This is a string."); + reader = new CharSequenceReader(stringBuilder); + } + + @Test + public void testReadLineLimit_whenReaderIsNullAndLimitIs0_thenExpectEmptyString() + throws IOException { + String result = BOINCUtils.readLineLimit(null, 0); + + assertNotNull(result); + assertTrue(result.isEmpty()); + } + + @Test(expected = NullPointerException.class) + public void testReadLineLimit_whenReaderIsNullAndLimitIs1_thenExpectNullPointerException() + throws IOException { + BOINCUtils.readLineLimit(null, 1); + } + + @Test + public void testReadLineLimit_whenReaderHasEmptyStringAndLimitIs1_thenExpectNull() + throws IOException { + stringBuilder.setLength(0); + + assertNull(BOINCUtils.readLineLimit(reader, 1)); + } + + @Test + public void testReadLineLimit_whenReaderHasNonEmptyStringAndLimitIsLengthOfString_thenExpectReaderString() + throws IOException { + assertEquals(BOINCUtils.readLineLimit(reader, stringBuilder.length()), "This is a string."); + } + + @Test + public void testReadLineLimit_whenReaderHasNonEmptyStringAndLimitIsLengthOfStringPlus1_thenExpectReaderString() + throws IOException { + assertEquals(BOINCUtils.readLineLimit(reader, stringBuilder.length() + 1), "This is a string."); + } + + @Test + public void testReadLineLimit_whenReaderHasStringWithNewlineAndLimitIsLengthOfString_thenExpectStringWithoutNewline() + throws IOException { + stringBuilder.setCharAt(4, '\n'); + + assertEquals(BOINCUtils.readLineLimit(reader, stringBuilder.length()), "This"); + } + + @Test + public void testReadLineLimit_whenReaderHasStringWithCarriageReturnAndLimitIsLengthOfString_thenExpectStringWithoutCarriageReturn() + throws IOException { + stringBuilder.setCharAt(4, '\r'); + + assertEquals(BOINCUtils.readLineLimit(reader, stringBuilder.length()), "This"); + } +}