File tree Expand file tree Collapse file tree 4 files changed +52
-3
lines changed
appengine/guestbook-cloud-datastore/src
main/java/com/example/guestbook
test/java/com/example/guestbook Expand file tree Collapse file tree 4 files changed +52
-3
lines changed Original file line number Diff line number Diff line change 2020import com .google .cloud .datastore .DatastoreOptions ;
2121import com .google .cloud .datastore .KeyFactory ;
2222
23+ import java .util .concurrent .atomic .AtomicReference ;
24+
2325//[START all]
2426public 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]
Original file line number Diff line number Diff line change 1919import static org .junit .Assert .assertTrue ;
2020
2121import java .util .List ;
22+
23+ import org .junit .After ;
2224import org .junit .Before ;
2325import org .junit .Test ;
2426import org .junit .runner .RunWith ;
2830public 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}
Original file line number Diff line number Diff line change 2323import java .util .List ;
2424import javax .servlet .http .HttpServletRequest ;
2525import javax .servlet .http .HttpServletResponse ;
26+
27+ import org .junit .After ;
2628import org .junit .Before ;
2729import org .junit .Test ;
2830import 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}
Original file line number Diff line number Diff line change 66import com .google .cloud .datastore .Key ;
77import com .google .cloud .datastore .Query ;
88import com .google .cloud .datastore .QueryResults ;
9+ import com .google .cloud .datastore .testing .LocalDatastoreHelper ;
910import com .google .common .collect .Lists ;
11+
12+ import java .io .IOException ;
1013import java .util .ArrayList ;
1114
1215public 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 ());
You can’t perform that action at this time.
0 commit comments