|
| 1 | +package fr.opensagres.xdocreport.document.textstyling.html; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.StandardOpenOption; |
| 8 | +import java.util.logging.Logger; |
| 9 | + |
| 10 | +import fr.opensagres.xdocreport.core.document.SyntaxKind; |
| 11 | +import fr.opensagres.xdocreport.core.logging.LogUtils; |
| 12 | +import fr.opensagres.xdocreport.document.IXDocReport; |
| 13 | +import fr.opensagres.xdocreport.document.registry.XDocReportRegistry; |
| 14 | +import fr.opensagres.xdocreport.template.FieldExtractor; |
| 15 | +import fr.opensagres.xdocreport.template.FieldsExtractor; |
| 16 | +import fr.opensagres.xdocreport.template.IContext; |
| 17 | +import fr.opensagres.xdocreport.template.TemplateEngineKind; |
| 18 | +import fr.opensagres.xdocreport.template.formatter.FieldsMetadata; |
| 19 | +import junit.framework.TestCase; |
| 20 | + |
| 21 | +public class HTMLTextStylingContentHandlerTestCase extends TestCase { |
| 22 | + private static final Logger LOGGER = LogUtils.getLogger(HTMLTextStylingContentHandlerTestCase.class.getName()); |
| 23 | + |
| 24 | + public static final String HTML_WITH_TH = "<h2>Análise de Sentimento das Respostas:</h2>\n" |
| 25 | + + "<p><strong>1. Identificação de Palavras Mais Recorrentes:</strong></p>\n" + "<p>As palavras mais recorrentes nas respostas são:</p>\n" |
| 26 | + + "<ul>\n" |
| 27 | + + "<li><strong>"Nada a declarar"</strong>: Apresenta-se como um padrão, indicando a ausência de algo a ser dito.</li>\n" |
| 28 | + + "<li><strong>"Teste com o Welkey que..."</strong>: Indica uma tentativa de iniciar ou completar a tarefa.</li>\n" |
| 29 | + + "<li><strong>"Parece que tudo está bem"</strong>: Expressa uma sensação geral positiva.</li>\n" + "</ul>\n" |
| 30 | + + "<p><strong>2. Classificação das Respostas:</strong></p>\n" + "<table>\n" + "<thead>\n" + "<tr>\n" + "<th>Classificação</th>\n" |
| 31 | + + "<th>Número de Respostas</th>\n" + "<th>Porcentagem (%)</th>\n" + "</tr>\n" + "</thead>\n" + "<tbody>\n" + "<tr>\n" |
| 32 | + + "<td>Positiva</td>\n" + "<td>10</td>\n" + "<td>50%</td>\n" + "</tr>\n" + "<tr>\n" + "<td>Negativa</td>\n" + "<td>6</td>\n" |
| 33 | + + "<td>30%</td>\n" + "</tr>\n" + "<tr>\n" + "<td>Neutra</td>\n" + "<td>4</td>\n" + "<td>20%</td>\n" + "</tr>\n" + "</tbody>\n" |
| 34 | + + "</table>\n" + "<p><strong>3. Cálculo dos Percentuais:</strong></p>\n" + "<ul>\n" |
| 35 | + + "<li><strong>Positiva:</strong> 50% das respostas expressam uma sensação positiva, com foco na ausência de algo a declarar e na sensação geral de bem-estar.</li>\n" |
| 36 | + + "<li><strong>Negativa:</strong> 30% das respostas apresentam um tom negativo, com foco em falhas ou limitações.</li>\n" |
| 37 | + + "<li><strong>Neutra:</strong> 20% das respostas são neutras, sem uma predominância clara de termos positivos ou negativos.</li>\n" |
| 38 | + + "</ul>\n" + "<p><strong>4. Apresentação dos Percentuais:</strong></p>\n" + "<ul>\n" + "<li><strong>Positiva: 50%</strong></li>\n" |
| 39 | + + "<li><strong>Negativa: 30%</strong></li>\n" + "<li><strong>Neutra: 20%</strong></li>\n" + "</ul>\n" |
| 40 | + + "<p><strong>Observações:</strong></p>\n" |
| 41 | + + "<p>A análise do sentimento das respostas mostra uma tendência predominante de neutra, com a ausência de algo a declarar e a sensação geral de bem-estar. A presença de palavras como "Teste com o Welkey que..." sugere um contexto de tarefa ou atividade específica.</p>\n" |
| 42 | + + ""; |
| 43 | + |
| 44 | + /** |
| 45 | + * Test for issue 701 - https://github.com/opensagres/xdocreport/issues/701 |
| 46 | + * |
| 47 | + * @throws Exception |
| 48 | + */ |
| 49 | + public void testfieldWithHtmlTagTh() throws Exception { |
| 50 | + try (var inputStream = HTMLTextStylingContentHandlerTestCase.class.getResourceAsStream("template_th.odt")) { |
| 51 | + |
| 52 | + IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker); |
| 53 | + |
| 54 | + inputStream.close(); |
| 55 | + FieldsMetadata fieldsMetadata = report.createFieldsMetadata(); |
| 56 | + String fieldName = "html"; |
| 57 | + fieldsMetadata.addFieldAsTextStyling(fieldName, SyntaxKind.Html, true); |
| 58 | + |
| 59 | + IContext context = report.createContext(); |
| 60 | + context.put(fieldName, HTML_WITH_TH); |
| 61 | + |
| 62 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 63 | + report.process(context, bos); |
| 64 | + Path outputPath = Path.of(System.getProperty("java.io.tmpdir"), "newTest-" + System.currentTimeMillis() + ".odt"); |
| 65 | + Files.write(outputPath, bos.toByteArray(), StandardOpenOption.CREATE_NEW); |
| 66 | + |
| 67 | + // Try to open generated ODT |
| 68 | + try { |
| 69 | + var result = new FileInputStream(outputPath.toFile()); |
| 70 | + var resultReport = XDocReportRegistry.getRegistry().loadReport(result, TemplateEngineKind.Freemarker); |
| 71 | + resultReport.extractFields(new FieldsExtractor<FieldExtractor>()); |
| 72 | + } catch (Exception ex) { |
| 73 | + fail("The generated document should be parseable: " + ex.getMessage()); |
| 74 | + } |
| 75 | + LOGGER.fine("Output file saved on " + outputPath.toAbsolutePath().toString()); |
| 76 | + |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments