Skip to content

Commit c6c7539

Browse files
committed
Use LocalDatastoreHelper for integration tests.
1 parent 5328ac0 commit c6c7539

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Persistence.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,26 @@
2020
import com.google.cloud.datastore.DatastoreOptions;
2121
import com.google.cloud.datastore.KeyFactory;
2222

23+
import java.util.concurrent.atomic.AtomicReference;
24+
2325
//[START all]
2426
public class Persistence {
27+
private static AtomicReference<Datastore> datastore = new AtomicReference<>();
28+
2529
public static Datastore getDatastore() {
26-
return DatastoreOptions.builder().projectId("your-project-id-here").build().service();
30+
if (datastore.get() == null) {
31+
datastore.set(DatastoreOptions.builder().projectId("your-project-id-here").build().service());
32+
}
33+
34+
return datastore.get();
2735
}
2836

2937
public static KeyFactory getKeyFactory(Class<?> c) {
3038
return getDatastore().newKeyFactory().kind(c.getSimpleName());
3139
}
40+
41+
public static void setDatastore(Datastore datastore) {
42+
Persistence.datastore.set(datastore);
43+
}
3244
}
3345
//[END all]

appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/GreetingTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.junit.Assert.assertTrue;
2020

2121
import java.util.List;
22+
23+
import org.junit.After;
2224
import org.junit.Before;
2325
import org.junit.Test;
2426
import org.junit.runner.RunWith;
@@ -28,7 +30,7 @@
2830
public class GreetingTest {
2931
@Before
3032
public void setUp() {
31-
TestUtils.wipeDatastore();
33+
TestUtils.startDatastore();
3234
}
3335

3436
@Test
@@ -41,4 +43,9 @@ public void testSaveGreeting() throws Exception {
4143
assertTrue(greetings.size() == 1);
4244
assertEquals(greeting, greetings.get(0));
4345
}
46+
47+
@After
48+
public void tearDown() {
49+
TestUtils.stopDatastore();
50+
}
4451
}

appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.List;
2424
import javax.servlet.http.HttpServletRequest;
2525
import javax.servlet.http.HttpServletResponse;
26+
27+
import org.junit.After;
2628
import org.junit.Before;
2729
import org.junit.Test;
2830
import org.junit.runner.RunWith;
@@ -46,7 +48,7 @@ public void setUp() throws Exception {
4648
helper.setUp();
4749

4850
signGuestbookServlet = new SignGuestbookServlet();
49-
TestUtils.wipeDatastore();
51+
TestUtils.startDatastore();
5052
}
5153

5254
@Test
@@ -63,4 +65,9 @@ public void doPost_userNotLoggedIn() throws Exception {
6365
assertTrue(greetings.size() == 1);
6466
assertTrue(greetings.get(0).content.equals(testGreeting));
6567
}
68+
69+
@After
70+
public void tearDown() {
71+
TestUtils.stopDatastore();
72+
}
6673
}

appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/TestUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,33 @@
66
import com.google.cloud.datastore.Key;
77
import com.google.cloud.datastore.Query;
88
import com.google.cloud.datastore.QueryResults;
9+
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
910
import com.google.common.collect.Lists;
11+
12+
import java.io.IOException;
1013
import java.util.ArrayList;
1114

1215
public class TestUtils {
16+
static LocalDatastoreHelper datastore = LocalDatastoreHelper.create();
17+
18+
public static void startDatastore() {
19+
try {
20+
datastore.start();
21+
Persistence.setDatastore(datastore.options().service());
22+
} catch (IOException | InterruptedException e) {
23+
throw new RuntimeException(e);
24+
}
25+
}
26+
27+
public static void stopDatastore() {
28+
try {
29+
datastore.stop();
30+
Persistence.setDatastore(null);
31+
} catch (IOException | InterruptedException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
1336
public static void wipeDatastore() {
1437
Datastore datastore = getDatastore();
1538
QueryResults<Key> guestbooks = datastore.run(Query.keyQueryBuilder().kind("Greeting").build());

0 commit comments

Comments
 (0)