Hello, ${fn:escapeXml(user.nickname)}! (You can + sign out.)
+<% + } else { +%> +Hello! + Sign in + to include your name with greetings you post.
+<% + } +%> + +<%-- //[START datastore]--%> +<% + // Create the correct Ancestor key + KeyGuestbook '${fn:escapeXml(guestbookName)}' has no messages.
+<% + } else { +%> +Messages in Guestbook '${fn:escapeXml(guestbookName)}'.
+<% + // Look at all of our greetings + for (Greeting greeting : greetings) { + pageContext.setAttribute("greeting_content", greeting.content); + String author; + if (greeting.authorEmail == null) { + author = "An anonymous person"; + } else { + author = greeting.authorEmail; + String author_id = greeting.authorId; + if (user != null && user.getUserId().equals(author_id)) { + author += " (You)"; + } + } + pageContext.setAttribute("greeting_user", author); +%> +${fn:escapeXml(greeting_user)} wrote:
+${fn:escapeXml(greeting_content)}+<% + } + } +%> + + +<%-- //[END datastore]--%> + + + + +<%-- //[END all]--%> diff --git a/appengine/multitenancy/src/main/webapp/stylesheets/main.css b/appengine/multitenancy/src/main/webapp/stylesheets/main.css new file mode 100644 index 00000000000..05d72d5536d --- /dev/null +++ b/appengine/multitenancy/src/main/webapp/stylesheets/main.css @@ -0,0 +1,4 @@ +body { + font-family: Verdana, Helvetica, sans-serif; + background-color: #FFFFCC; +} diff --git a/appengine/multitenancy/src/test/java/com/example/appengine/GreetingTest.java b/appengine/multitenancy/src/test/java/com/example/appengine/GreetingTest.java new file mode 100644 index 00000000000..a93bfe788ab --- /dev/null +++ b/appengine/multitenancy/src/test/java/com/example/appengine/GreetingTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * 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.example.appengine; + +import static com.example.appengine.GuestbookTestUtilities.cleanDatastore; +import static org.junit.Assert.assertEquals; + +import com.google.appengine.api.datastore.DatastoreService; +import com.google.appengine.api.datastore.DatastoreServiceFactory; +import com.google.appengine.api.datastore.Entity; +import com.google.appengine.api.datastore.KeyFactory; +import com.google.appengine.api.datastore.PreparedQuery; +import com.google.appengine.api.datastore.Query; +import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; + +import com.googlecode.objectify.ObjectifyService; +import com.googlecode.objectify.util.Closeable; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + + +@RunWith(JUnit4.class) +public class GreetingTest { + private static final String TEST_CONTENT = "The world is Blue today"; + + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper( + // Set no eventual consistency, that way queries return all results. + // https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests + new LocalDatastoreServiceTestConfig() + .setDefaultHighRepJobPolicyUnappliedJobPercentage(0)); + + private Closeable closeable; + private DatastoreService ds; + + @Before + public void setUp() throws Exception { + + helper.setUp(); + ds = DatastoreServiceFactory.getDatastoreService(); + + ObjectifyService.register(Guestbook.class); + ObjectifyService.register(Greeting.class); + + closeable = ObjectifyService.begin(); + + cleanDatastore(ds, "default"); + } + + @After + public void tearDown() { + cleanDatastore(ds, "default"); + helper.tearDown(); + closeable.close(); + } + + @Test + public void createSaveObject() throws Exception { + + Greeting g = new Greeting("default", TEST_CONTENT); + ObjectifyService.ofy().save().entity(g).now(); + + Query query = new Query("Greeting") + .setAncestor(new KeyFactory.Builder("Guestbook", "default").getKey()); + PreparedQuery pq = ds.prepare(query); + Entity greeting = pq.asSingleEntity(); // Should only be one at this point. + assertEquals(greeting.getProperty("content"), TEST_CONTENT); + } +} diff --git a/appengine/multitenancy/src/test/java/com/example/appengine/GuestbookTestUtilities.java b/appengine/multitenancy/src/test/java/com/example/appengine/GuestbookTestUtilities.java new file mode 100644 index 00000000000..99a40b5fe49 --- /dev/null +++ b/appengine/multitenancy/src/test/java/com/example/appengine/GuestbookTestUtilities.java @@ -0,0 +1,43 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * 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.example.appengine; + +import com.google.appengine.api.datastore.DatastoreService; +import com.google.appengine.api.datastore.Entity; +import com.google.appengine.api.datastore.FetchOptions; +import com.google.appengine.api.datastore.Key; +import com.google.appengine.api.datastore.KeyFactory; +import com.google.appengine.api.datastore.PreparedQuery; +import com.google.appengine.api.datastore.Query; + +import java.util.ArrayList; +import java.util.List; + +public class GuestbookTestUtilities { + + public static void cleanDatastore(DatastoreService ds, String book) { + Query query = new Query("Greeting") + .setAncestor(new KeyFactory.Builder("Guestbook", book) + .getKey()).setKeysOnly(); + PreparedQuery pq = ds.prepare(query); + List