From 76198ea55106a6ec660ec0cdbb88dab4bb0cc74f Mon Sep 17 00:00:00 2001 From: Cristian Beskid Date: Mon, 3 Aug 2015 16:18:47 +0200 Subject: [PATCH] Extend unit test for inner public static classes Use the actual class names instead of the canonical class names when storing/retrieving values. This allows storing and retrieving objects defined using inner classes Update unit tests to use class names instead of canonical class names --- .../java/com/orhanobut/hawk/DataHelper.java | 10 +++--- .../com/orhanobut/hawk/DataHelperTest.java | 32 +++++++++---------- .../test/java/com/orhanobut/hawk/FooBar.java | 4 +++ .../java/com/orhanobut/hawk/HawkTest.java | 5 +++ 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/hawk/src/main/java/com/orhanobut/hawk/DataHelper.java b/hawk/src/main/java/com/orhanobut/hawk/DataHelper.java index 3ab4513..af4b625 100644 --- a/hawk/src/main/java/com/orhanobut/hawk/DataHelper.java +++ b/hawk/src/main/java/com/orhanobut/hawk/DataHelper.java @@ -122,7 +122,7 @@ static 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())) { @@ -130,8 +130,8 @@ static String addType(String cipherText, T t) { 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; } } @@ -140,13 +140,13 @@ static 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 + diff --git a/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java b/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java index 6a8cbfd..7f69539 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/DataHelperTest.java @@ -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); } @@ -61,13 +61,13 @@ public void addTypeAsList() { List 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 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); } @@ -76,15 +76,15 @@ public void addTypeAsMap() { Map 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 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); } @@ -112,13 +112,13 @@ public void addTypeAsSet() { Set 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 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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/hawk/src/test/java/com/orhanobut/hawk/FooBar.java b/hawk/src/test/java/com/orhanobut/hawk/FooBar.java index ede1a6a..bc336e0 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/FooBar.java +++ b/hawk/src/test/java/com/orhanobut/hawk/FooBar.java @@ -5,4 +5,8 @@ */ public class FooBar { String name = "hawk"; + + public static class InnerFoo { + public String name = "hawk"; + } } diff --git a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java index 83bc09c..2b8326c 100644 --- a/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java +++ b/hawk/src/test/java/com/orhanobut/hawk/HawkTest.java @@ -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