From f42c272850d3a9bcc312b24596d1c2482157bda9 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Fri, 15 May 2015 11:29:08 -0700 Subject: [PATCH] Merging in updated unit testing samples --- unittests/pom.xml | 76 +++++------------- .../src/main/webapp/WEB-INF/appengine-web.xml | 4 +- unittests/src/main/webapp/WEB-INF/queue.xml | 10 +++ unittests/src/main/webapp/WEB-INF/web.xml | 9 ++- .../{unittest => }/AuthenticationTest.java | 47 +++++------ .../appengine/samples/DeferredTaskTest.java | 74 +++++++++++++++++ ...LocalCustomPolicyHighRepDatastoreTest.java | 75 ++++++++++++++++++ .../{unittest => }/LocalDatastoreTest.java | 61 +++++++------- .../samples/LocalHighRepDatastoreTest.java | 60 ++++++++++++++ .../{unittest => }/LocalMemcacheTest.java | 71 +++++++++-------- .../samples/{unittest => }/MyFirstTest.java | 16 ++-- .../google/appengine/samples/OAuthTest.java | 67 ++++++++++++++++ .../CapabilitiesTest.java => ShortTest.java} | 56 ++++++------- .../samples/TaskQueueConfigTest.java | 73 +++++++++++++++++ .../appengine/samples/TaskQueueTest.java | 72 +++++++++++++++++ .../samples/unittest/DeferredTaskTest.java | 73 ----------------- ...LocalCustomPolicyHighRepDatastoreTest.java | 79 ------------------- .../unittest/LocalHighRepDatastoreTest.java | 63 --------------- .../appengine/samples/unittest/OAuthTest.java | 65 --------------- .../samples/unittest/TaskQueueTest.java | 72 ----------------- 20 files changed, 588 insertions(+), 535 deletions(-) create mode 100644 unittests/src/main/webapp/WEB-INF/queue.xml rename unittests/src/test/java/com/google/appengine/samples/{unittest => }/AuthenticationTest.java (62%) create mode 100644 unittests/src/test/java/com/google/appengine/samples/DeferredTaskTest.java create mode 100644 unittests/src/test/java/com/google/appengine/samples/LocalCustomPolicyHighRepDatastoreTest.java rename unittests/src/test/java/com/google/appengine/samples/{unittest => }/LocalDatastoreTest.java (55%) create mode 100644 unittests/src/test/java/com/google/appengine/samples/LocalHighRepDatastoreTest.java rename unittests/src/test/java/com/google/appengine/samples/{unittest => }/LocalMemcacheTest.java (51%) rename unittests/src/test/java/com/google/appengine/samples/{unittest => }/MyFirstTest.java (80%) create mode 100644 unittests/src/test/java/com/google/appengine/samples/OAuthTest.java rename unittests/src/test/java/com/google/appengine/samples/{unittest/CapabilitiesTest.java => ShortTest.java} (55%) create mode 100644 unittests/src/test/java/com/google/appengine/samples/TaskQueueConfigTest.java create mode 100644 unittests/src/test/java/com/google/appengine/samples/TaskQueueTest.java delete mode 100644 unittests/src/test/java/com/google/appengine/samples/unittest/DeferredTaskTest.java delete mode 100644 unittests/src/test/java/com/google/appengine/samples/unittest/LocalCustomPolicyHighRepDatastoreTest.java delete mode 100644 unittests/src/test/java/com/google/appengine/samples/unittest/LocalHighRepDatastoreTest.java delete mode 100644 unittests/src/test/java/com/google/appengine/samples/unittest/OAuthTest.java delete mode 100644 unittests/src/test/java/com/google/appengine/samples/unittest/TaskQueueTest.java diff --git a/unittests/pom.xml b/unittests/pom.xml index 85e2965ccd8..d0876d1d31c 100644 --- a/unittests/pom.xml +++ b/unittests/pom.xml @@ -5,25 +5,21 @@ war 1.0-SNAPSHOT - com.google.appengine.samples.unittest - unittests + com.google.appengine.samples + appengine-local-testing-samples 1 - 1.9.18 + 1.8.4 UTF-8 - - 3.1.0 - - com.google.appengine appengine-api-1.0-sdk - ${appengine.version} + ${appengine.target.version} javax.servlet @@ -41,54 +37,45 @@ junit junit - 4.12 + 4.10 test com.google.appengine appengine-testing - ${appengine.version} + ${appengine.target.version} test com.google.appengine appengine-api-stubs - ${appengine.version} + ${appengine.target.version} + test + + + com.google.appengine + appengine-tools-sdk + ${appengine.target.version} test - - ${project.build.directory}/${project.build.finalName}/WEB-INF/classes - - org.codehaus.mojo - versions-maven-plugin - 2.1 - - - compile - - display-dependency-updates - display-plugin-updates - - - - org.apache.maven.plugins - 3.1 + 2.5.1 maven-compiler-plugin 1.7 1.7 + org.apache.maven.plugins maven-war-plugin - 2.4 + 2.3 true @@ -105,36 +92,9 @@ com.google.appengine appengine-maven-plugin - ${appengine.version} - - false - - - - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - 2.15 - - - checkstyle - validate - - check - - - true - - - + ${appengine.target.version} + diff --git a/unittests/src/main/webapp/WEB-INF/appengine-web.xml b/unittests/src/main/webapp/WEB-INF/appengine-web.xml index 8bf645d1013..8337cc136a9 100644 --- a/unittests/src/main/webapp/WEB-INF/appengine-web.xml +++ b/unittests/src/main/webapp/WEB-INF/appengine-web.xml @@ -1,9 +1,9 @@ - your-app-id + appengine-local-testing-samples ${appengine.app.version} true - + diff --git a/unittests/src/main/webapp/WEB-INF/queue.xml b/unittests/src/main/webapp/WEB-INF/queue.xml new file mode 100644 index 00000000000..a0d8b77c060 --- /dev/null +++ b/unittests/src/main/webapp/WEB-INF/queue.xml @@ -0,0 +1,10 @@ + + + default + 1/s + + + my-queue-name + 3/s + + diff --git a/unittests/src/main/webapp/WEB-INF/web.xml b/unittests/src/main/webapp/WEB-INF/web.xml index 28bf558b8fa..7c1c7c6e1c6 100644 --- a/unittests/src/main/webapp/WEB-INF/web.xml +++ b/unittests/src/main/webapp/WEB-INF/web.xml @@ -1,4 +1,7 @@ - - - + + diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/AuthenticationTest.java b/unittests/src/test/java/com/google/appengine/samples/AuthenticationTest.java similarity index 62% rename from unittests/src/test/java/com/google/appengine/samples/unittest/AuthenticationTest.java rename to unittests/src/test/java/com/google/appengine/samples/AuthenticationTest.java index 58bf71e9f1d..4ba6bf1b8d7 100644 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/AuthenticationTest.java +++ b/unittests/src/test/java/com/google/appengine/samples/AuthenticationTest.java @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.appengine.samples.unittest; +package com.google.appengine.samples; + +// [START AuthenticationTest] -// [START auth] import com.google.appengine.api.users.UserService; import com.google.appengine.api.users.UserServiceFactory; import com.google.appengine.tools.development.testing.LocalServiceTestHelper; @@ -24,28 +25,28 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; public class AuthenticationTest { - private final LocalServiceTestHelper helper = - new LocalServiceTestHelper(new LocalUserServiceTestConfig()) - .setEnvIsAdmin(true).setEnvIsLoggedIn(true); - - @Before - public void setUp() { - helper.setUp(); - } - - @After - public void tearDown() { - helper.tearDown(); - } - - @Test - public void testIsAdmin() { - UserService userService = UserServiceFactory.getUserService(); - assertTrue(userService.isUserAdmin()); - } + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalUserServiceTestConfig()) + .setEnvIsAdmin(true).setEnvIsLoggedIn(true); + + @Before + public void setUp() { + helper.setUp(); + } + + @After + public void tearDown() { + helper.tearDown(); + } + + @Test + public void testIsAdmin() { + UserService userService = UserServiceFactory.getUserService(); + assertTrue(userService.isUserAdmin()); + } } -// [END auth] +// [END AuthenticationTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/DeferredTaskTest.java b/unittests/src/test/java/com/google/appengine/samples/DeferredTaskTest.java new file mode 100644 index 00000000000..661a9002605 --- /dev/null +++ b/unittests/src/test/java/com/google/appengine/samples/DeferredTaskTest.java @@ -0,0 +1,74 @@ +/** + * Copyright 2015 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.google.appengine.samples; + +// [START DeferredTaskTest] + +import com.google.appengine.api.taskqueue.DeferredTask; +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskOptions; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertTrue; + +public class DeferredTaskTest { + + // Unlike CountDownLatch, TaskCountDownlatch lets us reset. + private final LocalTaskQueueTestConfig.TaskCountDownLatch latch = + new LocalTaskQueueTestConfig.TaskCountDownLatch(1); + + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalTaskQueueTestConfig() + .setDisableAutoTaskExecution(false) + .setCallbackClass(LocalTaskQueueTestConfig.DeferredTaskCallback.class) + .setTaskExecutionLatch(latch)); + + private static class MyTask implements DeferredTask { + private static boolean taskRan = false; + + @Override + public void run() { + taskRan = true; + } + } + + @Before + public void setUp() { + helper.setUp(); + } + + @After + public void tearDown() { + MyTask.taskRan = false; + latch.reset(); + helper.tearDown(); + } + + @Test + public void testTaskGetsRun() throws InterruptedException { + QueueFactory.getDefaultQueue().add( + TaskOptions.Builder.withPayload(new MyTask())); + assertTrue(latch.await(5, TimeUnit.SECONDS)); + assertTrue(MyTask.taskRan); + } +} +// [END DeferredTaskTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/LocalCustomPolicyHighRepDatastoreTest.java b/unittests/src/test/java/com/google/appengine/samples/LocalCustomPolicyHighRepDatastoreTest.java new file mode 100644 index 00000000000..03f96629c52 --- /dev/null +++ b/unittests/src/test/java/com/google/appengine/samples/LocalCustomPolicyHighRepDatastoreTest.java @@ -0,0 +1,75 @@ +/** + * Copyright 2015 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.google.appengine.samples; + +// [START LocalCustomPolicyHighRepDatastoreTest] + +import com.google.appengine.api.datastore.*; +import com.google.appengine.api.datastore.dev.HighRepJobPolicy; +import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit; +import static org.junit.Assert.assertEquals; + +public class LocalCustomPolicyHighRepDatastoreTest { + private static final class CustomHighRepJobPolicy implements HighRepJobPolicy { + static int newJobCounter = 0; + static int existingJobCounter = 0; + + @Override + public boolean shouldApplyNewJob(Key entityGroup) { + // every other new job fails to apply + return newJobCounter++ % 2 == 0; + } + + @Override + public boolean shouldRollForwardExistingJob(Key entityGroup) { + // every other existing job fails to apply + return existingJobCounter++ % 2 == 0; + } + } + + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() + .setAlternateHighRepJobPolicyClass(CustomHighRepJobPolicy.class)); + + @Before + public void setUp() { + helper.setUp(); + } + + @After + public void tearDown() { + helper.tearDown(); + } + + @Test + public void testEventuallyConsistentGlobalQueryResult() { + DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); + ds.put(new Entity("yam")); // applies + ds.put(new Entity("yam")); // does not apply + // first global query only sees the first Entity + assertEquals(1, ds.prepare(new Query("yam")).countEntities(withLimit(10))); + // second global query sees both Entities because we "groom" (attempt to + // apply unapplied jobs) after every query + assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10))); + } +} +// [END LocalCustomPolicyHighRepDatastoreTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalDatastoreTest.java b/unittests/src/test/java/com/google/appengine/samples/LocalDatastoreTest.java similarity index 55% rename from unittests/src/test/java/com/google/appengine/samples/unittest/LocalDatastoreTest.java rename to unittests/src/test/java/com/google/appengine/samples/LocalDatastoreTest.java index a93c51951f4..6a3e1b64b68 100644 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalDatastoreTest.java +++ b/unittests/src/test/java/com/google/appengine/samples/LocalDatastoreTest.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.appengine.samples.unittest; +package com.google.appengine.samples; -// [START datastore_example_1] +// [START LocalDatastoreTest] import com.google.appengine.api.datastore.DatastoreService; import com.google.appengine.api.datastore.DatastoreServiceFactory; @@ -28,40 +28,41 @@ import org.junit.Test; import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; public class LocalDatastoreTest { - private final LocalServiceTestHelper helper = new - LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()); + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()); - @Before - public void setUp() { - helper.setUp(); - } + @Before + public void setUp() { + helper.setUp(); + } - @After - public void tearDown() { - helper.tearDown(); - } + @After + public void tearDown() { + helper.tearDown(); + } - // run this test twice to prove we're not leaking any state across tests - private void doTest() { - DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); - assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10))); - ds.put(new Entity("yam")); - ds.put(new Entity("yam")); - assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10))); - } + // run this test twice to prove we're not leaking any state across tests + private void doTest() { + DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); + assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10))); + ds.put(new Entity("yam")); + ds.put(new Entity("yam")); + assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10))); + } - @Test - public void testInsert1() { - doTest(); - } + @Test + public void testInsert1() { + doTest(); + } - @Test - public void testInsert2() { - doTest(); - } + @Test + public void testInsert2() { + doTest(); + } } -// [END datastore_example_1] \ No newline at end of file + +// [END LocalDatastoreTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/LocalHighRepDatastoreTest.java b/unittests/src/test/java/com/google/appengine/samples/LocalHighRepDatastoreTest.java new file mode 100644 index 00000000000..fbf98855bab --- /dev/null +++ b/unittests/src/test/java/com/google/appengine/samples/LocalHighRepDatastoreTest.java @@ -0,0 +1,60 @@ +/** + * Copyright 2015 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.google.appengine.samples; + +// [START LocalHighRepDatastoreTest] + +import com.google.appengine.api.datastore.*; +import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit; +import static org.junit.Assert.assertEquals; + +public class LocalHighRepDatastoreTest { + + // maximum eventual consistency + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() + .setDefaultHighRepJobPolicyUnappliedJobPercentage(100)); + + @Before + public void setUp() { + helper.setUp(); + } + + @After + public void tearDown() { + helper.tearDown(); + } + + @Test + public void testEventuallyConsistentGlobalQueryResult() { + DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); + Key ancestor = KeyFactory.createKey("foo", 3); + ds.put(new Entity("yam", ancestor)); + ds.put(new Entity("yam", ancestor)); + // global query doesn't see the data + assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10))); + // ancestor query does see the data + assertEquals(2, ds.prepare(new Query("yam", ancestor)).countEntities(withLimit(10))); + } +} +// [END LocalHighRepDatastoreTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalMemcacheTest.java b/unittests/src/test/java/com/google/appengine/samples/LocalMemcacheTest.java similarity index 51% rename from unittests/src/test/java/com/google/appengine/samples/unittest/LocalMemcacheTest.java rename to unittests/src/test/java/com/google/appengine/samples/LocalMemcacheTest.java index f94b869c8c1..0989eb28a3b 100644 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalMemcacheTest.java +++ b/unittests/src/test/java/com/google/appengine/samples/LocalMemcacheTest.java @@ -13,54 +13,59 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.appengine.samples.unittest; +package com.google.appengine.samples; -// [START memcache_example_1] + +// [START imports] import com.google.appengine.api.memcache.MemcacheService; import com.google.appengine.api.memcache.MemcacheServiceFactory; import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig; import com.google.appengine.tools.development.testing.LocalServiceTestHelper; -// [END memcache_example_1] +// [END imports] + import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; -// [START memcache_example_2] +// [START NameAndHelper] public class LocalMemcacheTest { - private final LocalServiceTestHelper helper = - new LocalServiceTestHelper(new LocalMemcacheServiceTestConfig()); - // [END memcache_example_2] + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalMemcacheServiceTestConfig()); + + // [END NameAndHelper] + + @Before + public void setUp() { + helper.setUp(); + } - @Before - public void setUp() { - helper.setUp(); - } + @After + public void tearDown() { + helper.tearDown(); + } - @After - public void tearDown() { - helper.tearDown(); - } + // run this test twice to prove we're not leaking any state across tests - // run this test twice to prove we're not leaking any state across tests - private void doTest() { - // [START memcache_example_3] - MemcacheService ms = MemcacheServiceFactory.getMemcacheService(); - assertFalse(ms.contains("yar")); - ms.put("yar", "foo"); - assertTrue(ms.contains("yar")); - // [END memcache_example_3] - } + // [START doTest] + private void doTest() { + MemcacheService ms = MemcacheServiceFactory.getMemcacheService(); + assertFalse(ms.contains("yar")); + ms.put("yar", "foo"); + assertTrue(ms.contains("yar")); + } + // [END doTest] - @Test - public void testInsert1() { - doTest(); - } + @Test + public void testInsert1() { + doTest(); + } - @Test - public void testInsert2() { - doTest(); - } + @Test + public void testInsert2() { + doTest(); + } } diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/MyFirstTest.java b/unittests/src/test/java/com/google/appengine/samples/MyFirstTest.java similarity index 80% rename from unittests/src/test/java/com/google/appengine/samples/unittest/MyFirstTest.java rename to unittests/src/test/java/com/google/appengine/samples/MyFirstTest.java index c4ae279751a..fa835d232b0 100644 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/MyFirstTest.java +++ b/unittests/src/test/java/com/google/appengine/samples/MyFirstTest.java @@ -13,17 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.appengine.samples.unittest; -// [START framework] +package com.google.appengine.samples; + +// [START MyFirstTest] + import org.junit.Test; import static org.junit.Assert.*; public class MyFirstTest { - @Test - public void testAddition() { - assertEquals(4, 2 + 2); - } + @Test + public void testAddition() { + assertEquals(4, 2 + 2); + } } -// [END framework] \ No newline at end of file +// [END MyFirstTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/OAuthTest.java b/unittests/src/test/java/com/google/appengine/samples/OAuthTest.java new file mode 100644 index 00000000000..81cb86fed58 --- /dev/null +++ b/unittests/src/test/java/com/google/appengine/samples/OAuthTest.java @@ -0,0 +1,67 @@ +/** + * Copyright 2015 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.google.appengine.samples; + +// [START OAuthTest] + +import com.google.appengine.api.oauth.OAuthRequestException; +import com.google.appengine.api.oauth.OAuthService; +import com.google.appengine.api.oauth.OAuthServiceFactory; +import com.google.appengine.api.users.User; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class OAuthTest { + private static final String OAUTH_CONSUMER_KEY = "notexample.com"; + private static final String OAUTH_EMAIL = "bozo@clown.com"; + private static final String OAUTH_USER_ID = "bozo"; + private static final String OAUTH_AUTH_DOMAIN = "clown.com"; + private static final boolean OAUTH_IS_ADMIN = true; + + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalUserServiceTestConfig() + .setOAuthConsumerKey(OAUTH_CONSUMER_KEY) + .setOAuthEmail(OAUTH_EMAIL) + .setOAuthUserId(OAUTH_USER_ID) + .setOAuthAuthDomain(OAUTH_AUTH_DOMAIN) + .setOAuthIsAdmin(OAUTH_IS_ADMIN)); + + @Before + public void setUp() { + helper.setUp(); + } + + @After + public void tearDown() { + helper.tearDown(); + } + + @Test + public void testConfig() throws OAuthRequestException { + OAuthService oauthService = OAuthServiceFactory.getOAuthService(); + assertEquals(OAUTH_CONSUMER_KEY, oauthService.getOAuthConsumerKey()); + assertEquals(new User(OAUTH_EMAIL, OAUTH_AUTH_DOMAIN, OAUTH_USER_ID), + oauthService.getCurrentUser()); + assertEquals(OAUTH_IS_ADMIN, oauthService.isUserAdmin()); + } +} + +// [END OAuthTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/CapabilitiesTest.java b/unittests/src/test/java/com/google/appengine/samples/ShortTest.java similarity index 55% rename from unittests/src/test/java/com/google/appengine/samples/unittest/CapabilitiesTest.java rename to unittests/src/test/java/com/google/appengine/samples/ShortTest.java index 7a2286f7e9e..6605668c6c8 100644 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/CapabilitiesTest.java +++ b/unittests/src/test/java/com/google/appengine/samples/ShortTest.java @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.appengine.samples.unittest; +package com.google.appengine.samples; + +// [START ShortTest] -// [START local_capabilities] import com.google.appengine.api.capabilities.Capability; import com.google.appengine.api.capabilities.CapabilityStatus; import com.google.appengine.api.datastore.DatastoreService; @@ -28,29 +29,30 @@ import org.junit.After; import org.junit.Test; -import static org.junit.Assert.*; - -public class CapabilitiesTest { - - private LocalServiceTestHelper helper; - - @After - public void tearDown() { - helper.tearDown(); - } - - @Test(expected = ApiProxy.CapabilityDisabledException.class) - public void testDisabledDatastore() { - Capability testOne = new Capability("datastore_v3"); - CapabilityStatus testStatus = CapabilityStatus.DISABLED; - //Initialize - LocalCapabilitiesServiceTestConfig config = - new LocalCapabilitiesServiceTestConfig().setCapabilityStatus(testOne, testStatus); - helper = new LocalServiceTestHelper(config); - helper.setUp(); - FetchOptions fo = FetchOptions.Builder.withLimit(10); - DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); - assertEquals(0, ds.prepare(new Query("yam")).countEntities(fo)); - } +import static org.junit.Assert.assertEquals; + +public class ShortTest { + private LocalServiceTestHelper helper; + + @After + public void tearDown() { + helper.tearDown(); + } + + + @Test(expected = ApiProxy.CapabilityDisabledException.class) + public void testDisabledDatastore() { + Capability testOne = new Capability("datastore_v3"); + CapabilityStatus testStatus = CapabilityStatus.DISABLED; + //Initialize + LocalCapabilitiesServiceTestConfig config = + new LocalCapabilitiesServiceTestConfig().setCapabilityStatus(testOne, testStatus); + helper = new LocalServiceTestHelper(config); + helper.setUp(); + FetchOptions fo = FetchOptions.Builder.withLimit(10); + DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); + assertEquals(0, ds.prepare(new Query("yam")).countEntities(fo)); + } } -// [END local_capabilities] + +// [END ShortTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/TaskQueueConfigTest.java b/unittests/src/test/java/com/google/appengine/samples/TaskQueueConfigTest.java new file mode 100644 index 00000000000..b056662eb5c --- /dev/null +++ b/unittests/src/test/java/com/google/appengine/samples/TaskQueueConfigTest.java @@ -0,0 +1,73 @@ +/** + * Copyright 2015 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.google.appengine.samples; + +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskOptions; +import com.google.appengine.api.taskqueue.dev.LocalTaskQueue; +import com.google.appengine.api.taskqueue.dev.QueueStateInfo; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TaskQueueConfigTest { + // [START LocalServiceTestHelper] + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalTaskQueueTestConfig() + .setQueueXmlPath("src/main/webapp/WEB-INF/queue.xml")); + //[END LocalServiceTestHelper] + + @Before + public void setUp() { + helper.setUp(); + } + + @After + public void tearDown() { + helper.tearDown(); + } + + + // Run this test twice to demonstrate we're not leaking state across tests. + // If we _are_ leaking state across tests we'll get an exception on the + // second test because there will already be a task with the given name. + private void doTest() throws InterruptedException { + // [START QueueFactory] + QueueFactory.getQueue("my-queue-name").add(TaskOptions.Builder.withTaskName("task29")); + // [END QueueFactory] + // give the task time to execute if tasks are actually enabled (which they + // aren't, but that's part of the test) + Thread.sleep(1000); + LocalTaskQueue ltq = LocalTaskQueueTestConfig.getLocalTaskQueue(); + QueueStateInfo qsi = ltq.getQueueStateInfo().get(QueueFactory.getQueue("my-queue-name").getQueueName()); + assertEquals(1, qsi.getTaskInfo().size()); + assertEquals("task29", qsi.getTaskInfo().get(0).getTaskName()); + } + + @Test + public void testTaskGetsScheduled1() throws InterruptedException { + doTest(); + } + + @Test + public void testTaskGetsScheduled2() throws InterruptedException { + doTest(); + } +} diff --git a/unittests/src/test/java/com/google/appengine/samples/TaskQueueTest.java b/unittests/src/test/java/com/google/appengine/samples/TaskQueueTest.java new file mode 100644 index 00000000000..21f0d1e326e --- /dev/null +++ b/unittests/src/test/java/com/google/appengine/samples/TaskQueueTest.java @@ -0,0 +1,72 @@ +/** + * Copyright 2015 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.google.appengine.samples; + +// [START TaskQueueTest] + +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskOptions; +import com.google.appengine.api.taskqueue.dev.LocalTaskQueue; +import com.google.appengine.api.taskqueue.dev.QueueStateInfo; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TaskQueueTest { + + private final LocalServiceTestHelper helper = + new LocalServiceTestHelper(new LocalTaskQueueTestConfig()); + + @Before + public void setUp() { + helper.setUp(); + } + + @After + public void tearDown() { + helper.tearDown(); + } + + + // Run this test twice to demonstrate we're not leaking state across tests. + // If we _are_ leaking state across tests we'll get an exception on the + // second test because there will already be a task with the given name. + private void doTest() throws InterruptedException { + QueueFactory.getDefaultQueue().add(TaskOptions.Builder.withTaskName("task29")); + // give the task time to execute if tasks are actually enabled (which they + // aren't, but that's part of the test) + Thread.sleep(1000); + LocalTaskQueue ltq = LocalTaskQueueTestConfig.getLocalTaskQueue(); + QueueStateInfo qsi = ltq.getQueueStateInfo().get(QueueFactory.getDefaultQueue().getQueueName()); + assertEquals(1, qsi.getTaskInfo().size()); + assertEquals("task29", qsi.getTaskInfo().get(0).getTaskName()); + } + + @Test + public void testTaskGetsScheduled1() throws InterruptedException { + doTest(); + } + + @Test + public void testTaskGetsScheduled2() throws InterruptedException { + doTest(); + } +} +// [END TaskQueueTest] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/DeferredTaskTest.java b/unittests/src/test/java/com/google/appengine/samples/unittest/DeferredTaskTest.java deleted file mode 100644 index d30f67cff1a..00000000000 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/DeferredTaskTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright 2015 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.google.appengine.samples.unittest; - -// [START taskqueue_example_2] -import com.google.appengine.api.taskqueue.DeferredTask; -import com.google.appengine.api.taskqueue.QueueFactory; -import com.google.appengine.api.taskqueue.TaskOptions; -import com.google.appengine.tools.development.testing.LocalServiceTestHelper; -import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.*; - -public class DeferredTaskTest { - - // Unlike CountDownLatch, TaskCountDownlatch lets us reset. - private final LocalTaskQueueTestConfig.TaskCountDownLatch latch = - new LocalTaskQueueTestConfig.TaskCountDownLatch(1); - - private final LocalServiceTestHelper helper = - new LocalServiceTestHelper(new LocalTaskQueueTestConfig() - .setDisableAutoTaskExecution(false) - .setCallbackClass(LocalTaskQueueTestConfig.DeferredTaskCallback.class) - .setTaskExecutionLatch(latch)); - - private static class MyTask implements DeferredTask { - private static boolean taskRan = false; - - @Override - public void run() { - taskRan = true; - } - } - - @Before - public void setUp() { - helper.setUp(); - } - - @After - public void tearDown() { - MyTask.taskRan = false; - latch.reset(); - helper.tearDown(); - } - - @Test - public void testTaskGetsRun() throws InterruptedException { - QueueFactory.getDefaultQueue().add( - TaskOptions.Builder.withPayload(new MyTask())); - assertTrue(latch.await(5, TimeUnit.SECONDS)); - assertTrue(MyTask.taskRan); - } -} -// [END taskqueue_example_2] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalCustomPolicyHighRepDatastoreTest.java b/unittests/src/test/java/com/google/appengine/samples/unittest/LocalCustomPolicyHighRepDatastoreTest.java deleted file mode 100644 index 0bb9505e4a4..00000000000 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalCustomPolicyHighRepDatastoreTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright 2015 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.google.appengine.samples.unittest; - -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.Key; -import com.google.appengine.api.datastore.Query; -import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; -import com.google.appengine.tools.development.testing.LocalServiceTestHelper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit; -import static org.junit.Assert.*; - -// [START HRD_example_2] -// ... -import com.google.appengine.api.datastore.dev.HighRepJobPolicy; - -public class LocalCustomPolicyHighRepDatastoreTest { - private static final class CustomHighRepJobPolicy implements HighRepJobPolicy { - static int newJobCounter = 0; - static int existingJobCounter = 0; - @Override - public boolean shouldApplyNewJob(Key entityGroup) { - // every other new job fails to apply - return newJobCounter++ % 2 == 0; - } - - @Override - public boolean shouldRollForwardExistingJob(Key entityGroup) { - // every other existing job fails to apply - return existingJobCounter++ % 2 == 0; - } - } - - private final LocalServiceTestHelper helper = - new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() - .setAlternateHighRepJobPolicyClass(CustomHighRepJobPolicy.class)); - - @Before - public void setUp() { - helper.setUp(); - } - - @After - public void tearDown() { - helper.tearDown(); - } - - @Test - public void testEventuallyConsistentGlobalQueryResult() { - DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); - ds.put(new Entity("yam")); // applies - ds.put(new Entity("yam")); // does not apply - // first global query only sees the first Entity - assertEquals(1, ds.prepare(new Query("yam")).countEntities(withLimit(10))); - // second global query sees both Entities because we "groom" (attempt to - // apply unapplied jobs) after every query - assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10))); - } -} -// [END HRD_example_2] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalHighRepDatastoreTest.java b/unittests/src/test/java/com/google/appengine/samples/unittest/LocalHighRepDatastoreTest.java deleted file mode 100644 index e66db4792c7..00000000000 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/LocalHighRepDatastoreTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Copyright 2015 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.google.appengine.samples.unittest; - -// [START HRD_example_1] -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.Key; -import com.google.appengine.api.datastore.KeyFactory; -import com.google.appengine.api.datastore.Query; -import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; -import com.google.appengine.tools.development.testing.LocalServiceTestHelper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit; -import static org.junit.Assert.*; - -public class LocalHighRepDatastoreTest { - - // maximum eventual consistency - private final LocalServiceTestHelper helper = - new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() - .setDefaultHighRepJobPolicyUnappliedJobPercentage(100)); - - @Before - public void setUp() { - helper.setUp(); - } - - @After - public void tearDown() { - helper.tearDown(); - } - - @Test - public void testEventuallyConsistentGlobalQueryResult() { - DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); - Key ancestor = KeyFactory.createKey("foo", 3); - ds.put(new Entity("yam", ancestor)); - ds.put(new Entity("yam", ancestor)); - // global query doesn't see the data - assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10))); - // ancestor query does see the data - assertEquals(2, ds.prepare(new Query("yam", ancestor)).countEntities(withLimit(10))); - } -} -// [END HRD_example_1] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/OAuthTest.java b/unittests/src/test/java/com/google/appengine/samples/unittest/OAuthTest.java deleted file mode 100644 index 6575fbfd412..00000000000 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/OAuthTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright 2015 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.google.appengine.samples.unittest; - -// [START oauth] -import com.google.appengine.api.oauth.OAuthRequestException; -import com.google.appengine.api.oauth.OAuthService; -import com.google.appengine.api.oauth.OAuthServiceFactory; -import com.google.appengine.api.users.User; -import com.google.appengine.tools.development.testing.LocalServiceTestHelper; -import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class OAuthTest { - private static final String OAUTH_CONSUMER_KEY = "notexample.com"; - private static final String OAUTH_EMAIL = "bozo@clown.com"; - private static final String OAUTH_USER_ID = "bozo"; - private static final String OAUTH_AUTH_DOMAIN = "clown.com"; - private static final boolean OAUTH_IS_ADMIN = true; - - private final LocalServiceTestHelper helper = - new LocalServiceTestHelper(new LocalUserServiceTestConfig() - .setOAuthConsumerKey(OAUTH_CONSUMER_KEY) - .setOAuthEmail(OAUTH_EMAIL) - .setOAuthUserId(OAUTH_USER_ID) - .setOAuthAuthDomain(OAUTH_AUTH_DOMAIN) - .setOAuthIsAdmin(OAUTH_IS_ADMIN)); - - @Before - public void setUp() { - helper.setUp(); - } - - @After - public void tearDown() { - helper.tearDown(); - } - - @Test - public void testConfig() throws OAuthRequestException { - OAuthService oauthService = OAuthServiceFactory.getOAuthService(); - assertEquals(OAUTH_CONSUMER_KEY, oauthService.getOAuthConsumerKey()); - assertEquals(new User(OAUTH_EMAIL, OAUTH_AUTH_DOMAIN, OAUTH_USER_ID), - oauthService.getCurrentUser()); - assertEquals(OAUTH_IS_ADMIN, oauthService.isUserAdmin()); - } -} -// [END oauth] diff --git a/unittests/src/test/java/com/google/appengine/samples/unittest/TaskQueueTest.java b/unittests/src/test/java/com/google/appengine/samples/unittest/TaskQueueTest.java deleted file mode 100644 index 4d2072043a3..00000000000 --- a/unittests/src/test/java/com/google/appengine/samples/unittest/TaskQueueTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Copyright 2015 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.google.appengine.samples.unittest; - -// [START taskqueue_example_1] -import com.google.appengine.api.taskqueue.QueueFactory; -import com.google.appengine.api.taskqueue.TaskOptions; -import com.google.appengine.api.taskqueue.dev.LocalTaskQueue; -import com.google.appengine.api.taskqueue.dev.QueueStateInfo; -import com.google.appengine.tools.development.testing.LocalServiceTestHelper; -import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class TaskQueueTest { - - private final LocalServiceTestHelper helper = - new LocalServiceTestHelper(new LocalTaskQueueTestConfig()); - - @Before - public void setUp() { - helper.setUp(); - } - - @After - public void tearDown() { - helper.tearDown(); - } - - - // Run this test twice to demonstrate we're not leaking state across tests. - // If we _are_ leaking state across tests we'll get an exception on the - // second test because there will already be a task with the given name. - private void doTest() throws InterruptedException { - QueueFactory.getDefaultQueue().add(TaskOptions.Builder.withTaskName("task29")); - // give the task time to execute if tasks are actually enabled (which they - // aren't, but that's part of the test) - Thread.sleep(1000); - LocalTaskQueue ltq = LocalTaskQueueTestConfig.getLocalTaskQueue(); - QueueStateInfo qsi = ltq.getQueueStateInfo().get(QueueFactory.getDefaultQueue() - .getQueueName()); - assertEquals(1, qsi.getTaskInfo().size()); - assertEquals("task29", qsi.getTaskInfo().get(0).getTaskName()); - } - - @Test - public void testTaskGetsScheduled1() throws InterruptedException { - doTest(); - } - - @Test - public void testTaskGetsScheduled2() throws InterruptedException { - doTest(); - } -} -// [END taskqueue_example_1]