mirror of
https://github.com/bbasaran/kessel-run.git
synced 2026-10-15 10:59:19 -04:00
Add basic exporter
This commit is contained in:
@@ -35,3 +35,4 @@ out/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
*.lst
|
||||
|
||||
@@ -26,6 +26,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
testImplementation("junit:junit:4.13.1")
|
||||
compileOnly("org.projectlombok:lombok")
|
||||
annotationProcessor("org.projectlombok:lombok")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.misoenergy.hackday.kessel_run;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PCGenExporter {
|
||||
|
||||
public PCGenExporter() {
|
||||
}
|
||||
|
||||
public void export(File file) {
|
||||
try {
|
||||
createOutputFile(file);
|
||||
} catch (IOException e) {
|
||||
handleFailedOutput(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void createOutputFile(File file) throws IOException {
|
||||
if (file.createNewFile()) {
|
||||
System.out.println("File created: " + file.getName());
|
||||
} else {
|
||||
System.out.println("File already exists. Deleting and re-running");
|
||||
file.delete();
|
||||
createOutputFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleFailedOutput(Exception e) {
|
||||
System.out.println("An error occurred.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,12 @@ package org.misoenergy.hackday.kessel_run;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
|
||||
@SpringBootTest
|
||||
class KesselRunApplicationTests {
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.misoenergy.hackday.kessel_run;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PCGenExporterTest {
|
||||
|
||||
|
||||
PCGenExporter exporter;
|
||||
|
||||
@Test
|
||||
void exporterExists() {
|
||||
exporter = new PCGenExporter();
|
||||
}
|
||||
|
||||
@Test
|
||||
void exporterCreatesFileWhenNoneExists() {
|
||||
exporter = new PCGenExporter();
|
||||
File file = new File("sw5eClasses.lst");
|
||||
deleteFileIfExists(file);
|
||||
exporter.export(file);
|
||||
assertTrue(file.isFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exporterRecreatesFileWhenExists() throws IOException {
|
||||
exporter = new PCGenExporter();
|
||||
File file = new File("sw5eClasses.lst");
|
||||
|
||||
deleteFileIfExists(file);
|
||||
createFileWithData(file);
|
||||
|
||||
exporter.export(file);
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
|
||||
assertTrue(reader.readLine() == null);
|
||||
|
||||
}
|
||||
|
||||
void deleteFileIfExists(File file) {
|
||||
if(!file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
void createFileWithData(File file) throws IOException {
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write("If this exists when tested, Fail!");
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user