Merging in updated unit testing samples by theacodes · Pull Request #9 · GoogleCloudPlatform/java-docs-samples · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 18 additions & 58 deletions unittests/pom.xml
4 changes: 2 additions & 2 deletions unittests/src/main/webapp/WEB-INF/appengine-web.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>your-app-id</application>
<application>appengine-local-testing-samples</application>
<version>${appengine.app.version}</version>
<threadsafe>true</threadsafe>

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
Expand Down
10 changes: 10 additions & 0 deletions unittests/src/main/webapp/WEB-INF/queue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<queue-entries>
<queue>
<name>default</name>
<rate>1/s</rate>
</queue>
<queue>
<name>my-queue-name</name>
<rate>3/s</rate>
</queue>
</queue-entries>
9 changes: 6 additions & 3 deletions unittests/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<?xml version="1.0" encoding="utf-8"?>
<web-app
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Original file line number Diff line number Diff line change
@@ -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]
Loading