From 89ca0ae19654f4251c017abb9bcb86005c1e39d6 Mon Sep 17 00:00:00 2001 From: Jyotprabhs Date: Fri, 21 Mar 2025 11:27:46 -0400 Subject: [PATCH 1/4] Added test files for boundary value analysis --- .../commons/io/FilenameUtils_TestPk.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/test/java/org/apache/commons/io/FilenameUtils_TestPk.java diff --git a/src/test/java/org/apache/commons/io/FilenameUtils_TestPk.java b/src/test/java/org/apache/commons/io/FilenameUtils_TestPk.java new file mode 100644 index 00000000000..27589fe2560 --- /dev/null +++ b/src/test/java/org/apache/commons/io/FilenameUtils_TestPk.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.io; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class FilenameUtils_TestPk { + + String fileName ; + // run before each test + @BeforeEach + void setUp(){ + fileName = ""; + } + + //test to check an empty file name + @Test + void testEmptyFileName(){ + fileName = ""; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("", baseName , "Base name should be empty for a empty input"); + } + + //Test to check single character file + + @Test + void testSingleCharacterFileName(){ + fileName = "p"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("p", baseName, "Base name should be the same for a single character file name"); + + } + + //Test to check file name with extension: typical case + @Test + void testFileNameWithExtension(){ + fileName = "document.text"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("document", baseName, " Base name should be 'document' when file name has an extension."); + } + //Test with no extension + @Test + void testFileNameWithNoExtension(){ + fileName = "document"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("document", baseName, " Base name should be the same as'document' when file name has no extension."); + } + // Test with special Character + @Test + void testFileNameWithSpecialCharacters(){ + fileName = "doc@file#name.txt"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("doc@file#name", baseName, " Base name should correctly handle special characters"); + } + // Test with numbers in filename + @Test + void testFileNameWithNumbers(){ + fileName = "doc1234.txt"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("doc1234", baseName, " Base name should correctly handle numbers"); + } + // Test with multiple dots + @Test + void testFileNameWithMultipleDots(){ + fileName = "document.excel.dot.txt"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("document.excel.dot", baseName, " Base name should remove only one extension"); + } + //Test involving full path of file + @Test + void testFileNameWithFullPath(){ + fileName = "C:\\Users\\14197\\Documents\\MyCode.txt"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("MyCode", baseName, " Base name should give only file name"); + } + //Test with different formats + @Test + void testFileNameWithDiffFormat(){ + fileName = "Document.pdf"; + String baseName = FilenameUtils.getBaseName(fileName); + assertEquals("Document", baseName, " Base name work perfectly with different formats."); + } +} From 72cdc8c2285d31817bc12d440c8b16767280d9df Mon Sep 17 00:00:00 2001 From: Jyotprabhs Date: Mon, 7 Apr 2025 03:46:48 -0400 Subject: [PATCH 2/4] Add test for FileUtils --- .../apache/commons/io/FileUtils_TestPK.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/java/org/apache/commons/io/FileUtils_TestPK.java diff --git a/src/test/java/org/apache/commons/io/FileUtils_TestPK.java b/src/test/java/org/apache/commons/io/FileUtils_TestPK.java new file mode 100644 index 00000000000..1eb7fe94cb5 --- /dev/null +++ b/src/test/java/org/apache/commons/io/FileUtils_TestPK.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.io; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; + +import org.junit.jupiter.api.Test; + + + +public class FileUtils_TestPK { + //test with null input + @Test + void testDeleteQuietlyWithNull(){ + File file =null; + boolean result = FileUtils.deleteQuietly(file); + assertFalse(result); //expecting false since file is null + + } + //Test with a non-existent file + @Test + void testDeleteQuietlyWithNonExistentFiles(){ + File file = new File("nonExistentFile.txt"); + boolean result = FileUtils.deleteQuietly(file); + assertFalse(result); //expecting false since file is null + + } + + //Test with an empty directory (boundary value) + @Test + void testDeleteQuietlyWithEmptyDirectory() throws IOException{ + File dir= new File("EmptyDir"); + + dir.mkdir();// creates an emtpy dir + boolean result = FileUtils.deleteQuietly(dir); + assertTrue(result); //expecting true since dir is empty + + } + + //Test with a non-empty directory + @Test + void testDeleteQuietlyWithNonEmptyDirectory() throws IOException{ + File dir= new File("nonEmptyDir"); + + dir.mkdir();// creates a non emtpy dir + new File(dir,"file1.txt").createNewFile(); + boolean result = FileUtils.deleteQuietly(dir); + assertTrue(result); //expecting true since dir is empty + + } + + +} From 952ea4636da0a065e41455baeb0e77db67ac861f Mon Sep 17 00:00:00 2001 From: Jyotprabhs Date: Tue, 8 Apr 2025 00:18:43 -0400 Subject: [PATCH 3/4] Replaced old test files with new structured versions for FileUtils and FilenameUtils --- ...eUtils_TestPK.java => FileUtils_Test.java} | 53 ++++++++++++++++++- ...ls_TestPk.java => FilenameUtils_Test.java} | 15 +++++- 2 files changed, 66 insertions(+), 2 deletions(-) rename src/test/java/org/apache/commons/io/{FileUtils_TestPK.java => FileUtils_Test.java} (58%) rename src/test/java/org/apache/commons/io/{FilenameUtils_TestPk.java => FilenameUtils_Test.java} (92%) diff --git a/src/test/java/org/apache/commons/io/FileUtils_TestPK.java b/src/test/java/org/apache/commons/io/FileUtils_Test.java similarity index 58% rename from src/test/java/org/apache/commons/io/FileUtils_TestPK.java rename to src/test/java/org/apache/commons/io/FileUtils_Test.java index 1eb7fe94cb5..c14d71dc74e 100644 --- a/src/test/java/org/apache/commons/io/FileUtils_TestPK.java +++ b/src/test/java/org/apache/commons/io/FileUtils_Test.java @@ -16,17 +16,19 @@ */ package org.apache.commons.io; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.IOException; +import java.math.BigInteger; import org.junit.jupiter.api.Test; -public class FileUtils_TestPK { +public class FileUtils_Test { //test with null input @Test void testDeleteQuietlyWithNull(){ @@ -66,6 +68,55 @@ void testDeleteQuietlyWithNonEmptyDirectory() throws IOException{ assertTrue(result); //expecting true since dir is empty } + // Test written to check byteCountToDisplaySize() using boundary value analysis + + //just before 1 kB + @Test + public void testByteCountToDisplaySizeBelow1KB(){ + assertEquals("1023 bytes", + FileUtils.byteCountToDisplaySize(BigInteger.valueOf(1023))); + } + + //Exactly at 1 kB + @Test + public void testByteCountToDisplaySizeExactlyAt1KB(){ + assertEquals("1 KB", + FileUtils.byteCountToDisplaySize(BigInteger.valueOf(1024))); + } + + //Just after 1 kB + @Test + public void testByteCountToDisplaySizeJustAfter1KB(){ + assertEquals("1 KB", + FileUtils.byteCountToDisplaySize(BigInteger.valueOf(1025))); + } + + //Just before 1 MB + @Test + public void testByteCountToDisplaySizeJustBefore1MB(){ + assertEquals("1023 KB", + FileUtils.byteCountToDisplaySize(BigInteger.valueOf(1023L * 1024))); + } + + //Exactly at 1 MB + @Test + public void testByteCountToDisplaySizeExactlyAt1MB(){ + assertEquals("1 MB", + FileUtils.byteCountToDisplaySize(BigInteger.valueOf(1024L * 1024))); + } + //Just before at 1 GB + @Test + public void testByteCountToDisplaySizeJustBeforeAt1GB(){ + assertEquals("1023 MB", + FileUtils.byteCountToDisplaySize(BigInteger.valueOf(1023L * 1024 * 1024))); + } + //Exactly at 1 GB + @Test + public void testByteCountToDisplaySizeExactlyAt1GB(){ + assertEquals("1 GB", + FileUtils.byteCountToDisplaySize(BigInteger.valueOf(1024L * 1024 * 1024))); + } + } diff --git a/src/test/java/org/apache/commons/io/FilenameUtils_TestPk.java b/src/test/java/org/apache/commons/io/FilenameUtils_Test.java similarity index 92% rename from src/test/java/org/apache/commons/io/FilenameUtils_TestPk.java rename to src/test/java/org/apache/commons/io/FilenameUtils_Test.java index 27589fe2560..ed77f75533d 100644 --- a/src/test/java/org/apache/commons/io/FilenameUtils_TestPk.java +++ b/src/test/java/org/apache/commons/io/FilenameUtils_Test.java @@ -22,7 +22,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class FilenameUtils_TestPk { +public class FilenameUtils_Test { + // Test Cases for getBaseName() String fileName ; // run before each test @@ -98,4 +99,16 @@ void testFileNameWithDiffFormat(){ String baseName = FilenameUtils.getBaseName(fileName); assertEquals("Document", baseName, " Base name work perfectly with different formats."); } + + //White Box test for getExtension() + // test filename is null + @Test + void testFileNameIsNull(){ + + } + // test for no index found in file name + @Test + void testFileNameIndexNotFound(){ + + } } From ab79533f6db160b1eae1f4b109132dabdc93fc57 Mon Sep 17 00:00:00 2001 From: Jyotprabhs Date: Sat, 12 Apr 2025 02:08:08 -0400 Subject: [PATCH 4/4] Update FilenameUtils_Test.java with necessary changes --- .../apache/commons/io/FilenameUtils_Test.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/apache/commons/io/FilenameUtils_Test.java b/src/test/java/org/apache/commons/io/FilenameUtils_Test.java index ed77f75533d..b852060f239 100644 --- a/src/test/java/org/apache/commons/io/FilenameUtils_Test.java +++ b/src/test/java/org/apache/commons/io/FilenameUtils_Test.java @@ -100,15 +100,14 @@ void testFileNameWithDiffFormat(){ assertEquals("Document", baseName, " Base name work perfectly with different formats."); } - //White Box test for getExtension() - // test filename is null - @Test - void testFileNameIsNull(){ - - } - // test for no index found in file name - @Test - void testFileNameIndexNotFound(){ - + + // Test for getExtension() starts here + + //Test to get extension of a valid filename + @Test + void testGetExtensionForValidFileName(){ + fileName="file.txt"; + + assertEquals("txt", FilenameUtils.getExtension(fileName)); } }