Amélioration du programme de test

This commit is contained in:
Moncef STITI 2025-03-02 16:18:59 +01:00
parent 8f79794ddc
commit d000eef590

@ -5,8 +5,6 @@ import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultCaret; import javax.swing.text.DefaultCaret;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import java.io.*;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
@ -55,76 +53,158 @@ public class BakeTestRunner extends JFrame {
createLogsDirectory(); createLogsDirectory();
} }
private void setupUI() { private void setupUI() {
mainPanel = new JPanel(new BorderLayout(10, 10)); mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
// Top panel with controls // Panel du haut avec contrôles modernisés
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5)); JPanel controlPanel = new JPanel();
runSelectedButton = new JButton("Run Selected Tests"); controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
runAllButton = new JButton("Run All Tests");
compareSelectedButton = new JButton("Compare Selected Test"); // Première ligne de contrôles
languageComboBox = new JComboBox<>(new String[]{"All", "C", "Java"}); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
openLogsButton = new JButton("Open Logs Directory"); runSelectedButton = new JButton("Run Selected Tests");
runAllButton = new JButton("Run All Tests");
controlPanel.add(runSelectedButton); compareSelectedButton = new JButton("Compare Selected Test");
controlPanel.add(runAllButton);
controlPanel.add(compareSelectedButton); // Styliser les boutons
controlPanel.add(new JLabel("Language:")); runSelectedButton.setBackground(new Color(70, 130, 180)); // Steel Blue
controlPanel.add(languageComboBox); runSelectedButton.setForeground(Color.WHITE);
controlPanel.add(openLogsButton); runSelectedButton.setFont(new Font("Dialog", Font.BOLD, 12));
// Table for test list runAllButton.setBackground(new Color(60, 179, 113)); // Medium Sea Green
String[] columnNames = {"#", "Language", "Test Name", "Status", "Last Run"}; runAllButton.setForeground(Color.WHITE);
tableModel = new DefaultTableModel(columnNames, 0) { runAllButton.setFont(new Font("Dialog", Font.BOLD, 12));
@Override
public boolean isCellEditable(int row, int column) { compareSelectedButton.setBackground(new Color(30, 144, 255)); // Dodger Blue
return false; compareSelectedButton.setForeground(Color.WHITE);
} compareSelectedButton.setFont(new Font("Dialog", Font.BOLD, 12));
};
testTable = new JTable(tableModel); buttonPanel.add(runSelectedButton);
testTable.getColumnModel().getColumn(0).setPreferredWidth(30); buttonPanel.add(runAllButton);
testTable.getColumnModel().getColumn(1).setPreferredWidth(70); buttonPanel.add(compareSelectedButton);
testTable.getColumnModel().getColumn(2).setPreferredWidth(300);
testTable.getColumnModel().getColumn(3).setPreferredWidth(80); // Deuxième ligne de contrôles
testTable.getColumnModel().getColumn(4).setPreferredWidth(150); JPanel filterPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
testTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); filterPanel.add(new JLabel("Language:"));
testTable.setRowHeight(25); languageComboBox = new JComboBox<>(new String[]{"All", "C", "Java"});
languageComboBox.setPreferredSize(new Dimension(100, 25));
// Log area
logArea = new JTextArea(); openLogsButton = new JButton("Open Logs Directory");
logArea.setEditable(false); openLogsButton.setBackground(new Color(211, 211, 211)); // Light Gray
logArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); openLogsButton.setFont(new Font("Dialog", Font.PLAIN, 12));
DefaultCaret caret = (DefaultCaret) logArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); filterPanel.add(languageComboBox);
filterPanel.add(Box.createHorizontalStrut(10));
// Progress bar filterPanel.add(openLogsButton);
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true); controlPanel.add(buttonPanel);
progressBar.setString("Ready"); controlPanel.add(Box.createVerticalStrut(5));
controlPanel.add(filterPanel);
// Layout
JSplitPane splitPane = new JSplitPane( // Tableau stylisé pour la liste des tests
JSplitPane.VERTICAL_SPLIT, String[] columnNames = {"#", "Language", "Test Name", "Status", "Last Run"};
new JScrollPane(testTable), tableModel = new DefaultTableModel(columnNames, 0) {
new JScrollPane(logArea) @Override
); public boolean isCellEditable(int row, int column) {
splitPane.setDividerLocation(300); return false;
}
mainPanel.add(controlPanel, BorderLayout.NORTH);
mainPanel.add(splitPane, BorderLayout.CENTER); @Override
mainPanel.add(progressBar, BorderLayout.SOUTH); public Class<?> getColumnClass(int column) {
if (column == 0) return Integer.class;
setContentPane(mainPanel); return String.class;
}
// Add action listeners };
runSelectedButton.addActionListener(e -> runSelectedTests());
runAllButton.addActionListener(e -> runAllTests()); testTable = new JTable(tableModel) {
compareSelectedButton.addActionListener(e -> compareSelectedTest()); @Override
openLogsButton.addActionListener(e -> openLogsDirectory()); public Component prepareRenderer(javax.swing.table.TableCellRenderer renderer, int row, int column) {
languageComboBox.addActionListener(e -> filterTestsByLanguage()); Component comp = super.prepareRenderer(renderer, row, column);
}
// Alterner les couleurs des lignes
if (!comp.getBackground().equals(getSelectionBackground())) {
String status = (String) getValueAt(row, 3);
if ("PASSED".equals(status)) {
comp.setBackground(PASSED_COLOR);
} else if ("FAILED".equals(status)) {
comp.setBackground(FAILED_COLOR);
} else if ("RUNNING".equals(status)) {
comp.setBackground(RUNNING_COLOR);
} else {
comp.setBackground(row % 2 == 0 ? new Color(240, 240, 250) : Color.WHITE);
}
}
return comp;
}
};
testTable.getColumnModel().getColumn(0).setPreferredWidth(30);
testTable.getColumnModel().getColumn(0).setMaxWidth(50);
testTable.getColumnModel().getColumn(1).setPreferredWidth(70);
testTable.getColumnModel().getColumn(1).setMaxWidth(100);
testTable.getColumnModel().getColumn(2).setPreferredWidth(300);
testTable.getColumnModel().getColumn(3).setPreferredWidth(80);
testTable.getColumnModel().getColumn(3).setMaxWidth(120);
testTable.getColumnModel().getColumn(4).setPreferredWidth(150);
testTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
testTable.setRowHeight(28);
testTable.setShowGrid(false);
testTable.setIntercellSpacing(new Dimension(0, 0));
testTable.getTableHeader().setFont(new Font("Dialog", Font.BOLD, 12));
testTable.getTableHeader().setOpaque(false);
testTable.getTableHeader().setBackground(new Color(240, 240, 240));
testTable.setFont(new Font("Dialog", Font.PLAIN, 12));
// Zone de logs améliorée
logArea = new JTextArea();
logArea.setEditable(false);
logArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
logArea.setBackground(new Color(250, 250, 250));
logArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
DefaultCaret caret = (DefaultCaret) logArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JPanel logPanel = new JPanel(new BorderLayout());
logPanel.add(new JLabel(" Test Output:"), BorderLayout.NORTH);
logPanel.add(new JScrollPane(logArea), BorderLayout.CENTER);
// Barre de progression stylisée
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
progressBar.setString("Ready");
progressBar.setFont(new Font("Dialog", Font.BOLD, 11));
progressBar.setForeground(new Color(46, 139, 87)); // Sea Green
// Layout
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
new JScrollPane(testTable),
logPanel
);
splitPane.setDividerLocation(300);
splitPane.setContinuousLayout(true);
splitPane.setDividerSize(5);
mainPanel.add(controlPanel, BorderLayout.NORTH);
mainPanel.add(splitPane, BorderLayout.CENTER);
JPanel statusPanel = new JPanel(new BorderLayout());
statusPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
statusPanel.add(progressBar, BorderLayout.CENTER);
mainPanel.add(statusPanel, BorderLayout.SOUTH);
setContentPane(mainPanel);
// Add action listeners
runSelectedButton.addActionListener(e -> runSelectedTests());
runAllButton.addActionListener(e -> runAllTests());
compareSelectedButton.addActionListener(e -> compareSelectedTest());
openLogsButton.addActionListener(e -> openLogsDirectory());
languageComboBox.addActionListener(e -> filterTestsByLanguage());
}
private void createLogsDirectory() { private void createLogsDirectory() {
try { try {
Files.createDirectories(Paths.get(logsDir)); Files.createDirectories(Paths.get(logsDir));
@ -169,74 +249,130 @@ public class BakeTestRunner extends JFrame {
showComparisonDialog(logFile, language, testName); showComparisonDialog(logFile, language, testName);
} }
private void showComparisonDialog(File logFile, String language, String testName) { private void showComparisonDialog(File logFile, String language, String testName) {
JDialog dialog = new JDialog(this, "Comparison: " + language + " - " + testName, true); JDialog dialog = new JDialog(this, "Comparison: " + language + " - " + testName, true);
dialog.setLayout(new BorderLayout(10, 10)); dialog.setLayout(new BorderLayout(10, 10));
dialog.setSize(1000, 600); dialog.setSize(1000, 600);
dialog.setLocationRelativeTo(this); dialog.setLocationRelativeTo(this);
try { try {
List<String> lines = Files.readAllLines(logFile.toPath()); List<String> lines = Files.readAllLines(logFile.toPath());
String content = String.join("\n", lines); String content = String.join("\n", lines);
// Split content to make and bake sections if possible // Split content to make and bake sections if possible
String makeOutput = ""; String makeOutput = "";
String bakeOutput = ""; String bakeOutput = "";
String comparisonResults = "";
// Basic parsing - can be enhanced for better splitting
int makeIndex = content.indexOf("=== Make Output ==="); // Better parsing - enhanced for clearer splitting
int bakeIndex = content.indexOf("=== Bake Output ==="); int makeIndex = content.indexOf("=== Make Output ===");
int comparisonIndex = content.indexOf("=== Comparison Results ==="); int bakeIndex = content.indexOf("=== Bake Output ===");
int comparisonIndex = content.indexOf("=== Comparison Results ===");
if (makeIndex != -1 && bakeIndex != -1) {
makeOutput = content.substring(makeIndex, bakeIndex).trim(); if (makeIndex != -1 && bakeIndex != -1) {
if (comparisonIndex != -1) { makeOutput = content.substring(makeIndex, bakeIndex).trim();
bakeOutput = content.substring(bakeIndex, comparisonIndex).trim(); if (comparisonIndex != -1) {
} else { bakeOutput = content.substring(bakeIndex, comparisonIndex).trim();
bakeOutput = content.substring(bakeIndex).trim(); comparisonResults = content.substring(comparisonIndex).trim();
} } else {
} bakeOutput = content.substring(bakeIndex).trim();
}
JTextArea makeArea = new JTextArea(makeOutput); }
JTextArea bakeArea = new JTextArea(bakeOutput);
JTextArea comparisonArea = new JTextArea(); JTextPane makeArea = createStyledTextPane(makeOutput);
JTextPane bakeArea = createStyledTextPane(bakeOutput);
if (comparisonIndex != -1) { JTextPane comparisonArea = createStyledTextPane(
comparisonArea.setText(content.substring(comparisonIndex).trim()); comparisonIndex != -1 ? comparisonResults : "No comparison results available."
} else { );
comparisonArea.setText("No comparison results available.");
} JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Make Output", new JScrollPane(makeArea));
makeArea.setEditable(false); tabbedPane.addTab("Bake Output", new JScrollPane(bakeArea));
bakeArea.setEditable(false); tabbedPane.addTab("Comparison", new JScrollPane(comparisonArea));
comparisonArea.setEditable(false); tabbedPane.setFont(new Font("Dialog", Font.PLAIN, 12));
makeArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); // Détection et mise en évidence des différences
bakeArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); highlightDifferences(makeArea, bakeArea);
comparisonArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
dialog.add(tabbedPane, BorderLayout.CENTER);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Make Output", new JScrollPane(makeArea)); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
tabbedPane.addTab("Bake Output", new JScrollPane(bakeArea)); JButton closeButton = new JButton("Close");
tabbedPane.addTab("Comparison", new JScrollPane(comparisonArea)); closeButton.setFont(new Font("Dialog", Font.BOLD, 12));
closeButton.addActionListener(e -> dialog.dispose());
dialog.add(tabbedPane, BorderLayout.CENTER); buttonPanel.add(closeButton);
JButton closeButton = new JButton("Close"); dialog.add(buttonPanel, BorderLayout.SOUTH);
closeButton.addActionListener(e -> dialog.dispose()); dialog.setVisible(true);
JPanel buttonPanel = new JPanel(); } catch (IOException e) {
buttonPanel.add(closeButton); JOptionPane.showMessageDialog(dialog,
dialog.add(buttonPanel, BorderLayout.SOUTH); "Error reading log file: " + e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
dialog.setVisible(true); }
}
} catch (IOException e) {
JOptionPane.showMessageDialog(dialog, // Méthode pour créer un JTextPane stylisé
"Error reading log file: " + e.getMessage(), private JTextPane createStyledTextPane(String content) {
"Error", JOptionPane.ERROR_MESSAGE); JTextPane textPane = new JTextPane();
} textPane.setEditable(false);
textPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
textPane.setText(content);
textPane.setMargin(new Insets(10, 10, 10, 10));
textPane.setBackground(new Color(252, 252, 252));
// Appliquer une coloration syntaxique basique
highlightKeywords(textPane);
return textPane;
}
// Méthode pour mettre en évidence les mots clés
private void highlightKeywords(JTextPane textPane) {
// Obtenez le document et le kit de style
javax.swing.text.StyledDocument doc = textPane.getStyledDocument();
javax.swing.text.Style defaultStyle = javax.swing.text.StyleContext.getDefaultStyleContext()
.getStyle(javax.swing.text.StyleContext.DEFAULT_STYLE);
// Créez différents styles pour différents types de texte
javax.swing.text.Style errorStyle = doc.addStyle("errorStyle", defaultStyle);
javax.swing.text.StyleConstants.setForeground(errorStyle, new Color(220, 0, 0));
javax.swing.text.StyleConstants.setBold(errorStyle, true);
javax.swing.text.Style successStyle = doc.addStyle("successStyle", defaultStyle);
javax.swing.text.StyleConstants.setForeground(successStyle, new Color(0, 128, 0));
javax.swing.text.StyleConstants.setBold(successStyle, true);
// Appliquez les styles aux mots-clés
String text = textPane.getText();
applyStyle(doc, text, "Error", errorStyle);
applyStyle(doc, text, "error", errorStyle);
applyStyle(doc, text, "ERROR", errorStyle);
applyStyle(doc, text, "failed", errorStyle);
applyStyle(doc, text, "FAILED", errorStyle);
applyStyle(doc, text, "PASSED", successStyle);
applyStyle(doc, text, "passed", successStyle);
applyStyle(doc, text, "successful", successStyle);
}
// Méthode auxiliaire pour appliquer un style à un mot-clé spécifique
private void applyStyle(javax.swing.text.StyledDocument doc, String text, String pattern, javax.swing.text.Style style) {
int pos = 0;
while ((pos = text.indexOf(pattern, pos)) >= 0) {
doc.setCharacterAttributes(pos, pattern.length(), style, true);
pos += pattern.length();
} }
}
// Méthode pour mettre en évidence les différences entre les sorties make et bake
private void highlightDifferences(JTextPane makeArea, JTextPane bakeArea) {
// Cette méthode devrait idéalement comparer les lignes et mettre en évidence les différences
// Pour une première implémentation simple, nous allons juste colorer les lignes contenant des erreurs
highlightKeywords(makeArea);
highlightKeywords(bakeArea);
}
private void loadTests() { private void loadTests() {
tableModel.setRowCount(0); tableModel.setRowCount(0);