-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendDeveloperTests.java
More file actions
204 lines (166 loc) · 6.76 KB
/
Copy pathBackendDeveloperTests.java
File metadata and controls
204 lines (166 loc) · 6.76 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// --== CS400 Fall 2023 File Header Information ==--
// Name: Arnav Srivastav
// Email: asrivastav32@wisc.edu
// Group: C35
// TA: Alexander Peseckis
// Lecturer: Florian Heimerl
import org.junit.jupiter.api.Assertions;
import java.util.Scanner;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* JUnit tests for the Backend Developer's implementation of the Backend interface.
* These tests verify the functionality of methods related to reading and managing meteorite data.
*/
public class BackendDeveloperTests {
private BackendImplementation backend;
/**
* Test the accessor methods for the Meteorite class.
*/
@Test
public void testMeteoriteAccessors() {
// Create a sample Meteorite instance
Meteorite meteorite = new MeteoriteImpl("Sample Meteorite", 30.0, -75.0, true, 500.0);
// Test accessor methods
assertEquals("Sample Meteorite", meteorite.getName());
assertEquals(30.0, meteorite.getLatitude(), 0.01); // Allowing a small margin of error for double comparison
assertEquals(-75.0, meteorite.getLongitude(), 0.01);
assertTrue(meteorite.getFall()); // Use assertTrue for boolean getters
assertEquals(500.0, meteorite.getMass(), 0.01);
}
/**
* Test reading meteorite data from a file.
*/
@Test
public void testReadDataFromFile() {
BackendImplementation backend = new BackendImplementation();
String filePath = "meteorites.csv";
try {
backend.readDataFromFile(filePath);
// Check if the data has been loaded into meteoriteTree
assertFalse(backend.getMeteoriteTree().isEmpty());
} catch (Exception e) {
fail("Exception occurred: " + e.getMessage());
}
}
/**
* Test the insertion of meteorite data from a CSV file.
*/
@Test
public void testInsertFromCSV() {
BackendImplementation backend = new BackendImplementation();
String filePath = "meteorites.csv";
try {
boolean result = backend.insertFromCSV(filePath);
// Check if insertion was successful
assertTrue(result);
} catch (Exception e) {
fail("Exception occurred: " + e.getMessage());
}
}
/**
* Test retrieving meteorites with the maximum mass.
*/
@Test
public void testGetMeteoritesWithMaxMass() {
BackendImplementation backend = new BackendImplementation();
String filePath = "meteorites.csv";
try {
backend.readDataFromFile(filePath);
} catch (Exception e) {
fail("Exception occurred: " + e.getMessage());
}
// Call the method to retrieve meteorites with the highest mass
List<Meteorite> result = backend.getMeteoritesWithMaxMass();
// Check if the list is not empty
assertFalse(result.isEmpty());
}
/**
* Test retrieving meteorites within a specified mass range.
*/
@Test
public void testGetMeteoritesWithinMassRange() {
BackendImplementation backend = new BackendImplementation();
String filePath = "meteorites.csv";
try {
backend.readDataFromFile(filePath);
} catch (Exception e) {
fail("Exception occurred: " + e.getMessage());
}
double minMass = 0.0;
double maxMass = 100.0;
List<Meteorite> result = backend.getMeteoritesWithinMassRange(minMass, maxMass);
assertFalse(result.isEmpty());
for (Meteorite meteorite : result) {
double mass = meteorite.getMass();
assertTrue(mass >= minMass && mass <= maxMass);
}
}
/**
* Integration test for loading data from a file through the frontend.
* This test checks if the frontend properly calls the backend's readDataFromFile method.
*/
@Test
public void IntegrationTestLoadFile() {
TextUITester tester = new TextUITester("1\nmeteorite.csv\n4"); // Provided commands: Load file, File name, Exit
Scanner scnr = new Scanner(System.in);
BackendImplementation backend = new BackendImplementation();
FrontendInterface frontend = new FrontendImplementation(backend, scnr);
frontend.startCommandLoop();
String output = tester.checkOutput();
if (output.contains("Searching for file...")) {
Assertions.assertTrue(true);
} else {
Assertions.fail("Integration Test 1: Failed");
}
}
/**
* Integration test for listing meteorites within a specified mass range through the frontend.
* This test checks if the frontend properly calls the backend's getMeteoritesByMassRange method.
*/
@Test
public void IntegrationTestListAllRange() {
TextUITester tester = new TextUITester("3\n100.0\n200.0\n4"); // Provided commands: List range, Low threshold, High threshold, Exit
BackendImplementation backend = new BackendImplementation();
Scanner scnr = new Scanner(System.in);
FrontendInterface frontend = new FrontendImplementation(backend, scnr);
frontend.startCommandLoop();
String output = tester.checkOutput();
if (output.contains("Printing meteorites within range...")) {
Assertions.assertTrue(true);
} else {
Assertions.fail("Integration Test 2: Failed");
}
}
@Test
public void testLoadFileWithNonExistentFile() {
TextUITester tester = new TextUITester("1\nnonexistent.csv\n4");
Scanner scanner = new Scanner(System.in);
BackendImplementation backend = new BackendImplementation();
FrontendImplementation frontend = new FrontendImplementation(backend, scanner);
// Simulate user input with a non-existent file
frontend.startCommandLoop();
String output = tester.checkOutput();
// Verify that the code handles a non-existent file properly and provides an error message
if( output.contains("File not found")){
Assertions.assertTrue(true);
} else{
Assertions.fail("Frontend testing failed for this test.");
}
}
@Test
public void testListAllRangeWithInvalidRange() {
TextUITester tester = new TextUITester("3\n200.00\n200.00\n4\n100\n4");
Scanner scanner = new Scanner(System.in);
BackendImplementation backend = new BackendImplementation();
FrontendImplementation frontend = new FrontendImplementation(backend, scanner);
frontend.startCommandLoop();
String output = tester.checkOutput();
if( output.contains("Invalid range: try again")){
Assertions.assertTrue(true);
} else{
Assertions.fail("Frontend testing failed for this test.");
}
}
}