diff --git a/src/test/java/org/apache/commons/io/FileUtils_Test.java b/src/test/java/org/apache/commons/io/FileUtils_Test.java new file mode 100644 index 00000000000..c14d71dc74e --- /dev/null +++ b/src/test/java/org/apache/commons/io/FileUtils_Test.java @@ -0,0 +1,122 @@ +/* + * 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 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_Test { + //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 + + } + // 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_Test.java b/src/test/java/org/apache/commons/io/FilenameUtils_Test.java new file mode 100644 index 00000000000..b852060f239 --- /dev/null +++ b/src/test/java/org/apache/commons/io/FilenameUtils_Test.java @@ -0,0 +1,113 @@ +/* + * 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_Test { + // Test Cases for getBaseName() + + 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."); + } + + + // Test for getExtension() starts here + + //Test to get extension of a valid filename + @Test + void testGetExtensionForValidFileName(){ + fileName="file.txt"; + + assertEquals("txt", FilenameUtils.getExtension(fileName)); + } +}