Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
pilgr committed Sep 15, 2015
2 parents cb1b3c8 + b2b3b47 commit 8bed09d
Show file tree
Hide file tree
Showing 15 changed files with 567 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: android
jdk: oraclejdk7
env:
matrix:
- ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a
- ANDROID_TARGET=android-15 ANDROID_ABI=armeabi-v7a

android:
components:
Expand Down
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,50 @@ Save data object. Your custom classes must have no-arg constructor.
Paper creates separate data file for each key.

```java
Paper.put("city", "Lund"); // Primitive
Paper.put("task-queue", queue); // LinkedList
Paper.put("countries", countryCodeMap); // HashMap
Paper.book().write("city", "Lund"); // Primitive
Paper.book().write("task-queue", queue); // LinkedList
Paper.book().write("countries", countryCodeMap); // HashMap
```

#### Read
Read data objects. Paper instantiates exactly the classes which has been used in saved data. The limited backward and forward compatibility is supported. See [Handle data class changes](#handle-data-structure-changes).

```java
String city = Paper.get("city");
LinkedList queue = Paper.get("task-queue");
HashMap countryCodeMap = Paper.get("countries");
String city = Paper.book().read("city");
LinkedList queue = Paper.book().read("task-queue");
HashMap countryCodeMap = Paper.book().read("countries");
```

Use default values if object doesn't exist in the storage.

```java
String city = Paper.get("city", "Kyiv");
LinkedList queue = Paper.get("task-queue", new LinkedList());
HashMap countryCodeMap = Paper.get("countries", new HashMap());
String city = Paper.book().read("city", "Kyiv");
LinkedList queue = Paper.book().read("task-queue", new LinkedList());
HashMap countryCodeMap = Paper.book().read("countries", new HashMap());
```

#### Delete
Delete data for one key.

```java
Paper.delete("countries");
Paper.book().delete("countries");
```

Completely clear Paper storage. Doesn't require to call init() before usage.
Completely destroys Paper storage. Requires to call ```Paper.init()``` before usage.

```java
Paper.clear(context);
Paper.book().destroy();
```

#### Use custom book
You can create custom Book with separate storage using

```java
Paper.book("custom-book")...;
```

Any changes in one book doesn't affect to others books.

#### Handle data structure changes
Class fields which has been removed will be ignored on restore and new fields will have their default values. For example, if you have following data class saved in Paper storage:

Expand Down Expand Up @@ -115,7 +124,7 @@ Paper is based on the following assumptions:
- Saved data on mobile are relatively small;
- Random file access on flash storage is very fast.

So each data object is saved in separate file and put/get operations write/read whole file.
So each data object is saved in separate file and write/read operations write/read whole file.

The [Kryo](https://github.com/EsotericSoftware/kryo) is used for object graph serialization and to provide data compatibility support.

Expand Down
8 changes: 4 additions & 4 deletions paperdb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "21.1.2"

defaultConfig {
minSdkVersion 15
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
Expand All @@ -29,12 +29,12 @@ android {
}

dependencies {
compile 'com.esotericsoftware:kryo:3.0.1'
compile 'de.javakaffee:kryo-serializers:0.30'
compile 'com.esotericsoftware:kryo:3.0.2'
compile 'de.javakaffee:kryo-serializers:0.33'

androidTestCompile 'com.orhanobut:hawk:1.14'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
}

apply from: '../publish.gradle'
apply from: '../publish.gradle'
14 changes: 7 additions & 7 deletions paperdb/src/androidTest/java/io/paperdb/CompatibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class CompatibilityTest {

@Before
public void setUp() throws Exception {
Paper.clear(getTargetContext());
Paper.init(getTargetContext());
Paper.book().destroy();
}

@Test
Expand All @@ -34,10 +34,10 @@ public void testChangeClass()
testClass.timestamp = 123;

// Save original class. Only class name is changed to TestClassNew
Paper.put("test", testClass);
Paper.book().write("test", testClass);

// Read and instantiate a modified class TestClassNew based on saved data in TestClass
TestClassNew newTestClass = Paper.get("test");
TestClassNew newTestClass = Paper.book().read("test");
// Check original value is restored despite new default value in TestClassNew
assertThat(newTestClass.name).isEqualTo("original");
// Check default value for new added field
Expand All @@ -51,9 +51,9 @@ public void testNotCompatibleClassChanges() throws Exception {
TestClass testClass = getClassInstanceWithNewName(TestClass.class,
TestClassNotCompatible.class.getName());
testClass.timestamp = 123;
Paper.put("not-compatible", testClass);
Paper.book().write("not-compatible", testClass);

Paper.<TestClassNotCompatible>get("not-compatible");
Paper.book().<TestClassNotCompatible>read("not-compatible");
}

@Test
Expand All @@ -62,9 +62,9 @@ public void testTransientFields() throws Exception {
tc.timestamp = 123;
tc.transientField = "changed";

Paper.put("transient-class", tc);
Paper.book().write("transient-class", tc);

TestClassTransient readTc = Paper.get("transient-class");
TestClassTransient readTc = Paper.book().read("transient-class");
assertThat(readTc.timestamp).isEqualTo(123);
assertThat(readTc.transientField).isEqualTo("default");
}
Expand Down
30 changes: 18 additions & 12 deletions paperdb/src/androidTest/java/io/paperdb/DataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Map;

import io.paperdb.testdata.ClassWithoutPublicNoArgConstructor;
import io.paperdb.testdata.Person;

import static android.support.test.InstrumentationRegistry.getTargetContext;
Expand All @@ -23,47 +24,47 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests List put/get API
* Tests List write/read API
*/
@RunWith(AndroidJUnit4.class)
public class DataTest {

@Before
public void setUp() throws Exception {
Paper.clear(getTargetContext());
Paper.init(getTargetContext());
Paper.book().destroy();
}

@Test
public void testPutEmptyList() throws Exception {
final List<Person> inserted = genPersonList(0);
Paper.put("persons", inserted);
assertThat(Paper.<List>get("persons")).isEmpty();
Paper.book().write("persons", inserted);
assertThat(Paper.book().<List>read("persons")).isEmpty();
}

@Test
public void testPutGetList() {
final List<Person> inserted = genPersonList(10000);
Paper.put("persons", inserted);
List<Person> persons = Paper.get("persons");
Paper.book().write("persons", inserted);
List<Person> persons = Paper.book().read("persons");
assertThat(persons).isEqualTo(inserted);
}

@Test
public void testPutMap() {
final Map<Integer, Person> inserted = genPersonMap(10000);
Paper.put("persons", inserted);
Paper.book().write("persons", inserted);

final Map<Integer, Person> personMap = Paper.get("persons");
final Map<Integer, Person> personMap = Paper.book().read("persons");
assertThat(personMap).isEqualTo(inserted);
}

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

final Person savedPerson = Paper.get("profile");
final Person savedPerson = Paper.book().read("profile");
assertThat(savedPerson).isEqualTo(person);
assertThat(savedPerson).isNotSameAs(person);
}
Expand Down Expand Up @@ -133,9 +134,14 @@ public void testPutSynchronizedList() {
testReadWrite(Collections.synchronizedList(new ArrayList<>()));
}

@Test(expected = PaperDbException.class)
public void testReadWriteClassWithoutNoArgConstructor() {
testReadWrite(new ClassWithoutPublicNoArgConstructor("constructor argument"));
}

private Object testReadWriteWithoutClassCheck(Object originObj) {
Paper.put("obj", originObj);
Object readObj = Paper.get("obj");
Paper.book().write("obj", originObj);
Object readObj = Paper.book().read("obj");
assertThat(readObj).isEqualTo(originObj);
return readObj;
}
Expand Down
Loading

0 comments on commit 8bed09d

Please sign in to comment.