some refactoring and a new test for URLContentLoaderTest

main
Daniel Fromm 2025-05-13 00:16:20 +02:00
parent 006c5525c3
commit 8a74b2a7b5
5 changed files with 96 additions and 21 deletions

View File

@ -1,5 +1,6 @@
package domain; package domain;
import org.apache.lucene.analysis.de.GermanAnalyzer;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
@ -109,8 +110,8 @@ public class TextProcessing {
if (text == null || text.isBlank()) { if (text == null || text.isBlank()) {
return words; return words;
} }
CharArraySet luceneStopwords = CharArraySet luceneStopwords = stopwordList != null ? new CharArraySet(stopwordList,
stopwordList != null ? new CharArraySet(stopwordList, true) : CharArraySet.EMPTY_SET; true) : CharArraySet.EMPTY_SET;
try (Analyzer analyzer = new StandardAnalyzer(luceneStopwords)) { try (Analyzer analyzer = new StandardAnalyzer(luceneStopwords)) {
TokenStream tokenStream = analyzer.tokenStream(null, text); TokenStream tokenStream = analyzer.tokenStream(null, text);
@ -138,22 +139,6 @@ public class TextProcessing {
return words; return words;
} }
// public Map<String, Integer> stemming(Map<String, Integer> wordList) {
// Map<String, Integer> 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) { public void setStemming(boolean stemming) {
this.stemming = stemming; this.stemming = stemming;
} }

View File

@ -49,7 +49,6 @@ public class WordCloudCreator {
// Write the updated HTML to the output file // Write the updated HTML to the output file
writer.write(updatedHtml); writer.write(updatedHtml);
System.out.println("Output file 'output.html' created successfully!");
return true; return true;
} catch (IOException e) { } catch (IOException e) {

View File

@ -62,7 +62,7 @@ public class TUI {
public void fileMenu() { public void fileMenu() {
while(fMenu) { while(fMenu) {
System.out.println("(0) Load Stopwords\n(1) Add to Stopwords\n(2) Set Max Words in HTML\n" + 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()); option = Integer.parseInt(scan.nextLine());
switch(option) { switch(option) {
case (0): case (0):

View File

@ -1,5 +1,7 @@
import domain.FileLoader; import domain.FileLoader;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import javax.swing.*;
import java.io.File; import java.io.File;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -20,4 +22,50 @@ class FileLoaderTest {
FileLoader loader = new FileLoader(); FileLoader loader = new FileLoader();
assertEquals("File format not supported", loader.getFileFormat(new File("test.xyz"))); 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());
}
} }

View File

@ -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.");
}
}