My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2009 Jens Scheffler (appenginefan.com)
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing
* permissions and limitations under the License.
*/

package com.appenginefan.toolkit.persistence;

import java.util.List;
import java.util.Map.Entry;

import com.google.common.base.Function;
import com.google.common.base.Functions;

import junit.framework.TestCase;

/**
* A template for testing persistences that can digest
* Strings.
*/
public class StringPersistenceTest
extends TestCase {

private StringPersistence persistence;

/**
* will set up a MapBasedPersistence if persistence is
* null when called.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
persistence =
new StringPersistence(
new MapBasedPersistence<byte[]>());
}

public void testConversion() {
for (String s : new String[] { "regular String", "" }) {
assertEquals(s, persistence.makeType(persistence
.makeArray(s)));
}
}

public void testBasicSetAndGet() {
persistence.mutate("A", Functions.constant("A"));
assertEquals(persistence.get("A"), "A");
}

public void testOverwrite() {
persistence.mutate("A", Functions.constant("A"));
assertEquals("B", persistence.mutate("A", Functions
.constant("B")));
assertEquals(persistence.get("A"), "B");
}

public void testGetUnknownKey() {
assertNull(persistence.get("A"));
}

public void testDelete() {
persistence.mutate("A", Functions.constant("A"));
assertNull(persistence.mutate("A", Functions
.constant((String) null)));
assertNull(persistence.get("A"));
}

public void testFunctionInput() {
persistence.mutate("A", Functions.constant("B"));
persistence.mutate("A", new Function<String, String>() {
@Override
public String apply(String fromPersistence) {
assertEquals(fromPersistence, "B");
return "C";
}
});
assertEquals(persistence.get("A"), "C");
}

public void testScan() {
persistence.mutate("A1", Functions.constant("A1"));
persistence.mutate("A2", Functions.constant("A2"));
persistence.mutate("A3", Functions.constant("A3"));
persistence.mutate("A2", Functions
.constant((String) null));
List<Entry<String, String>> scanResult =
persistence.scan("A", "B", 10);
assertEquals(2, scanResult.size());
assertEquals("A1", scanResult.get(0).getKey());
assertEquals("A3", scanResult.get(1).getKey());
assertEquals("A1", scanResult.get(0).getValue());
assertEquals("A3", scanResult.get(1).getValue());
scanResult = persistence.scan("A1", "A3", 10);
assertEquals(1, scanResult.size());
assertEquals("A1", scanResult.get(0).getKey());
scanResult = persistence.scan("A1", "A4", 1);
assertEquals(1, scanResult.size());
assertEquals("A1", scanResult.get(0).getKey());
scanResult = persistence.scan("B", "Z", 10);
assertEquals(0, scanResult.size());
scanResult = persistence.scan("A1", "A4", 0);
assertEquals(0, scanResult.size());
}

public void testScanReverse() {
persistence.mutate("A1", Functions.constant("A1"));
persistence.mutate("A2", Functions.constant("A2"));
persistence.mutate("A3", Functions.constant("A3"));
persistence.mutate("A2", Functions
.constant((String) null));
List<Entry<String, String>> scanResult =
persistence.scanReverse("A", "B", 10);
assertEquals(2, scanResult.size());
assertEquals("A3", scanResult.get(0).getKey());
assertEquals("A1", scanResult.get(1).getKey());
assertEquals("A3", scanResult.get(0).getValue());
assertEquals("A1", scanResult.get(1).getValue());
scanResult = persistence.scanReverse("A1", "A3", 10);
assertEquals(1, scanResult.size());
assertEquals("A1", scanResult.get(0).getKey());
scanResult = persistence.scanReverse("A0", "A4", 1);
assertEquals(1, scanResult.size());
assertEquals("A3", scanResult.get(0).getKey());
scanResult = persistence.scanReverse("B", "Z", 10);
assertEquals(0, scanResult.size());
scanResult = persistence.scanReverse("A0", "A4", 0);
assertEquals(0, scanResult.size());
}
}

Change log

r9 by schefflerjens on Apr 23, 2009   Diff
added "scanReverse" method to Persistence
interface, making it more
useful for pagination
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by schefflerjens on Apr 22, 2009   Diff
Experimental implementation of an
efficient counter/id generator.
Not ready for real-life use yet (unit
test with in-memory App Engine
currently fails)
...
r3 by schefflerjens on Apr 18, 2009   Diff
Fixed some javadoc in
StringPersistenceTest

r2 by schefflerjens on Apr 17, 2009   Diff
Initial revision
All revisions of this file

File info

Size: 4665 bytes, 141 lines
Powered by Google Project Hosting