Skip to content

Commit 8cbe6cf

Browse files
Add unit tests
Generated by Claude Sonnet 4.5, manually fixed by me. Change-Id: Ieec23f0b0f783775a166b530c9c5c75bc0190c53 Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
1 parent b6481f5 commit 8cbe6cf

File tree

5 files changed

+340
-0
lines changed

5 files changed

+340
-0
lines changed

ctf/org.eclipse.tracecompass.ctf.core.tests/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Export-Package: org.eclipse.tracecompass.ctf.core.tests;x-friends:="org.eclipse.
2424
org.eclipse.tracecompass.ctf.core.tests.trace;x-internal:=true,
2525
org.eclipse.tracecompass.ctf.core.tests.types;x-internal:=true
2626
Import-Package: com.google.common.collect,
27+
com.google.gson,
2728
org.antlr.runtime;version="3.2.0",
2829
org.apache.commons.io,
2930
org.eclipse.test.performance,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
10+
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
import static org.junit.Assert.assertTrue;
16+
17+
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
18+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
19+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
20+
import org.junit.Test;
21+
22+
import com.google.gson.JsonArray;
23+
import com.google.gson.JsonObject;
24+
import com.google.gson.JsonPrimitive;
25+
26+
/**
27+
* Integration test class for CTF2 parsing functionality
28+
*/
29+
public class CTF2IntegrationTest {
30+
31+
/**
32+
* Test parsing integer with mappings for enumeration-like behavior
33+
*
34+
* @throws Exception if parsing fails
35+
*/
36+
@Test
37+
public void testIntegerWithMappings() throws Exception {
38+
CTFTrace trace = new CTFTrace();
39+
40+
JsonObject fieldClass = new JsonObject();
41+
fieldClass.add("type", new JsonPrimitive("fixed-length-unsigned-integer"));
42+
fieldClass.add("length", new JsonPrimitive(8));
43+
fieldClass.add("byte-order", new JsonPrimitive("le"));
44+
45+
// Add mappings for enumeration-like behavior
46+
JsonObject mappings = new JsonObject();
47+
JsonArray range1 = new JsonArray();
48+
range1.add(new JsonPrimitive(0));
49+
range1.add(new JsonPrimitive(0));
50+
JsonArray ranges1 = new JsonArray();
51+
ranges1.add(range1);
52+
mappings.add("ZERO", ranges1);
53+
54+
JsonArray range2 = new JsonArray();
55+
range2.add(new JsonPrimitive(1));
56+
range2.add(new JsonPrimitive(1));
57+
JsonArray ranges2 = new JsonArray();
58+
ranges2.add(range2);
59+
mappings.add("ONE", ranges2);
60+
61+
fieldClass.add("mappings", mappings);
62+
63+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "fixed-length-unsigned-integer", "test", "int_field", fieldClass);
64+
65+
IntegerDeclaration result = org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser.INSTANCE.parse(node,
66+
new org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser.Param(trace));
67+
68+
assertNotNull(result);
69+
assertEquals(8, result.getLength());
70+
assertNotNull(result.getMappings());
71+
assertEquals(2, result.getMappings().size());
72+
assertTrue(result.getMappings().containsKey("ZERO"));
73+
assertTrue(result.getMappings().containsKey("ONE"));
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
10+
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
16+
import java.nio.ByteOrder;
17+
18+
import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
19+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
20+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
21+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.floatingpoint.FloatDeclarationParser;
22+
import org.junit.Test;
23+
24+
import com.google.gson.JsonObject;
25+
import com.google.gson.JsonPrimitive;
26+
27+
/**
28+
* Test class for FloatDeclarationParser CTF2 support
29+
*/
30+
public class FloatDeclarationParserTest {
31+
32+
/**
33+
* Test parsing 32-bit floating point from CTF2 JSON
34+
*
35+
* @throws Exception if parsing fails
36+
*/
37+
@Test
38+
public void testCTF2Float32Parsing() throws Exception {
39+
CTFTrace trace = new CTFTrace();
40+
41+
JsonObject fieldClass = new JsonObject();
42+
fieldClass.add("length", new JsonPrimitive(32));
43+
fieldClass.add("byte-order", new JsonPrimitive("le"));
44+
45+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test", "float_field", fieldClass);
46+
47+
FloatDeclaration result = FloatDeclarationParser.INSTANCE.parse(node,
48+
new FloatDeclarationParser.Param(trace));
49+
50+
assertNotNull(result);
51+
assertEquals(8, result.getExponent());
52+
assertEquals(24, result.getMantissa());
53+
assertEquals(ByteOrder.LITTLE_ENDIAN, result.getByteOrder());
54+
}
55+
56+
/**
57+
* Test parsing 64-bit floating point from CTF2 JSON
58+
*
59+
* @throws Exception if parsing fails
60+
*/
61+
@Test
62+
public void testCTF2Float64Parsing() throws Exception {
63+
CTFTrace trace = new CTFTrace();
64+
65+
JsonObject fieldClass = new JsonObject();
66+
fieldClass.add("length", new JsonPrimitive(64));
67+
fieldClass.add("byte-order", new JsonPrimitive("be"));
68+
fieldClass.add("alignment", new JsonPrimitive(8));
69+
70+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "test", "test", "double_field", fieldClass);
71+
72+
FloatDeclaration result = FloatDeclarationParser.INSTANCE.parse(node,
73+
new FloatDeclarationParser.Param(trace));
74+
75+
assertNotNull(result);
76+
assertEquals(11, result.getExponent());
77+
assertEquals(53, result.getMantissa());
78+
assertEquals(ByteOrder.BIG_ENDIAN, result.getByteOrder());
79+
assertEquals(8, result.getAlignment());
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
10+
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
16+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonTraceMetadataNode;
17+
import org.junit.Test;
18+
19+
import com.google.gson.JsonObject;
20+
21+
/**
22+
* Test class for JsonTraceMetadataNode environment parsing
23+
*/
24+
public class JsonTraceMetadataNodeTest {
25+
26+
/**
27+
* Test parsing environment object from CTF2 JSON
28+
*
29+
* @throws Exception if parsing fails
30+
*/
31+
@Test
32+
public void testEnvironmentParsing() throws Exception {
33+
JsonTraceMetadataNode node = new JsonTraceMetadataNode(null, "trace-class", "test");
34+
35+
JsonObject environment = new JsonObject();
36+
environment.addProperty("hostname", "test-host");
37+
environment.addProperty("domain", "kernel");
38+
environment.addProperty("tracer_name", "lttng-modules");
39+
40+
// Simulate Gson deserialization by setting the field directly
41+
java.lang.reflect.Field envField = JsonTraceMetadataNode.class.getDeclaredField("fEnvironment");
42+
envField.setAccessible(true);
43+
envField.set(node, environment);
44+
45+
node.initialize();
46+
47+
assertNotNull(node.getEnvironment());
48+
assertEquals("test-host", node.getEnvironment().get("hostname").getAsString());
49+
assertEquals("kernel", node.getEnvironment().get("domain").getAsString());
50+
assertEquals("lttng-modules", node.getEnvironment().get("tracer_name").getAsString());
51+
}
52+
53+
/**
54+
* Test UID and packet header parsing
55+
*
56+
* @throws Exception if parsing fails
57+
*/
58+
@Test
59+
public void testUidAndPacketHeader() throws Exception {
60+
JsonTraceMetadataNode node = new JsonTraceMetadataNode(null, "trace-class", "test");
61+
62+
// Simulate Gson deserialization
63+
java.lang.reflect.Field uidField = JsonTraceMetadataNode.class.getDeclaredField("fUid");
64+
uidField.setAccessible(true);
65+
uidField.set(node, "test-uid-123");
66+
67+
node.initialize();
68+
69+
assertEquals("test-uid-123", node.getUid());
70+
}
71+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
10+
11+
package org.eclipse.tracecompass.ctf.core.tests.types;
12+
13+
import static org.junit.Assert.assertNotNull;
14+
import static org.junit.Assert.assertTrue;
15+
16+
import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
17+
import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
18+
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
19+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
20+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
21+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser;
22+
import org.junit.Test;
23+
24+
import com.google.gson.JsonObject;
25+
import com.google.gson.JsonPrimitive;
26+
27+
/**
28+
* Test class for TypeAliasParser CTF2 field type support
29+
*/
30+
public class TypeAliasParserTest {
31+
32+
/**
33+
* Test parsing fixed-length floating point field class
34+
*
35+
* @throws Exception if parsing fails
36+
*/
37+
@Test
38+
public void testFixedLengthFloatingPointParsing() throws Exception {
39+
CTFTrace trace = new CTFTrace();
40+
DeclarationScope scope = new DeclarationScope(null, "test");
41+
42+
JsonObject fieldClass = new JsonObject();
43+
fieldClass.add("type", new JsonPrimitive("fixed-length-floating-point-number"));
44+
fieldClass.add("length", new JsonPrimitive(32));
45+
fieldClass.add("byte-order", new JsonPrimitive("le"));
46+
47+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "fixed-length-floating-point-number", "test", "float_field", fieldClass);
48+
49+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
50+
new TypeAliasParser.Param(trace, scope));
51+
52+
assertNotNull(result);
53+
assertTrue(result instanceof FloatDeclaration);
54+
55+
FloatDeclaration floatDecl = (FloatDeclaration) result;
56+
assertTrue(floatDecl.getExponent() > 0);
57+
assertTrue(floatDecl.getMantissa() > 0);
58+
}
59+
60+
/**
61+
* Test parsing static-length string field class
62+
*
63+
* @throws Exception if parsing fails
64+
*/
65+
@Test
66+
public void testStaticLengthStringParsing() throws Exception {
67+
CTFTrace trace = new CTFTrace();
68+
DeclarationScope scope = new DeclarationScope(null, "test");
69+
70+
JsonObject fieldClass = new JsonObject();
71+
fieldClass.add("type", new JsonPrimitive("static-length-string"));
72+
fieldClass.add("length", new JsonPrimitive(16));
73+
fieldClass.add("encoding", new JsonPrimitive("utf-8"));
74+
75+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "static-length-string", "test", "string_field", fieldClass);
76+
77+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
78+
new TypeAliasParser.Param(trace, scope));
79+
80+
assertNotNull(result);
81+
}
82+
83+
/**
84+
* Test parsing dynamic-length string field class
85+
*
86+
* @throws Exception if parsing fails
87+
*/
88+
@Test
89+
public void testDynamicLengthStringParsing() throws Exception {
90+
// Test that the parser doesn't throw an exception for dynamic length strings
91+
// The actual parsing logic may not be fully implemented yet
92+
CTFTrace trace = new CTFTrace();
93+
DeclarationScope scope = new DeclarationScope(null, "test");
94+
95+
JsonObject fieldClass = new JsonObject();
96+
fieldClass.add("type", new JsonPrimitive("dynamic-length-string"));
97+
fieldClass.add("encoding", new JsonPrimitive("utf-8"));
98+
99+
JsonStructureFieldMemberMetadataNode node = new JsonStructureFieldMemberMetadataNode(null, "dynamic-length-string", "test", "dyn_string_field", fieldClass);
100+
101+
try {
102+
IDeclaration result = TypeAliasParser.INSTANCE.parse(node,
103+
new TypeAliasParser.Param(trace, scope));
104+
// If we get here without exception, the basic parsing works
105+
assertNotNull(result);
106+
} catch (Exception e) {
107+
// For now, just verify the parser recognizes the type
108+
assertTrue("Parser should handle dynamic-length-string type",
109+
e.getMessage() == null || !e.getMessage().contains("Invalid field class"));
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)