Skip to content

Commit

Permalink
Test inner class serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Dec 23, 2019
1 parent 2102020 commit 426fd9e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 19 deletions.
47 changes: 45 additions & 2 deletions src/test/java/com/github/ravenlab/autogson/test/AutoGsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.junit.Test;

import com.github.ravenlab.AutoGson;
import com.github.ravenlab.autogson.test.obj.FooBar;
import com.github.ravenlab.autogson.test.obj.FooBarChild;
import com.github.ravenlab.autogson.test.foo.FooBar;
import com.github.ravenlab.autogson.test.foo.FooBarChild;
import com.github.ravenlab.autogson.test.foo.data.ExtraFooData;
import com.github.ravenlab.autogson.test.foo.data.FooData;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -78,4 +80,45 @@ public void testInheritance()
fail("Class not found or an issue with the original json");
}
}

@Test
public void testInnerCustomClass()
{
FooBar child = new FooBarChild();
Gson gson = new Gson();
String json = AutoGson.toJson(gson, child);

try
{
FooBarChild foo = AutoGson.fromJson(gson, json);
String data = foo.getData().getData();
assertTrue(data.equals("somedata"));
}
catch (JsonSyntaxException | ClassNotFoundException e)
{
e.printStackTrace();
fail("Class not found or an issue with the original json");
}
}

@Test
public void testInnerCustomClassChild()
{
FooBar child = new FooBarChild();
Gson gson = new Gson();
String json = AutoGson.toJson(gson, child);

try
{
FooBarChild foo = AutoGson.fromJson(gson, json);
FooData<String> fooData = foo.getData();
System.out.println(fooData.getClass().getName());
assertTrue(fooData instanceof ExtraFooData);
}
catch (JsonSyntaxException | ClassNotFoundException e)
{
e.printStackTrace();
fail("Class not found or an issue with the original json");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.ravenlab.autogson.test.obj;
package com.github.ravenlab.autogson.test.foo;

public class FooBar {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.ravenlab.autogson.test.foo;

import com.github.ravenlab.autogson.test.foo.data.ExtraFooData;
import com.github.ravenlab.autogson.test.foo.data.FooData;

public class FooBarChild extends FooBar {

private String fooBar;
private ExtraFooData<String> fooData;
public FooBarChild()
{
super();
this.fooBar = "foobar";
this.fooData = new ExtraFooData<>("somedata", "someotherdata");
}

public String getFooBar()
{
return this.fooBar;
}

public FooData<String> getData()
{
return this.fooData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.ravenlab.autogson.test.foo.data;

public class ExtraFooData<T> extends FooData<T> {

private T extraData;
public ExtraFooData(T data, T extraData)
{
super(data);
}

public T getExtraData()
{
return this.extraData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.ravenlab.autogson.test.foo.data;

public class FooData<T> {

private T data;
public FooData(T data)
{
this.data = data;
}

public T getData()
{
return this.data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.github.ravenlab.autogson.test.foo.data;

This file was deleted.

0 comments on commit 426fd9e

Please sign in to comment.