Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Willemsen committed Oct 25, 2022
1 parent 52b899c commit 88bb6db
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion base/uk.ac.stfc.isis.ibex.epics.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ Require-Bundle: org.junit,
org.mockito.mockito-core;bundle-version="4.7.0",
net.bytebuddy.byte-buddy;bundle-version="1.12.13",
net.bytebuddy.byte-buddy-agent;bundle-version="1.12.13",
org.objenesis;bundle-version="3.2.0"
org.objenesis;bundle-version="3.2.0",
com.google.gson,
com.google.guava
Automatic-Module-Name: uk.ac.stfc.isis.ibex.epics.tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package uk.ac.stfc.isis.ibex.epics.tests.conversion.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import uk.ac.stfc.isis.ibex.epics.conversion.ConversionException;
import uk.ac.stfc.isis.ibex.epics.conversion.json.LowercaseEnumTypeAdapterFactory;

@RunWith(MockitoJUnitRunner.Strict.class)
public class LowercaseEnumTypeAdapterFactoryTest {

public static enum TestEnum {
IMPLICIT_ONE,
IMPLICIT_TWO;
}

public static enum TestEnumWithSerializedName {
@SerializedName("name1") EXPLICIT_ONE,
@SerializedName("name2") EXPLICIT_TWO;
}

public static enum TestEnumMixed {
MIXED_ONE,
@SerializedName("explicit_name") MIXED_TWO;
}

@Test
public void convert_enum() throws ConversionException {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory())
.create();

assertEquals(TestEnum.IMPLICIT_ONE, gson.fromJson("\"implicit_one\"", TestEnum.class));
assertEquals(TestEnum.IMPLICIT_TWO, gson.fromJson("\"implicit_two\"", TestEnum.class));

assertEquals(TestEnumWithSerializedName.EXPLICIT_ONE, gson.fromJson("\"name1\"", TestEnumWithSerializedName.class));
assertEquals(TestEnumWithSerializedName.EXPLICIT_TWO, gson.fromJson("\"name2\"", TestEnumWithSerializedName.class));

assertEquals(TestEnumMixed.MIXED_ONE, gson.fromJson("\"mixed_one\"", TestEnumMixed.class));
assertEquals(TestEnumMixed.MIXED_TWO, gson.fromJson("\"explicit_name\"", TestEnumMixed.class));
}
}

0 comments on commit 88bb6db

Please sign in to comment.