Skip to content

Commit

Permalink
tmf: Allow TmfConfiguration to be de-/serialized with GSON
Browse files Browse the repository at this point in the history
Also, generate unique UUID as ID from name.

[Added] Allow TmfConfiguration to be de-/serialized with GSON
[Added] TmfConfiguration.UNKNOWN string if values are not set
[Added[ Utility methods to write/read configuration to/from JSON file

Signed-off-by: Bernd Hufmann <[email protected]>
  • Loading branch information
bhufmann committed Oct 18, 2024
1 parent 4563db4 commit c6031c5
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.tmf.core.config.ITmfConfiguration;
import org.eclipse.tracecompass.tmf.core.config.TmfConfiguration;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.google.common.collect.ImmutableMap;

Expand All @@ -38,6 +50,27 @@ public class TmfConfigurationTest {
private static final @NonNull Map<@NonNull String, @NonNull Object> PARAM = ImmutableMap.of("path", "/tmp/home/my-test.xml");
private static final String EXPECTED_TO_STRING = "TmfConfiguration[fName=/tmp/my-test.xml, fDescription=descriptor, fType=my-source-id, fId=my-test.xml, fParameters={path=/tmp/home/my-test.xml}]";

private static final TemporaryFolder TEMPORARY_FOLDER = new TemporaryFolder();

/**
* Setup-up test class
*
* @throws IOException
* if an error occurs
*/
@BeforeClass
public static void setUp() throws IOException {
TEMPORARY_FOLDER.create();
}

/**
* Clean-up test class
*/
@AfterClass
public static void cleanUp() {
TEMPORARY_FOLDER.delete();
}

// ------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -65,45 +98,15 @@ public void testBuilder() {
*/
@Test
public void testBuilderMissingParams() {
// Test missing source type ID
TmfConfiguration.Builder builder = new TmfConfiguration.Builder()
.setName(PATH)
.setId(ID)
.setDescription(DESC)
.setParameters(PARAM);
try {
builder.build();
fail("No exception created");
} catch (IllegalStateException e) {
// success
}

// Test blank source type ID
builder = new TmfConfiguration.Builder()
.setName(PATH)
.setSourceTypeId(" ") // blank
.setId(ID)
.setDescription(DESC)
.setParameters(PARAM);
try {
builder.build();
fail("No exception created");
} catch (IllegalStateException e) {
// success
}

// Test missing ID
builder = new TmfConfiguration.Builder()
TmfConfiguration.Builder builder = new TmfConfiguration.Builder()
.setName(PATH)
.setDescription(DESC)
.setSourceTypeId(SOURCE_ID)
.setParameters(PARAM);
try {
builder.build();
fail("No exception created");
} catch (IllegalStateException e) {
// success
}
ITmfConfiguration config = builder.build();
// success - no exception created
assertEquals(UUID.nameUUIDFromBytes(Objects.requireNonNull((PATH).getBytes(Charset.defaultCharset()))).toString(), config.getId());

// Test blank ID
builder = new TmfConfiguration.Builder()
Expand All @@ -112,12 +115,9 @@ public void testBuilderMissingParams() {
.setId("\n") // blank
.setDescription(DESC)
.setParameters(PARAM);
try {
builder.build();
fail("No exception created");
} catch (IllegalStateException e) {
// success
}
config = builder.build();
// success - no exception created
assertEquals(UUID.nameUUIDFromBytes(Objects.requireNonNull((PATH).getBytes(Charset.defaultCharset()))).toString(), config.getId());

// Test successful builder
builder = new TmfConfiguration.Builder()
Expand Down Expand Up @@ -216,4 +216,34 @@ public void testHashCode() {
assertEquals(config2.hashCode(), config2.hashCode());
assertNotEquals(config1.hashCode(), config2.hashCode());
}

/**
* Test {@Link TmfConfiguration#hashCode()}
*
* @throws TmfConfigurationException
* if a such error occurs
*/
@Test
public void testJsonFile() throws TmfConfigurationException {
TmfConfiguration.Builder builder = new TmfConfiguration.Builder()
.setName(PATH)
.setId(ID)
.setDescription(DESC)
.setSourceTypeId(SOURCE_ID)
.setParameters(PARAM);
ITmfConfiguration config1 = builder.build();

File folder = TEMPORARY_FOLDER.getRoot();

IPath rootFolder = new Path(folder.getAbsolutePath());
TmfConfiguration.writeConfiguration(config1, rootFolder);

IPath path = rootFolder.append(config1.getId()).addFileExtension("json");
File file = path.toFile();
assertTrue(file.exists());

ITmfConfiguration readConfig = TmfConfiguration.fromJsonFile(file);
assertNotNull(readConfig);
assertEquals(config1, readConfig);
}
}
Loading

0 comments on commit c6031c5

Please sign in to comment.