ctf: Update display text for unknown enum values

bug 560478

When values for a CTF enum field is not known, it displays "<unknown>
(%s)", where %s is the value instead of null

[fixed] Display unknown value for unknown CTF enum instead of null

Change-Id: I4727da35f75bab3b0b5579b3650d10abdaa03832
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/158262
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
diff --git a/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/types/EnumDefinitionTest.java b/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/types/EnumDefinitionTest.java
index 52417c5..dd14e66 100644
--- a/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/types/EnumDefinitionTest.java
+++ b/ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/types/EnumDefinitionTest.java
@@ -1,5 +1,6 @@
 /*******************************************************************************
- * Copyright (c) 2013, 2014 Ericsson
+ * Copyright (c) 2013, 2020 Ericsson, École Polytechnique de Montréal
+ *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
  * which accompanies this distribution, and is available at
@@ -30,7 +31,7 @@
  * The class <code>EnumDefinitionTest</code> contains tests for the class
  * <code>{@link EnumDefinition}</code>.
  *
- * @author ematkho
+ * @author Matthew Khouzam
  * @version $Revision: 1.0 $
  */
 public class EnumDefinitionTest {
@@ -94,4 +95,25 @@
 
         assertEquals("{ value = b, container = 12 }", result);
     }
+
+    /**
+     * Test results of an unknown enumeration value
+     */
+    @Test
+    public void testUnknownEnum() {
+        IntegerDeclaration integerDeclaration = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
+                Encoding.ASCII, "", 8);
+        EnumDeclaration declaration = new EnumDeclaration(
+                integerDeclaration);
+        declaration.add(0, 10, "a");
+        declaration.add(11, 20, "b");
+        String fieldName = "unknownValue";
+
+        // Test with a value of 0, where 0 is not part of the enumeration
+        EnumDefinition fixture = new EnumDefinition(declaration, null, fieldName, new IntegerDefinition(integerDeclaration, null, fieldName, 22));
+
+        assertEquals("<unknown> (22)", fixture.getValue());
+        assertEquals("{ value = <unknown> (22), container = 22 }", fixture.toString());
+    }
+
 }
\ No newline at end of file
diff --git a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDefinition.java b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDefinition.java
index b24c1ef..18c2bdd 100644
--- a/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDefinition.java
+++ b/ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDefinition.java
@@ -15,6 +15,7 @@
 package org.eclipse.tracecompass.ctf.core.event.types;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
 
 /**
@@ -33,9 +34,11 @@
     // Attributes
     // ------------------------------------------------------------------------
 
+    private static final String UNKNOWN_ENUM = "<unknown> (%s)"; //$NON-NLS-1$
+
     private final IntegerDefinition fInteger;
 
-    private final String fValue;
+    private final @Nullable String fValue;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -72,7 +75,7 @@
      * @return the value of the enum.
      */
     public String getValue() {
-        return fValue;
+        return fValue != null ? fValue : String.format(UNKNOWN_ENUM, getIntegerValue());
     }
 
     @Override