Skip to content

Commit

Permalink
Fix unstable UtilAllTest#testCalculateFileSizeInPath on Windows (#7419)
Browse files Browse the repository at this point in the history
This patch offers an alternative approach to
5d492c3. Instead of manually setting
up the directory #testCalculateFileSizeInPath needs and then
recursively deleting it, it uses JUnit's TemporaryFolder Rule to
handle all of this work, and allows the code to concentrate on the
business logic.

Closes #7418
  • Loading branch information
mureinik authored Nov 9, 2023
1 parent 70dc93a commit 27759f3
Showing 1 changed file with 36 additions and 50 deletions.
86 changes: 36 additions & 50 deletions common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class UtilAllTest {
@Rule
public TemporaryFolder tempDir = new TemporaryFolder();

@Test
public void testCurrentStackTrace() {
Expand Down Expand Up @@ -236,56 +241,37 @@ public void testCalculateFileSizeInPath() throws Exception {
* - file_1_2_0
* - dir_2
*/
String basePath = System.getProperty("java.io.tmpdir") + File.separator + "testCalculateFileSizeInPath";
File baseFile = new File(basePath);
try {
// test empty path
assertEquals(0, UtilAll.calculateFileSizeInPath(baseFile));

// create baseDir
assertTrue(baseFile.mkdirs());

File file0 = new File(baseFile, "file_0");
assertTrue(file0.createNewFile());
writeFixedBytesToFile(file0, 1313);

assertEquals(1313, UtilAll.calculateFileSizeInPath(baseFile));

// build a file tree like above
File dir1 = new File(baseFile, "dir_1");
dir1.mkdirs();
File file10 = new File(dir1, "file_1_0");
File file11 = new File(dir1, "file_1_1");
File dir12 = new File(dir1, "dir_1_2");
dir12.mkdirs();
File file120 = new File(dir12, "file_1_2_0");
File dir2 = new File(baseFile, "dir_2");
dir2.mkdirs();

// write all file with 1313 bytes data
assertTrue(file10.createNewFile());
writeFixedBytesToFile(file10, 1313);
assertTrue(file11.createNewFile());
writeFixedBytesToFile(file11, 1313);
assertTrue(file120.createNewFile());
writeFixedBytesToFile(file120, 1313);

assertEquals(1313 * 4, UtilAll.calculateFileSizeInPath(baseFile));
} finally {
deleteFolder(baseFile);
}
}

public static void deleteFolder(File folder) {
if (folder.isDirectory()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
deleteFolder(file);
}
}
}
folder.delete();
File baseFile = tempDir.getRoot();

// test empty path
assertEquals(0, UtilAll.calculateFileSizeInPath(baseFile));

File file0 = new File(baseFile, "file_0");
assertTrue(file0.createNewFile());
writeFixedBytesToFile(file0, 1313);

assertEquals(1313, UtilAll.calculateFileSizeInPath(baseFile));

// build a file tree like above
File dir1 = new File(baseFile, "dir_1");
dir1.mkdirs();
File file10 = new File(dir1, "file_1_0");
File file11 = new File(dir1, "file_1_1");
File dir12 = new File(dir1, "dir_1_2");
dir12.mkdirs();
File file120 = new File(dir12, "file_1_2_0");
File dir2 = new File(baseFile, "dir_2");
dir2.mkdirs();

// write all file with 1313 bytes data
assertTrue(file10.createNewFile());
writeFixedBytesToFile(file10, 1313);
assertTrue(file11.createNewFile());
writeFixedBytesToFile(file11, 1313);
assertTrue(file120.createNewFile());
writeFixedBytesToFile(file120, 1313);

assertEquals(1313 * 4, UtilAll.calculateFileSizeInPath(baseFile));
}

private void writeFixedBytesToFile(File file, int size) throws Exception {
Expand Down

0 comments on commit 27759f3

Please sign in to comment.