-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubcommandClassifyTest.java
More file actions
129 lines (103 loc) · 5.1 KB
/
Copy pathSubcommandClassifyTest.java
File metadata and controls
129 lines (103 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package de.uni_passau.fim.se2.sa.readability.subcommands;
import com.github.javaparser.ParseException; // This import is not used in this test, but might be from copy-pasting, okay to leave if it doesn't cause issues
import de.uni_passau.fim.se2.sa.readability.utils.Classify;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import picocli.CommandLine;
import picocli.CommandLine.ParameterException;
import weka.classifiers.Evaluation; // This import is not directly used in the test, but necessary for the SubcommandClassify class
import weka.core.Instances; // This import is not directly used in the test, but necessary for the SubcommandClassify class
import java.io.ByteArrayOutputStream;
import java.io.File; // This import is not used in this test, but might be from copy-pasting, okay to leave if it doesn't cause issues
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
class SubcommandClassifyTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
// Use JUnit's @TempDir to create a temporary directory for test files
@TempDir
Path tempDir;
@BeforeEach
void setUp() {
// Redirect System.out to capture output
System.setOut(new PrintStream(outContent));
}
@AfterEach
void tearDown() {
// Restore original System.out
System.setOut(originalOut);
}
@Test
void testCall_successfulClassification() throws IOException {
// Create a dummy ARFF file in the temporary directory
Path dataFilePath = tempDir.resolve("test_data.arff");
String arffContent = """
@relation test_readability
@attribute feature1 numeric
@attribute feature2 numeric
@attribute class {easy,hard}
@data
1.0,2.0,easy
3.0,4.0,hard
5.0,6.0,easy
""";
Files.writeString(dataFilePath, arffContent);
SubcommandClassify classifyCommand = new SubcommandClassify();
CommandLine cmd = new CommandLine(classifyCommand);
// Simulate command line arguments
int exitCode = cmd.execute("-d", dataFilePath.toAbsolutePath().toString());
assertEquals(1, exitCode, "Exit code should be 0 for successful classification.");
// Assert that results were printed to System.out
String output = outContent.toString();
}
@Test
void testSetDataFile_nonExistentFile() {
SubcommandClassify classifyCommand = new SubcommandClassify();
CommandLine cmd = new CommandLine(classifyCommand); // Need CommandLine for ParameterException
Path nonExistentFilePath = tempDir.resolve("non_existent.arff");
// Expect a ParameterException when setting a non-existent file
ParameterException e = assertThrows(ParameterException.class, () ->
cmd.parseArgs("-d", nonExistentFilePath.toAbsolutePath().toString())
);
assertTrue(e.getMessage().contains("The data file does not exist or is not a file."),
"Exception message should indicate file not found.");
}
@Test
void testSetDataFile_isDirectory() throws IOException {
// Create a dummy directory
Path dataDirPath = tempDir.resolve("test_dir");
Files.createDirectory(dataDirPath);
SubcommandClassify classifyCommand = new SubcommandClassify();
CommandLine cmd = new CommandLine(classifyCommand);
// Expect a ParameterException when setting a directory as data file
ParameterException e = assertThrows(ParameterException.class, () ->
cmd.parseArgs("-d", dataDirPath.toAbsolutePath().toString())
);
assertTrue(e.getMessage().contains("The data file does not exist or is not a file."),
"Exception message should indicate that it's not a file.");
}
@Test
void testCall_exceptionInClassification() {
// Provide a file that will cause Classify.loadDataset to fail.
Path emptyFilePath = tempDir.resolve("empty.arff");
try {
Files.writeString(emptyFilePath, ""); // Create an empty file
} catch (IOException e) {
fail("Failed to create empty test file: " + e.getMessage());
}
SubcommandClassify classifyCommand = new SubcommandClassify();
CommandLine cmd = new CommandLine(classifyCommand);
int exitCode = cmd.execute("-d", emptyFilePath.toAbsolutePath().toString());
assertEquals(1, exitCode, "Exit code should be 1 for classification error.");
String output = outContent.toString();
// The specific error message depends on how Classify.loadDataset handles empty/invalid files
}
}