Skip to content

Commit

Permalink
add support for constructor arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianTerhorst committed May 6, 2016
1 parent c4c8475 commit d718fe2
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.Map;
import java.util.Set;

import io.fabianterhorst.iron.testdata.ClassWithoutPublicNoArgConstructor;
import io.fabianterhorst.iron.testdata.Person;
import io.fabianterhorst.iron.testdata.PersonArg;

import static android.support.test.InstrumentationRegistry.getTargetContext;
import static io.fabianterhorst.iron.testdata.TestDataGenerator.genPerson;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void testPutMap() {

@Test
public void testPutPOJO() {
final Person person = genPerson(1);
final Person person = genPerson(new Person(), 1);
Iron.chest().write("profile", person);
//Iron.chest().invalidateCache("profile");
final Person savedPerson = Iron.chest().read("profile");
Expand Down Expand Up @@ -172,9 +172,9 @@ public void testPutSynchronizedList() {
assertThat(testReadWrite(list)).isEqualTo(list.getClass());
}

@Test(expected = IronException.class)
@Test
public void testReadWriteClassWithoutNoArgConstructor() {
ClassWithoutPublicNoArgConstructor constructor = new ClassWithoutPublicNoArgConstructor("constructor argument");
PersonArg constructor = new PersonArg("name");
assertThat(testReadWrite(constructor)).isEqualTo(constructor.getClass());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.fabianterhorst.iron.Iron;
import io.fabianterhorst.iron.testdata.Person;
import io.fabianterhorst.iron.testdata.PersonArg;
import io.fabianterhorst.iron.testdata.TestDataGenerator;

import static android.support.test.InstrumentationRegistry.getTargetContext;
Expand Down Expand Up @@ -44,7 +45,12 @@ public void testReadWrite500Contacts() throws Exception {
Hawk.clear();
long hawkTime = runTest(new HawkReadWriteContactsTest(), contacts, REPEAT_COUNT);

printResults("Read/write 500 contacts", ironTime, hawkTime);
final List<PersonArg> contactsArg = TestDataGenerator.genPersonArgList(500);
Iron.init(getTargetContext());
Iron.chest().destroy();
long ironArg = runTest(new IronReadWriteContactsArgTest(), contactsArg, REPEAT_COUNT);

printResults("Read/write 500 contacts", ironTime, hawkTime, ironArg);
}

@Test
Expand Down Expand Up @@ -92,6 +98,12 @@ private void printResults(String name, long ironTime, long hawkTime) {
name, ironTime, hawkTime));
}

private void printResults(String name, long paperTime, long hawkTime, long ironArgTime) {
Log.i(TAG, String.format("..................................\n%s " +
"\n Iron: %d \n Iron(arg-cons): %d \n Hawk: %d",
name, paperTime, ironArgTime, hawkTime));
}

private <T> long runTest(TestTask<T> task, T extra, int repeat) {
long start = SystemClock.uptimeMillis();
for (int i = 0; i < repeat; i++) {
Expand All @@ -104,6 +116,15 @@ interface TestTask<T> {
void run(int i, T extra);
}

private class IronReadWriteContactsArgTest implements TestTask<List<PersonArg>> {
@Override
public void run(int i, List<PersonArg> extra) {
String key = "contacts" + i;
Iron.chest().write(key, extra);
Iron.chest().<List<Person>>read(key);
}
}

private class IronReadWriteContactsTest implements TestTask<List<Person>> {
@Override
public void run(int i, List<Person> extra) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/**
* Tests deprecated put/get API
*/
@SuppressWarnings("deprecation")
@RunWith(AndroidJUnit4.class)
public class DataTest {

Expand Down Expand Up @@ -62,7 +63,7 @@ public void testPutMap() {

@Test
public void testPutPOJO() {
final Person person = genPerson(1);
final Person person = genPerson(new Person(), 1);
Iron.put("profile", person);

final Person savedPerson = Iron.get("profile");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.fabianterhorst.iron.testdata;

import java.util.Arrays;

public class PersonArg extends Person {
public PersonArg(String name) {
super();
setName("changed" + name);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || PersonArg.class != o.getClass()) return false;

PersonArg person = (PersonArg) o;

if (getAge() != person.getAge()) return false;
if (!Arrays.equals(getBikes(), person.getBikes())) return false;
if (getName() != null ? !getName().equals(person.getName()) : person.getName() != null)
return false;
//noinspection RedundantIfStatement
if (getPhoneNumbers() != null
? !getPhoneNumbers().equals(person.getPhoneNumbers())
: person.getPhoneNumbers() != null)
return false;

return true;
}

@Override
public int hashCode() {
return super.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ public class TestDataGenerator {
public static List<Person> genPersonList(int size) {
List<Person> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
Person p = genPerson(i);
Person p = genPerson(new Person(), i);
list.add(p);
}
return list;
}

public static List<PersonArg> genPersonArgList(int size) {
List<PersonArg> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
PersonArg p = genPerson(new PersonArg("name"), i);
list.add(p);
}
return list;
}

@NonNull
public static Person genPerson(int i) {
Person p = new Person();
public static <T extends Person> T genPerson(T p, int i) {
p.setAge(i);
p.setBikes(new String[2]);
p.getBikes()[0] = "Kellys gen#" + i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;

import org.objenesis.strategy.StdInstantiatorStrategy;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -70,6 +72,8 @@ private Kryo createKryoInstance() {
new NoArgCollectionSerializer());
// To keep backward compatibility don't change the order of serializers above

kryo.setInstantiatorStrategy(
new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
return kryo;
}

Expand Down

0 comments on commit d718fe2

Please sign in to comment.