diff --git a/appengine/guestbook-cloud-datastore/README.md b/appengine/guestbook-cloud-datastore/README.md index 94b3edf3de9..1c6d6957c8a 100644 --- a/appengine/guestbook-cloud-datastore/README.md +++ b/appengine/guestbook-cloud-datastore/README.md @@ -19,10 +19,10 @@ Then start the [Cloud Datastore Emulator](https://cloud.google.com/datastore/doc Finally, in a new shell, [set the Datastore Emulator environmental variables](https://cloud.google.com/datastore/docs/tools/datastore-emulator#setting_environment_variables) and run - mvn clean appengine:devserver + mvn clean appengine:run ## Deploying Modify `appengine-web.xml` to reflect your app ID and version, then: - mvn clean appengine:update + mvn clean appengine:deploy diff --git a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java index 988118174c0..12eb3d6c6de 100644 --- a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java +++ b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Greeting.java @@ -1,16 +1,14 @@ /** * 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 + *
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 + *
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 + *
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. */ @@ -25,6 +23,8 @@ import com.google.cloud.datastore.FullEntity.Builder; import com.google.cloud.datastore.IncompleteKey; import com.google.cloud.datastore.Key; +import com.google.common.base.MoreObjects; + import java.util.Date; import java.util.Objects; @@ -32,7 +32,6 @@ public class Greeting { private Guestbook book; public Key key; - public String authorEmail; public String authorId; public String content; @@ -54,11 +53,6 @@ public Greeting(String book, String content, String id, String email) { authorId = id; } - /** - * Load greeting from Datastore entity - * - * @param entity - */ public Greeting(Entity entity) { key = entity.hasKey() ? entity.key() : null; authorEmail = entity.contains("authorEmail") ? entity.getString("authorEmail") : null; @@ -102,16 +96,28 @@ public boolean equals(Object o) { return false; } Greeting greeting = (Greeting) o; - return Objects.equals(key, greeting.key) && - Objects.equals(authorEmail, greeting.authorEmail) && - Objects.equals(authorId, greeting.authorId) && - Objects.equals(content, greeting.content) && - Objects.equals(date, greeting.date); + return Objects.equals(key, greeting.key) + && Objects.equals(authorEmail, greeting.authorEmail) + && Objects.equals(authorId, greeting.authorId) + && Objects.equals(content, greeting.content) + && Objects.equals(date, greeting.date); } @Override public int hashCode() { return Objects.hash(key, authorEmail, authorId, content, date); } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("key", key) + .add("authorEmail", authorEmail) + .add("authorId", authorId) + .add("content", content) + .add("date", date) + .add("book", book) + .toString(); + } } -//[END all] \ No newline at end of file +//[END all] diff --git a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java index 694010ab5c5..ae407175ecb 100644 --- a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java +++ b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/Guestbook.java @@ -1,19 +1,16 @@ /** * 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 + *
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 + *
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 + *
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.guestbook;
import static com.example.guestbook.Persistence.getDatastore;
@@ -27,20 +24,25 @@
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
+import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
+
import java.util.List;
+import java.util.Objects;
//[START all]
public class Guestbook {
- private static final KeyFactory kf = getKeyFactory(Guestbook.class);
-
+ private static final KeyFactory keyFactory = getKeyFactory(Guestbook.class);
private final Key key;
+
public final String book;
public Guestbook(String book) {
this.book = book == null ? "default" : book;
- key = kf.newKey(this.book); // There is a 1:1 mapping between Guestbook names and Guestbook objects
+ key =
+ keyFactory.newKey(
+ this.book); // There is a 1:1 mapping between Guestbook names and Guestbook objects
}
public Key getKey() {
@@ -49,12 +51,13 @@ public Key getKey() {
public List 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
+ * 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
+ * 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.guestbook;
import com.google.cloud.datastore.Datastore;
@@ -34,12 +31,12 @@ public static Datastore getDatastore() {
return datastore.get();
}
- public static KeyFactory getKeyFactory(Class> c) {
- return getDatastore().newKeyFactory().kind(c.getSimpleName());
- }
-
public static void setDatastore(Datastore datastore) {
Persistence.datastore.set(datastore);
}
+
+ public static KeyFactory getKeyFactory(Class> c) {
+ return getDatastore().newKeyFactory().kind(c.getSimpleName());
+ }
}
//[END all]
diff --git a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java
index eec1a34c2a8..8d6061a7146 100644
--- a/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java
+++ b/appengine/guestbook-cloud-datastore/src/main/java/com/example/guestbook/SignGuestbookServlet.java
@@ -1,16 +1,14 @@
/**
* 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
+ * 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
+ * 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
+ * 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.
*/
@@ -20,6 +18,7 @@
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
+
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -29,12 +28,11 @@
public class SignGuestbookServlet extends HttpServlet {
// Process the HTTP POST of the form
@Override
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws IOException {
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Greeting greeting;
UserService userService = UserServiceFactory.getUserService();
- User user = userService.getCurrentUser(); // Find out who the user is.
+ User user = userService.getCurrentUser(); // Find out who the user is.
String guestbookName = req.getParameter("guestbookName");
String content = req.getParameter("content");
diff --git a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml
index 14ea1140aa7..bdcb11ebd86 100644
--- a/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml
+++ b/appengine/guestbook-cloud-datastore/src/main/webapp/WEB-INF/appengine-web.xml
@@ -1,10 +1,8 @@
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
+ * 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
+ * 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.guestbook;
@@ -18,16 +16,17 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import java.util.List;
-
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
+import java.util.List;
+
@RunWith(JUnit4.class)
public class GreetingTest {
+
@Before
public void setUp() {
TestUtils.startDatastore();
diff --git a/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java b/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java
index 03aaa73bff6..243fca2a858 100644
--- a/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java
+++ b/appengine/guestbook-cloud-datastore/src/test/java/com/example/guestbook/SignGuestbookServletTest.java
@@ -1,29 +1,22 @@
/**
* 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
+ * 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
+ * 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
+ * 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.guestbook;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
-import java.util.List;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -32,6 +25,10 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
@RunWith(JUnit4.class)
public class SignGuestbookServletTest {
private final LocalServiceTestHelper helper = new LocalServiceTestHelper();