From 8a74b2a7b5a0fa1dc5b1a30412c0db6a26b68bbd Mon Sep 17 00:00:00 2001 From: Daniel Fromm <3015351@stud.hs-mannheim.de> Date: Tue, 13 May 2025 00:16:20 +0200 Subject: [PATCH] some refactoring and a new test for URLContentLoaderTest --- src/main/java/domain/TextProcessing.java | 21 ++------- src/main/java/domain/WordCloudCreator.java | 1 - src/main/java/tui/TUI.java | 2 +- src/test/java/FileLoaderTest.java | 50 +++++++++++++++++++++- src/test/java/URLContenLoaderTest.java | 43 +++++++++++++++++++ 5 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 src/test/java/URLContenLoaderTest.java diff --git a/src/main/java/domain/TextProcessing.java b/src/main/java/domain/TextProcessing.java index 0b14c05..3b7c695 100644 --- a/src/main/java/domain/TextProcessing.java +++ b/src/main/java/domain/TextProcessing.java @@ -1,5 +1,6 @@ package domain; +import org.apache.lucene.analysis.de.GermanAnalyzer; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.poi.xwpf.usermodel.XWPFDocument; @@ -109,8 +110,8 @@ public class TextProcessing { if (text == null || text.isBlank()) { return words; } - CharArraySet luceneStopwords = - stopwordList != null ? new CharArraySet(stopwordList, true) : CharArraySet.EMPTY_SET; + CharArraySet luceneStopwords = stopwordList != null ? new CharArraySet(stopwordList, + true) : CharArraySet.EMPTY_SET; try (Analyzer analyzer = new StandardAnalyzer(luceneStopwords)) { TokenStream tokenStream = analyzer.tokenStream(null, text); @@ -138,22 +139,6 @@ public class TextProcessing { return words; } -// public Map stemming(Map wordList) { -// Map wordCounts = new HashMap<>(); -// GermanStemmer stemmer = new GermanStemmer(); -// -// for (String key : wordList.keySet()) { -// char[] wordChars = key.toCharArray(); -// int length = stemmer.stem(wordChars, wordChars.length); // Stemming durchführen -// String stemmedWord = new String(wordChars, 0, length); // Gestemmtes Wort extrahieren -// -// if (stemmedWord != null && !stemmedWord.isBlank()) { -// wordCounts.merge(stemmedWord, wordList.get(key), Integer::sum); -// } -// } -// return wordCounts; -// } - public void setStemming(boolean stemming) { this.stemming = stemming; } diff --git a/src/main/java/domain/WordCloudCreator.java b/src/main/java/domain/WordCloudCreator.java index ab5fe9f..8c1da73 100644 --- a/src/main/java/domain/WordCloudCreator.java +++ b/src/main/java/domain/WordCloudCreator.java @@ -49,7 +49,6 @@ public class WordCloudCreator { // Write the updated HTML to the output file writer.write(updatedHtml); - System.out.println("Output file 'output.html' created successfully!"); return true; } catch (IOException e) { diff --git a/src/main/java/tui/TUI.java b/src/main/java/tui/TUI.java index 11ed149..08bd504 100644 --- a/src/main/java/tui/TUI.java +++ b/src/main/java/tui/TUI.java @@ -62,7 +62,7 @@ public class TUI { public void fileMenu() { while(fMenu) { System.out.println("(0) Load Stopwords\n(1) Add to Stopwords\n(2) Set Max Words in HTML\n" + - "(3) Stemming? (only German available)\n(4) Create WordCloud and Exit"); + "(3) Stemming not functioning!\n(4) Create WordCloud and Exit"); option = Integer.parseInt(scan.nextLine()); switch(option) { case (0): diff --git a/src/test/java/FileLoaderTest.java b/src/test/java/FileLoaderTest.java index aa93d0f..5644fe3 100644 --- a/src/test/java/FileLoaderTest.java +++ b/src/test/java/FileLoaderTest.java @@ -1,5 +1,7 @@ import domain.FileLoader; import org.junit.jupiter.api.Test; + +import javax.swing.*; import java.io.File; import static org.junit.jupiter.api.Assertions.*; @@ -20,4 +22,50 @@ class FileLoaderTest { FileLoader loader = new FileLoader(); assertEquals("File format not supported", loader.getFileFormat(new File("test.xyz"))); } -} + + @Test + void testInitialFilePathAndStopwordsPath() { + FileLoader loader = new FileLoader(); + assertNull(loader.getFilePath()); + assertNull(loader.getStopwordsPath()); + } + + @Test + void testInitialFileFormatAndStopwordFormat() { + FileLoader loader = new FileLoader(); + assertEquals("", loader.getFileFormat()); + assertEquals("", loader.getStopwordFormat()); + } + + @Test + void testLoadFileGUI_noFileSelected() { + FileLoader loader = new FileLoader(); + JFileChooser mockFileChooser = new JFileChooser() { + @Override + public int showOpenDialog(java.awt.Component parent) { + return JFileChooser.CANCEL_OPTION; // Simulate no file selected + } + }; + assertDoesNotThrow(() -> loader.loadFileGUI()); + assertNull(loader.getFilePath()); + } + + @Test + void testLoadFileGUI_fileSelected() { + FileLoader loader = new FileLoader(); + JFileChooser mockFileChooser = new JFileChooser() { + @Override + public int showOpenDialog(java.awt.Component parent) { + return JFileChooser.APPROVE_OPTION; // Simulate file selected + } + + @Override + public File getSelectedFile() { + return new File("test.txt"); // Simulate selected file + } + }; + assertDoesNotThrow(() -> loader.loadFileGUI()); + assertNotNull(loader.getFilePath()); + assertEquals("txt", loader.getFileFormat()); + } +} \ No newline at end of file diff --git a/src/test/java/URLContenLoaderTest.java b/src/test/java/URLContenLoaderTest.java new file mode 100644 index 0000000..8c82ea9 --- /dev/null +++ b/src/test/java/URLContenLoaderTest.java @@ -0,0 +1,43 @@ +import domain.URLContentLoader; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class URLContentLoaderTest { + private URLContentLoader urlContentLoader; + + @BeforeEach + void setUp() { + urlContentLoader = new URLContentLoader(); + } + + @Test + void testSetAndGetUrlPath() { + String testUrl = "http://example.com"; + urlContentLoader.setUrlPath(testUrl); + assertEquals(testUrl, urlContentLoader.getUrlPath(), "URL path should be set and retrieved correctly."); + } + + @Test + void testLoadURLContent_validUrl() { + urlContentLoader.setUrlPath("https://www.example.com"); + String content = urlContentLoader.loadURLContent(); + assertNotNull(content, "Content should not be null for a valid URL."); + assertFalse(content.isEmpty(), "Content should not be empty for a valid URL."); + } + + @Test + void testLoadURLContent_invalidUrl() { + urlContentLoader.setUrlPath("invalid-url"); + String content = urlContentLoader.loadURLContent(); + assertTrue(content.isEmpty(), "Content should be empty for an invalid URL."); + } + + @Test + void testLoadURLContent_nullUrl() { + urlContentLoader.setUrlPath(null); + String content = urlContentLoader.loadURLContent(); + assertTrue(content.isEmpty(), "Content should be empty when URL is null."); + } +} \ No newline at end of file