Skip to content

Commit

Permalink
Merge pull request #67 from sferra/master
Browse files Browse the repository at this point in the history
Hawk stores objects defined using inner classes, but cannot restore them
  • Loading branch information
orhanobut committed Aug 5, 2015
2 parents d48d6ec + 76198ea commit 60a4d1c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
10 changes: 5 additions & 5 deletions hawk/src/main/java/com/orhanobut/hawk/DataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ static <T> String addType(String cipherText, T t) {
if (List.class.isAssignableFrom(t.getClass())) {
List<?> list = (List<?>) t;
if (!list.isEmpty()) {
keyClassName = list.get(0).getClass().getCanonicalName();
keyClassName = list.get(0).getClass().getName();
}
dataType = DataType.LIST;
} else if (Map.class.isAssignableFrom(t.getClass())) {
dataType = DataType.MAP;
Map<?, ?> map = (Map) t;
if (!map.isEmpty()) {
for (Map.Entry<?, ?> entry : map.entrySet()) {
keyClassName = entry.getKey().getClass().getCanonicalName();
valueClassName = entry.getValue().getClass().getCanonicalName();
keyClassName = entry.getKey().getClass().getName();
valueClassName = entry.getValue().getClass().getName();
break;
}
}
Expand All @@ -140,13 +140,13 @@ static <T> String addType(String cipherText, T t) {
if (!set.isEmpty()) {
Iterator<?> iterator = set.iterator();
if (iterator.hasNext()) {
keyClassName = iterator.next().getClass().getCanonicalName();
keyClassName = iterator.next().getClass().getName();
}
}
dataType = DataType.SET;
} else {
dataType = DataType.OBJECT;
keyClassName = t.getClass().getCanonicalName();
keyClassName = t.getClass().getName();
}

return keyClassName + INFO_DELIMITER +
Expand Down
32 changes: 16 additions & 16 deletions hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public void testOldVersionCheck() {
public void addTypeAsObject() {
String text = "test";
String actual = DataHelper.addType(CIPHER_TEXT, text);
String expected = text.getClass().getCanonicalName() + "##0V@" + CIPHER_TEXT;
String expected = text.getClass().getName() + "##0V@" + CIPHER_TEXT;
assertThat(actual).isEqualTo(expected);

Foo foo = new Foo();
String actualFoo = DataHelper.addType(CIPHER_TEXT, foo);
String expectedFoo = foo.getClass().getCanonicalName() + "##0V@" + CIPHER_TEXT;
String expectedFoo = foo.getClass().getName() + "##0V@" + CIPHER_TEXT;
assertThat(actualFoo).isEqualTo(expectedFoo);
}

Expand All @@ -61,13 +61,13 @@ public void addTypeAsList() {
List<String> list = new ArrayList<>();
list.add("test");
String actual = DataHelper.addType(CIPHER_TEXT, list);
String expected = list.get(0).getClass().getCanonicalName() + "##1V@" + CIPHER_TEXT;
String expected = list.get(0).getClass().getName() + "##1V@" + CIPHER_TEXT;
assertThat(actual).isEqualTo(expected);

List<Foo> list2 = new ArrayList<>();
list2.add(new Foo());
String actual2 = DataHelper.addType(CIPHER_TEXT, list2);
String expected2 = list2.get(0).getClass().getCanonicalName() + "##1V@" + CIPHER_TEXT;
String expected2 = list2.get(0).getClass().getName() + "##1V@" + CIPHER_TEXT;
assertThat(actual2).isEqualTo(expected2);
}

Expand All @@ -76,15 +76,15 @@ public void addTypeAsMap() {
Map<String, String> map = new HashMap<>();
map.put("key", "value");
String actual = DataHelper.addType(CIPHER_TEXT, map);
String expected = String.class.getCanonicalName() + "#" +
String.class.getCanonicalName() + "#2V@" + CIPHER_TEXT;
String expected = String.class.getName() + "#" +
String.class.getName() + "#2V@" + CIPHER_TEXT;
assertThat(actual).isEqualTo(expected);

Map<String, Foo> map2 = new HashMap<>();
map2.put("key", new Foo());
String actual2 = DataHelper.addType(CIPHER_TEXT, map2);
String expected2 = String.class.getCanonicalName() + "#" +
Foo.class.getCanonicalName() + "#2V@" + CIPHER_TEXT;
String expected2 = String.class.getName() + "#" +
Foo.class.getName() + "#2V@" + CIPHER_TEXT;
assertThat(actual2).isEqualTo(expected2);
}

Expand Down Expand Up @@ -112,13 +112,13 @@ public void addTypeAsSet() {
Set<String> set = new HashSet<>();
set.add("key");
String actual = DataHelper.addType(CIPHER_TEXT, set);
String expected = String.class.getCanonicalName() + "##3V@" + CIPHER_TEXT;
String expected = String.class.getName() + "##3V@" + CIPHER_TEXT;
assertThat(actual).isEqualTo(expected);

Set<Foo> set2 = new HashSet<>();
set2.add(new Foo());
String actual2 = DataHelper.addType(CIPHER_TEXT, set2);
String expected2 = Foo.class.getCanonicalName() + "##3V@" + CIPHER_TEXT;
String expected2 = Foo.class.getName() + "##3V@" + CIPHER_TEXT;
assertThat(actual2).isEqualTo(expected2);
}

Expand All @@ -130,8 +130,8 @@ public void getDataInfoAsObject() {
DataInfo dataInfo = DataHelper.getDataInfo(clazz + "#" + clazz + "#" + info + "@" + cipher);
assertThat(dataInfo).isNotNull();
assertThat(dataInfo.isNewVersion()).isTrue();
assertThat(dataInfo.getKeyClazz().getCanonicalName()).isEqualTo(clazz);
assertThat(dataInfo.getValueClazz().getCanonicalName()).isEqualTo(clazz);
assertThat(dataInfo.getKeyClazz().getName()).isEqualTo(clazz);
assertThat(dataInfo.getValueClazz().getName()).isEqualTo(clazz);
assertThat(dataInfo.getDataType()).isEqualTo(DataType.OBJECT);
}

Expand All @@ -143,7 +143,7 @@ public void getDataInfoAsList() {
DataInfo dataInfo = DataHelper.getDataInfo(clazz + "##" + info + "@" + cipher);
assertThat(dataInfo).isNotNull();
assertThat(dataInfo.isNewVersion()).isTrue();
assertThat(dataInfo.getKeyClazz().getCanonicalName()).isEqualTo(clazz);
assertThat(dataInfo.getKeyClazz().getName()).isEqualTo(clazz);
assertThat(dataInfo.getValueClazz()).isNull();
assertThat(dataInfo.getDataType()).isEqualTo(DataType.LIST);
}
Expand All @@ -156,8 +156,8 @@ public void getDataInfoAsMap() {
DataInfo dataInfo = DataHelper.getDataInfo(clazz + "#" + clazz + "#" + info + "@" + cipher);
assertThat(dataInfo).isNotNull();
assertThat(dataInfo.isNewVersion()).isTrue();
assertThat(dataInfo.getKeyClazz().getCanonicalName()).isEqualTo(clazz);
assertThat(dataInfo.getValueClazz().getCanonicalName()).isEqualTo(clazz);
assertThat(dataInfo.getKeyClazz().getName()).isEqualTo(clazz);
assertThat(dataInfo.getValueClazz().getName()).isEqualTo(clazz);
assertThat(dataInfo.getDataType()).isEqualTo(DataType.MAP);
}

Expand All @@ -169,7 +169,7 @@ public void getDataInfoAsSet() {
DataInfo dataInfo = DataHelper.getDataInfo(clazz + "##" + info + "@" + cipher);
assertThat(dataInfo).isNotNull();
assertThat(dataInfo.isNewVersion()).isTrue();
assertThat(dataInfo.getKeyClazz().getCanonicalName()).isEqualTo(clazz);
assertThat(dataInfo.getKeyClazz().getName()).isEqualTo(clazz);
assertThat(dataInfo.getValueClazz()).isNull();
assertThat(dataInfo.getDataType()).isEqualTo(DataType.SET);
}
Expand Down
4 changes: 4 additions & 0 deletions hawk/src/test/java/com/orhanobut/hawk/FooBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
*/
public class FooBar {
String name = "hawk";

public static class InnerFoo {
public String name = "hawk";
}
}
5 changes: 5 additions & 0 deletions hawk/src/test/java/com/orhanobut/hawk/HawkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public void testSingleItem() {

assertThat(fooBar).isNotNull();
assertThat(fooBar.name).isEqualTo("hawk");

assertTrue(Hawk.put("innerClass", new FooBar.InnerFoo()));
FooBar.InnerFoo innerFoo = Hawk.get("innerClass");
assertThat(innerFoo).isNotNull();
assertThat(innerFoo.name).isEqualTo("hawk");
}

@Test
Expand Down

0 comments on commit 60a4d1c

Please sign in to comment.