Skip to content
This repository

#264 - using Apache ObjectSerializer. #543

Merged
merged 8 commits into from about 1 month ago

5 participants

Sebastian Roth Damien Joan Zapata Heath Borders Csaba Kozák
Sebastian Roth
ened commented

This is for #264 - support for Set objects in SharedPreferences.

Some questions:

  • This implementation is not very efficient for ApiLevel > 11; mainly because I'm not sure how AA could support this? androidannotations-api links against 1.6.
    • Access to SdkVersionHelper.getSdkInt() should be ok I guess
  • It's using the ObjectSerializer class from the apache pig project as discussed on SO: http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences
    • A alternative implementation could be to put each entry of the Set into it's own property, like "$IDX_$NAME" along with a "MAX_$NAME" property. This would require a little more work in the AbstractPrefEditorField functions
ened added some commits
Sebastian Roth ened #264 - using Apache ObjectSerializer. 8051cc2
Sebastian Roth ened Merge remote-tracking branch 'upstream/develop' into 264_sharedpref_s…
…tring_set

Conflicts:
	AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
37cb77e
Sebastian Roth ened Merge & move things in the right place again. 516874b
Sebastian Roth ened Ensure the stringset files will generated as well. 1bc53aa
Sebastian Roth ened Ensure to use canonical name constants 5a87e8a
Sebastian Roth ened Merge branch 'develop' of https://github.com/excilys/androidannotations
… into 264_sharedpref_string_set

Conflicts:
	AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/SomePrefs.java
173bf64
Sebastian Roth
ened commented

Merged latest upstream/develop changes. Would be good to know whether it will be merged or not?

Damien
Collaborator
DayS commented

Sorry for the late reply. I don't have time to check this PR until next week. Maybe @JoanZapata @pyricau @mathieuboniface could take a look ? :)

Just to answer quickly, I'm not a big fan of binary encoding... I'd prefer encoding like json which keep some readability.
But I don't really know which is the best for this case. Any thought ?

Sebastian Roth
ened commented

Yes, serializing with JSON is another option. Android supports this using the org.json (http://developer.android.com/reference/org/json/package-summary.html) package. Will re-implement using that if we have a decision about it.

Joan Zapata
Collaborator

I believe android encodes StringSet as XML in recent releases, so my opinion is that AA should do the same.

Damien
Collaborator
DayS commented

This PR needs to be updated

Damien
Collaborator
DayS commented

This feature could really be interesting. @JoanZapata was right : SharedPreferences are stored using XML, so we should de it the same way.

@ened Sorry Do you have any more time to work on this ?

Sebastian Roth
ened commented

@DayS should have, but not too soon. Could someone point me to the part in the AOSP which handles (de-)serialization? Would certainly save some time.

Damien
Collaborator
DayS commented

I'll check that

ened added some commits
Sebastian Roth ened Merge https://github.com/excilys/androidannotations into 264_sharedpr…
…ef_string_set

Conflicts:
	AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
4ee1bfd
Sebastian Roth ened Update to latest AA code changes. c18702f
Sebastian Roth

@DayS one a side note: I found it would be useful to have support for additional types as well, like Set<Integer>. Do you think this is something AA could/should include? It's not part of the standard Android API though. Same as adding support for short preferences.

Damien
Collaborator

Thanks for the update :) I'll take a look at this today.

Yeah, it could be a good idea. But I think we should add a note in javadoc to warn user that this is not natively supported by Android.

Damien
Collaborator

I can't find your new serialization implementation. Am I crazy or is it just a mistake ? :)

Sebastian Roth

@DayS new implementation is the next task (for this week). Just updated the PR to be valid again.

Heath Borders

@ened Is this still something you're working on?

Csaba Kozák

@ened are you still working on this?

Csaba Kozák

@ened i can take over if you do not have the time to finish this.

Damien
Collaborator
DayS commented

@WonderCsabo > It'll be great :)

Sebastian Roth
ened commented

@WonderCsabo yes please & thanks for helping out.

Damien DayS merged commit c18702f into from
Damien DayS closed this
Damien
Collaborator
DayS commented

This one has been merged through #962

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 8 unique commits by 1 author.

Mar 29, 2013
Sebastian Roth ened #264 - using Apache ObjectSerializer. 8051cc2
Sebastian Roth ened Merge remote-tracking branch 'upstream/develop' into 264_sharedpref_s…
…tring_set

Conflicts:
	AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
37cb77e
Sebastian Roth ened Merge & move things in the right place again. 516874b
Sebastian Roth ened Ensure the stringset files will generated as well. 1bc53aa
Apr 05, 2013
Sebastian Roth ened Ensure to use canonical name constants 5a87e8a
May 23, 2013
Sebastian Roth ened Merge branch 'develop' of https://github.com/excilys/androidannotations
… into 264_sharedpref_string_set

Conflicts:
	AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/SomePrefs.java
173bf64
Oct 04, 2013
Sebastian Roth ened Merge https://github.com/excilys/androidannotations into 264_sharedpr…
…ef_string_set

Conflicts:
	AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
4ee1bfd
Sebastian Roth ened Update to latest AA code changes. c18702f
This page is out of date. Refresh to see the latest.

Showing 12 changed files with 298 additions and 3 deletions. Show diff stats Hide diff stats

  1. +4 0 ...tations/androidannotations-api/src/main/java/org/androidannotations/api/sharedpreferences/EditorHelper.java
  2. +4 0 ...roidannotations-api/src/main/java/org/androidannotations/api/sharedpreferences/SharedPreferencesHelper.java
  3. +31 0 ...oidannotations-api/src/main/java/org/androidannotations/api/sharedpreferences/StringSetPrefEditorField.java
  4. +73 0 ...s/androidannotations-api/src/main/java/org/androidannotations/api/sharedpreferences/StringSetPrefField.java
  5. +76 0 AndroidAnnotations/androidannotations-api/src/main/java/org/apache/pig/impl/util/ObjectSerializer.java
  6. +40 0 AndroidAnnotations/androidannotations-api/src/main/java/org/apache/pig/impl/util/WrappedIOException.java
  7. +1 0  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java
  8. +2 1  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java
  9. +10 1 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
  10. +5 1 ...ions/androidannotations/src/test/java/org/androidannotations/generation/SharedPrefsApiDependenciesTest.java
  11. +49 0 ...otations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/prefs/PrefsActivityTest.java
  12. +3 0  AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/SomePrefs.java
4 ...ions/androidannotations-api/src/main/java/org/androidannotations/api/sharedpreferences/EditorHelper.java
@@ -47,6 +47,10 @@ public final void apply() {
47 47 return new StringPrefEditorField<T>(cast(), key);
48 48 }
49 49
  50 + protected StringSetPrefEditorField<T> stringSetField(String key) {
  51 + return new StringSetPrefEditorField<T>(cast(), key);
  52 + }
  53 +
50 54 protected BooleanPrefEditorField<T> booleanField(String key) {
51 55 return new BooleanPrefEditorField<T>(cast(), key);
52 56 }
4 ...dannotations-api/src/main/java/org/androidannotations/api/sharedpreferences/SharedPreferencesHelper.java
@@ -41,6 +41,10 @@ protected StringPrefField stringField(String key, String defaultValue) {
41 41 return new StringPrefField(sharedPreferences, key, defaultValue);
42 42 }
43 43
  44 + protected StringSetPrefField stringSetField(String key) {
  45 + return new StringSetPrefField(sharedPreferences, key);
  46 + }
  47 +
44 48 protected BooleanPrefField booleanField(String key, boolean defaultValue) {
45 49 return new BooleanPrefField(sharedPreferences, key, defaultValue);
46 50 }
31 ...annotations-api/src/main/java/org/androidannotations/api/sharedpreferences/StringSetPrefEditorField.java
... ... @@ -0,0 +1,31 @@
  1 +/**
  2 + * Copyright (C) 2010-2012 eBusiness Information, Excilys Group
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 + * use this file except in compliance with the License. You may obtain a copy of
  6 + * the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed To in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13 + * License for the specific language governing permissions and limitations under
  14 + * the License.
  15 + */
  16 +package org.androidannotations.api.sharedpreferences;
  17 +
  18 +import java.util.Set;
  19 +
  20 +public final class StringSetPrefEditorField<T extends EditorHelper<T>> extends AbstractPrefEditorField<T> {
  21 +
  22 + StringSetPrefEditorField(T editorHelper, String key) {
  23 + super(editorHelper, key);
  24 + }
  25 +
  26 + public T put(Set<String> value) {
  27 + StringSetPrefField.setValue(editorHelper.getEditor(), key, value);
  28 +
  29 + return editorHelper;
  30 + }
  31 +}
73 ...ndroidannotations-api/src/main/java/org/androidannotations/api/sharedpreferences/StringSetPrefField.java
... ... @@ -0,0 +1,73 @@
  1 +/**
  2 + * Copyright (C) 2010-2013 eBusiness Information, Excilys Group
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 + * use this file except in compliance with the License. You may obtain a copy of
  6 + * the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed To in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12 + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13 + * License for the specific language governing permissions and limitations under
  14 + * the License.
  15 + */
  16 +package org.androidannotations.api.sharedpreferences;
  17 +
  18 +import java.io.IOException;
  19 +import java.io.Serializable;
  20 +import java.util.Set;
  21 +import java.util.TreeSet;
  22 +
  23 +import org.apache.pig.impl.util.ObjectSerializer;
  24 +
  25 +import android.content.SharedPreferences;
  26 +import android.content.SharedPreferences.Editor;
  27 +
  28 +public final class StringSetPrefField extends AbstractPrefField {
  29 +
  30 + StringSetPrefField(SharedPreferences sharedPreferences, String key) {
  31 + super(sharedPreferences, key);
  32 + }
  33 +
  34 + public Set<String> get() {
  35 + return getOr(new TreeSet<String>());
  36 + }
  37 +
  38 + @SuppressWarnings("unchecked")
  39 + public Set<String> getOr(Set<String> defaultValue) {
  40 + Object obj;
  41 + try {
  42 + obj = ObjectSerializer.deserialize(sharedPreferences.getString(key, null));
  43 + } catch (IOException e) {
  44 + return null;
  45 + }
  46 +
  47 + if(obj == null) {
  48 + return defaultValue;
  49 + }
  50 +
  51 + if(!(obj instanceof Set<?>)) {
  52 + return defaultValue;
  53 + }
  54 +
  55 + // No way to check further.
  56 +
  57 + return (Set<String>) obj;
  58 + }
  59 +
  60 + public void put(Set<String> value) {
  61 + SharedPreferences.Editor editor = sharedPreferences.edit();
  62 + setValue(editor, key, value);
  63 + apply(editor);
  64 + }
  65 +
  66 + static void setValue(Editor editor, String key, Set<String> value) {
  67 + try {
  68 + editor.putString(key, ObjectSerializer.serialize((Serializable) value));
  69 + } catch (IOException e) {
  70 + throw new RuntimeException("Argument for key `" + key + "` does not implement the Serializable interface");
  71 + }
  72 + }
  73 +}
76 AndroidAnnotations/androidannotations-api/src/main/java/org/apache/pig/impl/util/ObjectSerializer.java
... ... @@ -0,0 +1,76 @@
  1 +/*
  2 + * Licensed to the Apache Software Foundation (ASF) under one
  3 + * or more contributor license agreements. See the NOTICE file
  4 + * distributed with this work for additional information
  5 + * regarding copyright ownership. The ASF licenses this file
  6 + * to you under the Apache License, Version 2.0 (the
  7 + * "License"); you may not use this file except in compliance
  8 + * with the License. You may obtain a copy of the License at
  9 + *
  10 + * http://www.apache.org/licenses/LICENSE-2.0
  11 + *
  12 + * Unless required by applicable law or agreed to in writing, software
  13 + * distributed under the License is distributed on an "AS IS" BASIS,
  14 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15 + * See the License for the specific language governing permissions and
  16 + * limitations under the License.
  17 + */
  18 +
  19 +package org.apache.pig.impl.util;
  20 +
  21 +import java.io.ByteArrayInputStream;
  22 +import java.io.ByteArrayOutputStream;
  23 +import java.io.IOException;
  24 +import java.io.ObjectInputStream;
  25 +import java.io.ObjectOutputStream;
  26 +import java.io.Serializable;
  27 +
  28 +public class ObjectSerializer {
  29 +
  30 + public static String serialize(Serializable obj) throws IOException {
  31 + if (obj == null) return "";
  32 + try {
  33 + ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
  34 + ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
  35 + objStream.writeObject(obj);
  36 + objStream.close();
  37 + return encodeBytes(serialObj.toByteArray());
  38 + } catch (Exception e) {
  39 + throw WrappedIOException.wrap("Serialization error: " + e.getMessage(), e);
  40 + }
  41 + }
  42 +
  43 + public static Object deserialize(String str) throws IOException {
  44 + if (str == null || str.length() == 0) return null;
  45 + try {
  46 + ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
  47 + ObjectInputStream objStream = new ObjectInputStream(serialObj);
  48 + return objStream.readObject();
  49 + } catch (Exception e) {
  50 + throw WrappedIOException.wrap("Deserialization error: " + e.getMessage(), e);
  51 + }
  52 + }
  53 +
  54 + public static String encodeBytes(byte[] bytes) {
  55 + StringBuffer strBuf = new StringBuffer();
  56 +
  57 + for (int i = 0; i < bytes.length; i++) {
  58 + strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
  59 + strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
  60 + }
  61 +
  62 + return strBuf.toString();
  63 + }
  64 +
  65 + public static byte[] decodeBytes(String str) {
  66 + byte[] bytes = new byte[str.length() / 2];
  67 + for (int i = 0; i < str.length(); i+=2) {
  68 + char c = str.charAt(i);
  69 + bytes[i/2] = (byte) ((c - 'a') << 4);
  70 + c = str.charAt(i+1);
  71 + bytes[i/2] += (c - 'a');
  72 + }
  73 + return bytes;
  74 + }
  75 +
  76 +}
40 AndroidAnnotations/androidannotations-api/src/main/java/org/apache/pig/impl/util/WrappedIOException.java
... ... @@ -0,0 +1,40 @@
  1 +/*
  2 + * Licensed to the Apache Software Foundation (ASF) under one
  3 + * or more contributor license agreements. See the NOTICE file
  4 + * distributed with this work for additional information
  5 + * regarding copyright ownership. The ASF licenses this file
  6 + * to you under the Apache License, Version 2.0 (the
  7 + * "License"); you may not use this file except in compliance
  8 + * with the License. You may obtain a copy of the License at
  9 + *
  10 + * http://www.apache.org/licenses/LICENSE-2.0
  11 + *
  12 + * Unless required by applicable law or agreed to in writing, software
  13 + * distributed under the License is distributed on an "AS IS" BASIS,
  14 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15 + * See the License for the specific language governing permissions and
  16 + * limitations under the License.
  17 + */
  18 +
  19 +package org.apache.pig.impl.util;
  20 +
  21 +import java.io.IOException;
  22 +
  23 +/**
  24 + * FIXME PIG-80 replace this code when pig will be java 6 compliant with "throw
  25 + * new IOException(e);"
  26 + */
  27 +public class WrappedIOException {
  28 +
  29 + public static IOException wrap(final Throwable e) {
  30 + return wrap(e.getMessage(), e);
  31 + }
  32 +
  33 + public static IOException wrap(final String message, final Throwable e) {
  34 + final IOException wrappedException = new IOException(message + " [" +
  35 + e.getMessage() + "]");
  36 + wrappedException.setStackTrace(e.getStackTrace());
  37 + wrappedException.initCause(e);
  38 + return wrappedException;
  39 + }
  40 +}
1  ...idAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java
@@ -38,6 +38,7 @@
38 38 public static final String COLLECTION = Collection.class.getCanonicalName();
39 39 public static final String COLLECTIONS = Collections.class.getCanonicalName();
40 40 public static final String STRING = String.class.getCanonicalName();
  41 + public static final String STRING_SET = "java.util.Set<java.lang.String>";
41 42 public static final String CHAR_SEQUENCE = CharSequence.class.getCanonicalName();
42 43 public static final String SQL_EXCEPTION = SQLException.class.getCanonicalName();
43 44 public static final String INTEGER = Integer.class.getCanonicalName();
3  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java
@@ -94,7 +94,7 @@
94 94
95 95 private static final String METHOD_NAME_GET_ROOT_URL = "getRootUrl";
96 96
97   - private static final List<String> VALID_PREF_RETURN_TYPES = Arrays.asList("int", "boolean", "float", "long", CanonicalNameConstants.STRING);
  97 + private static final List<String> VALID_PREF_RETURN_TYPES = Arrays.asList("int", "boolean", "float", "long", CanonicalNameConstants.STRING, CanonicalNameConstants.STRING_SET);
98 98
99 99 private static final List<String> INVALID_PREF_METHOD_NAMES = Arrays.asList("edit", "getSharedPreferences", "clear", "getEditor", "apply");
100 100
@@ -756,6 +756,7 @@ public boolean isPrefMethod(Element element) {
756 756 } else {
757 757
758 758 String returnType = executableElement.getReturnType().toString();
  759 +
759 760 if (!VALID_PREF_RETURN_TYPES.contains(returnType)) {
760 761 annotationHelper.printError(element, "Method " + methodName + " should only return preference simple types in an " + annotationHelper.annotationName() + " annotated interface");
761 762 } else {
11 ...dAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
@@ -52,6 +52,8 @@
52 52 import org.androidannotations.api.sharedpreferences.SharedPreferencesHelper;
53 53 import org.androidannotations.api.sharedpreferences.StringPrefEditorField;
54 54 import org.androidannotations.api.sharedpreferences.StringPrefField;
  55 +import org.androidannotations.api.sharedpreferences.StringSetPrefEditorField;
  56 +import org.androidannotations.api.sharedpreferences.StringSetPrefField;
55 57 import org.androidannotations.helper.CanonicalNameConstants;
56 58 import org.androidannotations.helper.IdAnnotationHelper;
57 59 import org.androidannotations.helper.ModelConstants;
@@ -91,6 +93,7 @@ public EditorFieldHolder(Class<?> fieldClass, String fieldMethodName) {
91 93 put("int", new EditorFieldHolder(IntPrefEditorField.class, "intField"));
92 94 put("long", new EditorFieldHolder(LongPrefEditorField.class, "longField"));
93 95 put(CanonicalNameConstants.STRING, new EditorFieldHolder(StringPrefEditorField.class, "stringField"));
  96 + put(CanonicalNameConstants.STRING_SET, new EditorFieldHolder(StringSetPrefEditorField.class, "stringSetField"));
94 97 }
95 98 };
96 99
@@ -257,6 +260,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
257 260 defaultValue = JExpr.lit("");
258 261 }
259 262 addFieldHelperMethod(helperClass, fieldName, defaultValue, StringPrefField.class, "stringField");
  263 + } else if (CanonicalNameConstants.STRING_SET.equals(returnType)) {
  264 + addFieldHelperMethod(helperClass, fieldName, StringSetPrefField.class, "stringSetField");
260 265 }
261 266 }
262 267 }
@@ -281,6 +286,11 @@ private JExpression extractResValue(EBeanHolder eBeanHolder, Element method, JFi
281 286 return contextField.invoke("getResources").invoke(resourceGetMethodName).arg(idRef);
282 287 }
283 288
  289 + private void addFieldHelperMethod(JDefinedClass helperClass, String fieldName, Class<?> prefFieldHelperClass, String fieldHelperMethodName) {
  290 + JMethod fieldMethod = helperClass.method(JMod.PUBLIC, prefFieldHelperClass, fieldName);
  291 + fieldMethod.body()._return(JExpr.invoke(fieldHelperMethodName).arg(fieldName));
  292 + }
  293 +
284 294 private void addFieldHelperMethod(JDefinedClass helperClass, String fieldName, JExpression defaultValue, Class<?> prefFieldHelperClass, String fieldHelperMethodName) {
285 295 JMethod fieldMethod = helperClass.method(JMod.PUBLIC, prefFieldHelperClass, fieldName);
286 296 fieldMethod.body()._return(JExpr.invoke(fieldHelperMethodName).arg(fieldName).arg(defaultValue));
@@ -312,5 +322,4 @@ private JMethod getLocalClassName(EBeansHolder eBeansHolder, JDefinedClass helpe
312 322
313 323 return getLocalClassName;
314 324 }
315   -
316 325 }
6 ...s/androidannotations/src/test/java/org/androidannotations/generation/SharedPrefsApiDependenciesTest.java
@@ -33,6 +33,8 @@
33 33 import org.androidannotations.api.sharedpreferences.SharedPreferencesHelper;
34 34 import org.androidannotations.api.sharedpreferences.StringPrefEditorField;
35 35 import org.androidannotations.api.sharedpreferences.StringPrefField;
  36 +import org.androidannotations.api.sharedpreferences.StringSetPrefEditorField;
  37 +import org.androidannotations.api.sharedpreferences.StringSetPrefField;
36 38 import org.androidannotations.manifest.SomeClass;
37 39 import org.androidannotations.utils.AAProcessorTestHelper;
38 40 import org.junit.Before;
@@ -54,7 +56,9 @@
54 56 SharedPreferencesCompat.class,//
55 57 SharedPreferencesHelper.class,//
56 58 StringPrefEditorField.class,//
57   - StringPrefField.class //
  59 + StringPrefField.class, //
  60 + StringSetPrefEditorField.class,//
  61 + StringSetPrefField.class //
58 62 };
59 63
60 64 @Before
49 ...tions/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/prefs/PrefsActivityTest.java
@@ -16,6 +16,13 @@
16 16 package org.androidannotations.test15.prefs;
17 17
18 18 import static org.fest.assertions.Assertions.assertThat;
  19 +import static org.junit.Assert.fail;
  20 +
  21 +import java.io.IOException;
  22 +import java.io.Serializable;
  23 +import java.util.HashSet;
  24 +import java.util.Set;
  25 +import java.util.TreeSet;
19 26
20 27 import org.junit.Before;
21 28 import org.junit.Test;
@@ -24,6 +31,7 @@
24 31 import android.content.SharedPreferences;
25 32
26 33 import org.androidannotations.test15.AndroidAnnotationsTestRunner;
  34 +import org.apache.pig.impl.util.ObjectSerializer;
27 35
28 36 @RunWith(AndroidAnnotationsTestRunner.class)
29 37 public class PrefsActivityTest {
@@ -71,6 +79,23 @@ public void putLong() {
71 79 }
72 80
73 81 @Test
  82 + public void putStringSet() {
  83 + Set<String> values = new TreeSet<String>();
  84 + values.add("1");
  85 + values.add("2");
  86 + values.add("3");
  87 + values.add("4");
  88 + values.add("5");
  89 +
  90 + somePrefs.types().put(values);
  91 + try {
  92 + assertThat(ObjectSerializer.deserialize(sharedPref.getString("types", null))).isEqualTo(values);
  93 + } catch (IOException e) {
  94 + fail("IO exception while deserializing the object");
  95 + }
  96 + }
  97 +
  98 + @Test
74 99 public void editLong() {
75 100 long now = System.currentTimeMillis();
76 101
@@ -149,6 +174,30 @@ public void getLong() {
149 174 }
150 175
151 176 @Test
  177 + public void getStringSet() {
  178 + Set<String> values = new TreeSet<String>();
  179 + values.add("1");
  180 + values.add("2");
  181 + values.add("3");
  182 + values.add("4");
  183 + values.add("5");
  184 +
  185 + try {
  186 + sharedPref.edit()
  187 + .putString("types", ObjectSerializer.serialize((Serializable) values))
  188 + .commit();
  189 + } catch (IOException e) {
  190 + fail("Error while serializing string set: " + e.toString());
  191 + }
  192 +
  193 + Set<String> set = somePrefs.types().get();
  194 + int i = 0;
  195 + for(String v : set) {
  196 + assertThat(v).isEqualTo(new Integer(++i).toString());
  197 + }
  198 + }
  199 +
  200 + @Test
152 201 public void defaultValue() {
153 202 assertThat(somePrefs.name().get()).isEqualTo("John");
154 203 }
3  AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/SomePrefs.java
@@ -15,6 +15,8 @@
15 15 */
16 16 package org.androidannotations.test15.prefs;
17 17
  18 +import java.util.Set;
  19 +
18 20 import org.androidannotations.annotations.sharedpreferences.DefaultBoolean;
19 21 import org.androidannotations.annotations.sharedpreferences.DefaultFloat;
20 22 import org.androidannotations.annotations.sharedpreferences.DefaultInt;
@@ -43,4 +45,5 @@
43 45
44 46 long lastUpdated();
45 47
  48 + Set<String> types();
46 49 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.