Skip to content

Commit 6b02e53

Browse files
committed
Merge pull request #9 from jonparrott/master
Merging in updated unit testing samples
2 parents d06fb85 + f42c272 commit 6b02e53

20 files changed

+588
-535
lines changed

unittests/pom.xml

+18-58
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
55
<packaging>war</packaging>
66
<version>1.0-SNAPSHOT</version>
77

8-
<groupId>com.google.appengine.samples.unittest</groupId>
9-
<artifactId>unittests</artifactId>
8+
<groupId>com.google.appengine.samples</groupId>
9+
<artifactId>appengine-local-testing-samples</artifactId>
1010

1111
<properties>
1212
<appengine.app.version>1</appengine.app.version>
13-
<appengine.version>1.9.18</appengine.version>
13+
<appengine.target.version>1.8.4</appengine.target.version>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</properties>
1616

17-
<prerequisites>
18-
<maven>3.1.0</maven>
19-
</prerequisites>
20-
2117
<dependencies>
2218
<!-- Compile/runtime dependencies -->
2319
<dependency>
2420
<groupId>com.google.appengine</groupId>
2521
<artifactId>appengine-api-1.0-sdk</artifactId>
26-
<version>${appengine.version}</version>
22+
<version>${appengine.target.version}</version>
2723
</dependency>
2824
<dependency>
2925
<groupId>javax.servlet</groupId>
@@ -41,54 +37,45 @@
4137
<dependency>
4238
<groupId>junit</groupId>
4339
<artifactId>junit</artifactId>
44-
<version>4.12</version>
40+
<version>4.10</version>
4541
<scope>test</scope>
4642
</dependency>
4743
<dependency>
4844
<groupId>com.google.appengine</groupId>
4945
<artifactId>appengine-testing</artifactId>
50-
<version>${appengine.version}</version>
46+
<version>${appengine.target.version}</version>
5147
<scope>test</scope>
5248
</dependency>
5349
<dependency>
5450
<groupId>com.google.appengine</groupId>
5551
<artifactId>appengine-api-stubs</artifactId>
56-
<version>${appengine.version}</version>
52+
<version>${appengine.target.version}</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.appengine</groupId>
57+
<artifactId>appengine-tools-sdk</artifactId>
58+
<version>${appengine.target.version}</version>
5759
<scope>test</scope>
5860
</dependency>
5961
</dependencies>
6062

6163
<build>
62-
<!-- for hot reload of the web application-->
63-
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
6464
<plugins>
65-
<plugin>
66-
<groupId>org.codehaus.mojo</groupId>
67-
<artifactId>versions-maven-plugin</artifactId>
68-
<version>2.1</version>
69-
<executions>
70-
<execution>
71-
<phase>compile</phase>
72-
<goals>
73-
<goal>display-dependency-updates</goal>
74-
<goal>display-plugin-updates</goal>
75-
</goals>
76-
</execution>
77-
</executions>
78-
</plugin>
7965
<plugin>
8066
<groupId>org.apache.maven.plugins</groupId>
81-
<version>3.1</version>
67+
<version>2.5.1</version>
8268
<artifactId>maven-compiler-plugin</artifactId>
8369
<configuration>
8470
<source>1.7</source>
8571
<target>1.7</target>
8672
</configuration>
8773
</plugin>
74+
8875
<plugin>
8976
<groupId>org.apache.maven.plugins</groupId>
9077
<artifactId>maven-war-plugin</artifactId>
91-
<version>2.4</version>
78+
<version>2.3</version>
9279
<configuration>
9380
<archiveClasses>true</archiveClasses>
9481
<webResources>
@@ -105,36 +92,9 @@
10592
<plugin>
10693
<groupId>com.google.appengine</groupId>
10794
<artifactId>appengine-maven-plugin</artifactId>
108-
<version>${appengine.version}</version>
109-
<configuration>
110-
<enableJarClasses>false</enableJarClasses>
111-
<!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
112-
<!-- address>0.0.0.0</address>
113-
<port>8080</port -->
114-
<!-- Comment in the below snippet to enable local debugging with a remove debugger
115-
like those included with Eclipse or IntelliJ -->
116-
<!-- jvmFlags>
117-
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
118-
</jvmFlags -->
119-
</configuration>
120-
</plugin>
121-
<plugin>
122-
<groupId>org.apache.maven.plugins</groupId>
123-
<artifactId>maven-checkstyle-plugin</artifactId>
124-
<version>2.15</version>
125-
<executions>
126-
<execution>
127-
<id>checkstyle</id>
128-
<phase>validate</phase>
129-
<goals>
130-
<goal>check</goal>
131-
</goals>
132-
<configuration>
133-
<failOnViolation>true</failOnViolation>
134-
</configuration>
135-
</execution>
136-
</executions>
95+
<version>${appengine.target.version}</version>
13796
</plugin>
13897
</plugins>
13998
</build>
99+
140100
</project>

unittests/src/main/webapp/WEB-INF/appengine-web.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3-
<application>your-app-id</application>
3+
<application>appengine-local-testing-samples</application>
44
<version>${appengine.app.version}</version>
55
<threadsafe>true</threadsafe>
6-
6+
77
<system-properties>
88
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
99
</system-properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<queue-entries>
2+
<queue>
3+
<name>default</name>
4+
<rate>1/s</rate>
5+
</queue>
6+
<queue>
7+
<name>my-queue-name</name>
8+
<rate>3/s</rate>
9+
</queue>
10+
</queue-entries>
+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
3-
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app
3+
version="2.5"
4+
xmlns="http://java.sun.com/xml/ns/javaee"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
47
</web-app>

unittests/src/test/java/com/google/appengine/samples/unittest/AuthenticationTest.java renamed to unittests/src/test/java/com/google/appengine/samples/AuthenticationTest.java

+24-23
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.google.appengine.samples.unittest;
16+
package com.google.appengine.samples;
17+
18+
// [START AuthenticationTest]
1719

18-
// [START auth]
1920
import com.google.appengine.api.users.UserService;
2021
import com.google.appengine.api.users.UserServiceFactory;
2122
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
@@ -24,28 +25,28 @@
2425
import org.junit.Before;
2526
import org.junit.Test;
2627

27-
import static org.junit.Assert.*;
28+
import static org.junit.Assert.assertTrue;
2829

2930
public class AuthenticationTest {
3031

31-
private final LocalServiceTestHelper helper =
32-
new LocalServiceTestHelper(new LocalUserServiceTestConfig())
33-
.setEnvIsAdmin(true).setEnvIsLoggedIn(true);
34-
35-
@Before
36-
public void setUp() {
37-
helper.setUp();
38-
}
39-
40-
@After
41-
public void tearDown() {
42-
helper.tearDown();
43-
}
44-
45-
@Test
46-
public void testIsAdmin() {
47-
UserService userService = UserServiceFactory.getUserService();
48-
assertTrue(userService.isUserAdmin());
49-
}
32+
private final LocalServiceTestHelper helper =
33+
new LocalServiceTestHelper(new LocalUserServiceTestConfig())
34+
.setEnvIsAdmin(true).setEnvIsLoggedIn(true);
35+
36+
@Before
37+
public void setUp() {
38+
helper.setUp();
39+
}
40+
41+
@After
42+
public void tearDown() {
43+
helper.tearDown();
44+
}
45+
46+
@Test
47+
public void testIsAdmin() {
48+
UserService userService = UserServiceFactory.getUserService();
49+
assertTrue(userService.isUserAdmin());
50+
}
5051
}
51-
// [END auth]
52+
// [END AuthenticationTest]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.appengine.samples;
17+
18+
// [START DeferredTaskTest]
19+
20+
import com.google.appengine.api.taskqueue.DeferredTask;
21+
import com.google.appengine.api.taskqueue.QueueFactory;
22+
import com.google.appengine.api.taskqueue.TaskOptions;
23+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
24+
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
import java.util.concurrent.TimeUnit;
30+
31+
import static org.junit.Assert.assertTrue;
32+
33+
public class DeferredTaskTest {
34+
35+
// Unlike CountDownLatch, TaskCountDownlatch lets us reset.
36+
private final LocalTaskQueueTestConfig.TaskCountDownLatch latch =
37+
new LocalTaskQueueTestConfig.TaskCountDownLatch(1);
38+
39+
private final LocalServiceTestHelper helper =
40+
new LocalServiceTestHelper(new LocalTaskQueueTestConfig()
41+
.setDisableAutoTaskExecution(false)
42+
.setCallbackClass(LocalTaskQueueTestConfig.DeferredTaskCallback.class)
43+
.setTaskExecutionLatch(latch));
44+
45+
private static class MyTask implements DeferredTask {
46+
private static boolean taskRan = false;
47+
48+
@Override
49+
public void run() {
50+
taskRan = true;
51+
}
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
helper.setUp();
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
MyTask.taskRan = false;
62+
latch.reset();
63+
helper.tearDown();
64+
}
65+
66+
@Test
67+
public void testTaskGetsRun() throws InterruptedException {
68+
QueueFactory.getDefaultQueue().add(
69+
TaskOptions.Builder.withPayload(new MyTask()));
70+
assertTrue(latch.await(5, TimeUnit.SECONDS));
71+
assertTrue(MyTask.taskRan);
72+
}
73+
}
74+
// [END DeferredTaskTest]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.appengine.samples;
17+
18+
// [START LocalCustomPolicyHighRepDatastoreTest]
19+
20+
import com.google.appengine.api.datastore.*;
21+
import com.google.appengine.api.datastore.dev.HighRepJobPolicy;
22+
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
23+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
29+
import static org.junit.Assert.assertEquals;
30+
31+
public class LocalCustomPolicyHighRepDatastoreTest {
32+
private static final class CustomHighRepJobPolicy implements HighRepJobPolicy {
33+
static int newJobCounter = 0;
34+
static int existingJobCounter = 0;
35+
36+
@Override
37+
public boolean shouldApplyNewJob(Key entityGroup) {
38+
// every other new job fails to apply
39+
return newJobCounter++ % 2 == 0;
40+
}
41+
42+
@Override
43+
public boolean shouldRollForwardExistingJob(Key entityGroup) {
44+
// every other existing job fails to apply
45+
return existingJobCounter++ % 2 == 0;
46+
}
47+
}
48+
49+
private final LocalServiceTestHelper helper =
50+
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
51+
.setAlternateHighRepJobPolicyClass(CustomHighRepJobPolicy.class));
52+
53+
@Before
54+
public void setUp() {
55+
helper.setUp();
56+
}
57+
58+
@After
59+
public void tearDown() {
60+
helper.tearDown();
61+
}
62+
63+
@Test
64+
public void testEventuallyConsistentGlobalQueryResult() {
65+
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
66+
ds.put(new Entity("yam")); // applies
67+
ds.put(new Entity("yam")); // does not apply
68+
// first global query only sees the first Entity
69+
assertEquals(1, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
70+
// second global query sees both Entities because we "groom" (attempt to
71+
// apply unapplied jobs) after every query
72+
assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
73+
}
74+
}
75+
// [END LocalCustomPolicyHighRepDatastoreTest]

0 commit comments

Comments
 (0)