Merge pull request #3579 from Isira-Seneviratne/Add_tests_for_BOINCUtils

[Android] Add unit tests for BOINCUtils.
This commit is contained in:
Vitalii Koshura 2020-04-09 00:49:20 +02:00 committed by GitHub
commit c67ab30015
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 4 deletions

View File

@ -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 {

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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");
}
}