Forráskód Böngészése

LatexConstants LatexDocumentStart

Andrew Grant 4 hónapja
szülő
commit
da191a731e

+ 19 - 0
src/main/java/scot/carricksoftware/grantswriter/constants/LatexConstants.java

@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.constants;
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class LatexConstants {
+
+    private LatexConstants() {
+        // to stop checkstyle complaining
+    }
+
+    public static final String DOCUMENT_START = "\\begin{document}";
+
+}

+ 1 - 3
src/main/java/scot/carricksoftware/grantswriter/controllers/TexController.java

@@ -7,13 +7,11 @@ package scot.carricksoftware.grantswriter.controllers;
 
 import org.springframework.ui.Model;
 
-import java.io.IOException;
-
 public interface TexController {
 
     @SuppressWarnings({"SameReturnValue", "unused"})
     String screen(Model model);
 
     @SuppressWarnings({"SameReturnValue", "unused"})
-    String start(Model model) throws IOException;
+    String start(Model model) throws Exception;
 }

+ 1 - 3
src/main/java/scot/carricksoftware/grantswriter/controllers/TexControllerImpl.java

@@ -17,8 +17,6 @@ import scot.carricksoftware.grantswriter.constants.ViewConstants;
 import scot.carricksoftware.grantswriter.files.WriterFiles;
 import scot.carricksoftware.grantswriter.writer.TexWriter;
 
-import java.io.IOException;
-
 import static java.util.Objects.isNull;
 
 @Controller
@@ -50,7 +48,7 @@ public class TexControllerImpl implements TexController {
 
     @PostMapping(MappingConstants.TEX)
     @Override
-    public String start(Model model) throws IOException {
+    public String start(Model model) throws Exception {
         logger.debug("FilesControllerImpl::start");
         texWriter.write(writerFiles.getLatexFileName());
 

+ 1 - 3
src/main/java/scot/carricksoftware/grantswriter/writer/TexWriter.java

@@ -5,8 +5,6 @@
 
 package scot.carricksoftware.grantswriter.writer;
 
-import java.io.IOException;
-
 public interface TexWriter {
-    void write(String filename) throws IOException;
+    void write(String filename) throws Exception;
 }

+ 7 - 4
src/main/java/scot/carricksoftware/grantswriter/writer/TexWriterImpl.java

@@ -6,21 +6,24 @@
 package scot.carricksoftware.grantswriter.writer;
 
 import org.springframework.stereotype.Component;
-
-import java.io.IOException;
+import scot.carricksoftware.grantswriter.writer.latex.LatexDocumentStart;
 
 @Component
 public class TexWriterImpl implements TexWriter {
 
     private final FileWriter fileWriter;
 
-    public TexWriterImpl(FileWriter fileWriter) {
+    private final LatexDocumentStart docStart;
+
+    public TexWriterImpl(FileWriter fileWriter, LatexDocumentStart docStart) {
         this.fileWriter = fileWriter;
+        this.docStart = docStart;
     }
 
     @Override
-    public void write(String filename) throws IOException {
+    public void write(String filename) throws Exception {
         fileWriter.init(filename);
+        docStart.write();
         fileWriter.close();
     }
 }

+ 11 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/latex/LatexDocumentStart.java

@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer.latex;
+
+public interface LatexDocumentStart {
+
+    void write();
+}

+ 25 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/latex/LatexDocumentStartImpl.java

@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer.latex;
+
+import org.springframework.stereotype.Component;
+import scot.carricksoftware.grantswriter.constants.LatexConstants;
+import scot.carricksoftware.grantswriter.writer.FileWriter;
+
+@Component
+public class LatexDocumentStartImpl implements LatexDocumentStart {
+
+    private final FileWriter fileWriter;
+
+    public LatexDocumentStartImpl(FileWriter fileWriter) {
+        this.fileWriter = fileWriter;
+    }
+
+    @Override
+    public void write() {
+        fileWriter.writeLine(LatexConstants.DOCUMENT_START);
+    }
+}

+ 5 - 1
src/test/java/scot/carricksoftware/grantswriter/writer/TexWriterTest.java

@@ -11,6 +11,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.InOrder;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
+import scot.carricksoftware.grantswriter.writer.latex.LatexDocumentStart;
 
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.inOrder;
@@ -24,9 +25,12 @@ class TexWriterTest {
     @Mock
     private FileWriter fileWriterMock;
 
+    @Mock
+    private LatexDocumentStart docStartMockMock;
+
     @BeforeEach
     void setUp() {
-        texWriter = new TexWriterImpl(fileWriterMock);
+        texWriter = new TexWriterImpl(fileWriterMock, docStartMockMock);
     }
 
     @Test

+ 34 - 0
src/test/java/scot/carricksoftware/grantswriter/writer/latex/LatexDocumentStartImplTest.java

@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer.latex;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import scot.carricksoftware.grantswriter.writer.FileWriter;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+@ExtendWith(SpringExtension.class)
+class LatexDocumentStartImplTest {
+
+    private LatexDocumentStart documentStart;
+
+    @Mock
+    private FileWriter fileWriterMock ;
+
+    @BeforeEach
+    void setUp() {
+        documentStart = new LatexDocumentStartImpl(fileWriterMock);
+    }
+
+    @Test
+    public void constructorTest() {
+        assertNotNull(documentStart);
+    }
+}