Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions components/api/api-modules-javascript/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -13,7 +13,7 @@
<name>Components - API - Modules</name>
<artifactId>dirigible-components-api-modules-javascript</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.webjars</groupId>
Expand All @@ -26,6 +26,10 @@
<artifactId>stomp__stompjs</artifactId>
<version>${stompjs.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>

<profiles>
Expand Down Expand Up @@ -79,4 +83,4 @@
<parent.pom.folder>../../../</parent.pom.folder>
</properties>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,120 @@
* ### Key Features:
* - **Test Definition**: The `test` function allows developers to define individual test cases with a descriptive name and a function containing the test logic.
* - **Assertions**: Functions such as `assertEquals`, `assertNotEquals`, `assertTrue`, `assertFalse`, and `fail` provide a variety of assertion methods to validate conditions and compare values within tests.
* - **Result Tracking**: Test results are automatically captured and can be retrieved via the results API.
*
* ### Use Cases:
* - **Unit Testing**: These utilities are primarily used for writing unit tests to verify the functionality of individual components or functions in isolation.
* - **Integration Testing**: They can also be used in integration tests to validate the behavior of multiple components working together, ensuring that they interact correctly.
*
* ### Example Usage:
* ```ts
* import { test, assertEquals, assertTrue } from "@aerokit/sdk/junit";
* import { test, assertEquals, assertTrue, storeResults } from "@aerokit/sdk/junit";
*
* test("should add two numbers correctly", () => {
* const result = add(2, 3);
* const result = 2 + 3;
* assertEquals(5, result);
* });
*
* test("should return true for valid input", () => {
* const isValid = validateInput("valid input");
* const isValid = false; // Replace with actual validation logic
* assertTrue(isValid);
* });
*
* // After running tests, store results for later retrieval
* storeResults();
* ```
*/

const Assert = Java.type('org.junit.Assert');
import { Response } from '@aerokit/sdk/http';

const Assertions = Java.type('org.junit.jupiter.api.Assertions');
const TestResultsService = Java.type('org.eclipse.dirigible.components.ide.junit.service.TestResultsService');
const ArrayList = Java.type('java.util.ArrayList');
const TestResult = Java.type('org.eclipse.dirigible.components.ide.junit.domain.TestResult');

// Test Result Tracking
interface TestResult {
name: string;
status: 'passed' | 'failed' | 'skipped';
duration: number;
error?: string;
stackTrace?: string;
timestamp: number;
}

class TestResultCollector {
private results: TestResult[] = [];
private currentTest: { name: string; startTime: number } | null = null;
private haveFailedTests: boolean = false;

public hasFailedTests(): boolean {
return this.haveFailedTests;
}

public recordTestStart(name: string): void {
this.currentTest = { name, startTime: new Date().getTime() };
}

public recordTestComplete(status: 'passed' | 'failed' | 'skipped', error?: { message: string; stackTrace?: string }): void {
if (!this.currentTest) return;

const result: TestResult = {
name: this.currentTest.name,
status,
duration: new Date().getTime() - this.currentTest.startTime,
timestamp: new Date().getTime(),
};

if (error) {
result.error = error.message;
result.stackTrace = error.stackTrace;
}

this.results.push(result);
this.currentTest = null;

if (status === 'failed' || error) {
this.haveFailedTests = true;
}
}

public getResults(): TestResult[] {
return [...this.results];
}

public storeResults(): void {
const resultsList = new ArrayList();
this.results.forEach(e => resultsList.add(new TestResult(e.name, e.status, e.duration, e.error, e.stackTrace, e.timestamp)));
TestResultsService.get().storeResults(resultsList);
Response.sendRedirect('/services/web/ide-junit-results/junit-results');
}

public getSummary(): {
total: number;
passed: number;
failed: number;
skipped: number;
duration: number;
} {
const results = this.results;
const total = results.length;
const passed = results.filter(r => r.status === 'passed').length;
const failed = results.filter(r => r.status === 'failed').length;
const skipped = results.filter(r => r.status === 'skipped').length;
const duration = results.reduce((sum, r) => sum + r.duration, 0);

return { total, passed, failed, skipped, duration };
}

public clearResults(): void {
this.results = [];
this.currentTest = null;
this.haveFailedTests = false;
}
}

const resultCollector = new TestResultCollector();

/**
* Defines a test case.
Expand All @@ -39,8 +131,21 @@ const Assert = Java.type('org.junit.Assert');
* @param testFn The function containing the test logic and assertions.
*/
export function test(name: string, testFn: () => void) {
// Calls the global test runner function provided by the SDK environment.
(globalThis as any).test(name, testFn);
resultCollector.recordTestStart(name);
if (resultCollector.hasFailedTests()) {
resultCollector.recordTestComplete('skipped');
console.log(`- Skipping test "${name}" due to previous failures`);
} else {
try {
testFn();
resultCollector.recordTestComplete('passed');
} catch (error: any) {
resultCollector.recordTestComplete('failed', {
message: error?.message || String(error),
stackTrace: error?.stack,
});
}
}
}

/**
Expand All @@ -55,9 +160,9 @@ export function assertEquals<T>(expected: T, actual: T): void
export function assertEquals<T>(message: string, expected: T, actual: T): void
export function assertEquals<T>(messageOrExpected?: string | T, expectedOrActual?: T, actualOrUndefined?: T): void {
if (arguments.length === 3) {
Assert.assertEquals(messageOrExpected, expectedOrActual, actualOrUndefined);
Assertions.assertEquals(messageOrExpected, expectedOrActual, actualOrUndefined);
} else {
Assert.assertEquals(messageOrExpected, expectedOrActual);
Assertions.assertEquals(messageOrExpected, expectedOrActual);
}
}

Expand All @@ -73,9 +178,9 @@ export function assertNotEquals<T>(unexpected: T, actual: T): void
export function assertNotEquals<T>(message: string, unexpected: T, actual: T): void
export function assertNotEquals<T>(messageOrUnexpected?: string | T, unexpectedOrActual?: T, actualOrUndefined?: T): void {
if (arguments.length === 3) {
Assert.assertNotEquals(messageOrUnexpected, unexpectedOrActual, actualOrUndefined);
Assertions.assertNotEquals(messageOrUnexpected, unexpectedOrActual, actualOrUndefined);
} else {
Assert.assertNotEquals(messageOrUnexpected, unexpectedOrActual);
Assertions.assertNotEquals(messageOrUnexpected, unexpectedOrActual);
}
}

Expand All @@ -89,9 +194,9 @@ export function assertTrue(condition: boolean): void
export function assertTrue(message: string, condition: boolean): void
export function assertTrue(messageOrCondition?: string | boolean, conditionOrUndefined?: boolean): void {
if (arguments.length === 2) {
Assert.assertTrue(messageOrCondition, conditionOrUndefined);
Assertions.assertTrue(messageOrCondition, conditionOrUndefined);
} else {
Assert.assertTrue(messageOrCondition);
Assertions.assertTrue(messageOrCondition);
}
}

Expand All @@ -105,9 +210,9 @@ export function assertFalse(condition: boolean): void
export function assertFalse(message: string, condition: boolean): void
export function assertFalse(messageOrCondition?: string | boolean, conditionOrUndefined?: boolean): void {
if (arguments.length === 2) {
Assert.assertFalse(messageOrCondition, conditionOrUndefined);
Assertions.assertFalse(messageOrCondition, conditionOrUndefined);
} else {
Assert.assertFalse(messageOrCondition);
Assertions.assertFalse(messageOrCondition);
}
}

Expand All @@ -120,8 +225,53 @@ export function fail(): void
export function fail(message: string): void
export function fail(message?: string): void {
if (message) {
Assert.fail(message);
Assertions.fail(message);
} else {
Assert.fail();
Assertions.fail();
}
}

/**
* Retrieves all test results collected during this session.
*
* @returns An array of test results.
*/
export function getResults(): Array<{
name: string;
status: 'passed' | 'failed' | 'skipped';
duration: number;
error?: string;
stackTrace?: string;
timestamp: number;
}> {
return resultCollector.getResults();
}

/**
* Retrieves a summary of test execution.
*
* @returns An object containing counts and total duration.
*/
export function getResultsSummary(): {
total: number;
passed: number;
failed: number;
skipped: number;
duration: number;
} {
return resultCollector.getSummary();
}

/**
* Clears all collected test results.
*/
export function clearResults(): void {
resultCollector.clearResults();
}

/**
* Clears all collected test results.
*/
export function storeResults(): void {
resultCollector.storeResults();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright (c) 2010-2026 Eclipse Dirigible contributors
*
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-FileCopyrightText: Eclipse Dirigible contributors SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.dirigible.components.data.export.service;

import static java.text.MessageFormat.format;
Expand Down
4 changes: 4 additions & 0 deletions components/group/group-ide/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-ide-git</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-ide-junit-results</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-ide-template</artifactId>
Expand Down
39 changes: 39 additions & 0 deletions components/ide/ide-junit-results/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-parent</artifactId>
<version>13.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<name>Components - IDE - JUnit Results</name>
<artifactId>dirigible-components-ide-junit-results</artifactId>
<packaging>jar</packaging>

<dependencies>

<!-- Components -->
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-core-base</artifactId>
</dependency>

<!-- API -->
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-api-security</artifactId>
</dependency>

</dependencies>

<properties>
<license.header.location>../../../licensing-header.txt</license.header.location>
<parent.pom.folder>../../../</parent.pom.folder>
</properties>

</project>
Loading
Loading