Skip to content

Commit d404936

Browse files
committed
---
yaml --- r: 961 b: refs/heads/master c: e76c85c h: refs/heads/master i: 959: 5455f83 v: v3
1 parent b0c89ff commit d404936

12 files changed

Lines changed: 182 additions & 7 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
refs/heads/master: e3f5275d4c3449d2a2778d2a1fac9c4048384ffe
2+
refs/heads/master: e76c85c905a01d59ac7df282e6b2c6e23c9a99eb
33
refs/heads/travis: 0fa997e2fc9c6b61b2d91e6d163655aae67d44b6
44
refs/heads/gh-pages: 5a10432ecc75f29812e33a8236c900379509fe99

trunk/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ if (!blob.exists()) {
127127
}
128128
```
129129

130+
Testing
131+
-------
132+
133+
This library provides tools to help write tests for code that uses gcloud-java services.
134+
135+
See [TESTING] to read more about using our testing helpers.
136+
130137
Contributing
131138
------------
132139

@@ -159,6 +166,7 @@ Apache 2.0 - See [LICENSE] for more information.
159166
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
160167
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md
161168
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
169+
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md
162170
[cloud-platform]: https://cloud.google.com/
163171
[cloud-datastore]: https://cloud.google.com/datastore/docs
164172
[cloud-datastore-docs]: https://cloud.google.com/datastore/docs

trunk/TESTING.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## gcloud-java tools for testing
2+
3+
This library provides tools to help write tests for code that uses gcloud-java services.
4+
5+
### Testing code that uses Datastore
6+
7+
#### On your machine
8+
9+
You can test against a temporary local datastore by following these steps:
10+
11+
1. Start the local datastore emulator using `LocalGcdHelper`. This can be done in two ways:
12+
- Run `LocalGcdHelper.java`'s `main` method with arguments `START` and (optionally) `--port=<port number>`. This will create a temporary folder on your computer and bind `localhost:<port number>` for communication with the local datastore. The port number is an optional argument. If no port number is specified, port 8080 will be used.
13+
- Call `LocalGcdHelper.start(<project ID>, <port number>)` before running your tests. Save the `LocalGcdHelper` object returned so that you can stop the emulator later.
14+
15+
2. In your program, create and use a datastore whose host is set host to `localhost:<port number>`. For example,
16+
```java
17+
DatastoreOptions options = DatastoreOptions.builder()
18+
.projectId(PROJECT_ID)
19+
.host("http://localhost:8080")
20+
.build();
21+
Datastore localDatastore = DatastoreFactory.instance().get(options);
22+
```
23+
3. Run your tests.
24+
25+
4. Stop the local datastore emulator.
26+
- If you ran `LocalGcdHelper.java`'s `main` function to start the emulator, run `LocalGcdHelper.java`'s `main` method with arguments `STOP` and (optionally) `--port=<port number>`. If the port is not supplied, the program will attempt to close the last port started.
27+
- If you ran `LocalGcdHelper.start()` to start the emulator, call the `stop()` method on the `LocalGcdHelper` object returned by `LocalGcdHelper.start()`.
28+
29+
#### On a remote machine
30+
31+
You can test against a remote datastore emulator as well. To do this, set the `DatastoreOptions` project endpoint to the hostname of the remote machine, like the example below.
32+
33+
```java
34+
DatastoreOptions options = DatastoreOptions.builder()
35+
.projectId(PROJECT_ID)
36+
.host("http://<hostname of machine>:<port>")
37+
.build();
38+
Datastore localDatastore = DatastoreFactory.instance().get(options);
39+
```
40+
41+
Note that the remote datastore must be running before your tests are run.
42+
43+
### Testing code that uses Storage
44+
45+
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteGcsHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
46+
47+
1. Create a test Google Cloud project.
48+
49+
2. Download a JSON service account credentials file from the Google Developer's Console. See more about this on the [Google Cloud Platform Storage Authentication page][cloud-platform-storage-authentication].
50+
51+
3. Create a `RemoteGcsHelper` object using your project ID and JSON key.
52+
Here is an example that uses the `RemoteGcsHelper` to create a bucket.
53+
```java
54+
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create(PROJECT_ID, "/path/to/my/JSON/key.json");
55+
Storage storage = StorageFactory.instance().get(gcsHelper.options());
56+
String bucket = RemoteGcsHelper.generateBucketName();
57+
storage.create(BucketInfo.of(bucket));
58+
```
59+
60+
4. Run your tests.
61+
62+
5. Clean up the test project by using `forceDelete` to clear any buckets used.
63+
Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.
64+
```java
65+
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
66+
```
67+
68+
69+
[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts

trunk/gcloud-java-datastore/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ if (entity == null) {
6969
}
7070
```
7171

72+
Testing
73+
-------
74+
75+
This library has tools to help write tests for code that uses the Datastore.
76+
77+
See [TESTING] to read more about testing.
78+
7279
Contributing
7380
------------
7481

@@ -98,6 +105,7 @@ Apache 2.0 - See [LICENSE] for more information.
98105

99106
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
100107
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
108+
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-datastore
101109
[cloud-platform]: https://cloud.google.com/
102110
[cloud-datastore]: https://cloud.google.com/datastore/docs
103111
[cloud-datastore-docs]: https://cloud.google.com/datastore/docs

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java renamed to trunk/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.gcloud.datastore;
17+
package com.google.gcloud.datastore.testing;
1818

1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
17+
/**
18+
* A testing helper for Google Cloud Datastore.
19+
*
20+
* <p>A simple usage example:
21+
* <p>Before the test:
22+
* <pre> {@code
23+
* LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT_NUMBER);
24+
* DatastoreOptions options = DatastoreOptions.builder()
25+
* .projectId(PROJECT_ID)
26+
* .host("localhost:8080")
27+
* .build();
28+
* Datastore localDatastore = DatastoreFactory.instance().get(options);
29+
* } </pre>
30+
*
31+
* <p>After the test:
32+
* <pre> {@code
33+
* gcdHelper.stop();
34+
* } </pre>
35+
*
36+
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-datastore">
37+
* gcloud-java tools for testing</a>
38+
*/
39+
package com.google.gcloud.datastore.testing;

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertSame;
2323
import static org.junit.Assert.assertTrue;
2424

25+
import com.google.gcloud.datastore.testing.LocalGcdHelper;
2526
import com.google.gcloud.spi.DatastoreRpc;
2627
import com.google.gcloud.spi.DatastoreRpcFactory;
2728

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
3434
import com.google.gcloud.datastore.StructuredQuery.Projection;
3535
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
36+
import com.google.gcloud.datastore.testing.LocalGcdHelper;
3637
import com.google.gcloud.spi.DatastoreRpc;
3738
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason;
3839
import com.google.gcloud.spi.DatastoreRpcFactory;

trunk/gcloud-java-storage/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ Add this to your pom.xml file
2626
</dependency>
2727
```
2828

29+
Testing
30+
-------
31+
32+
This library has tools to help make tests for code using Cloud Storage.
33+
34+
See [TESTING] to read more about testing.
2935

3036
Contributing
3137
------------
@@ -56,6 +62,7 @@ Apache 2.0 - See [LICENSE] for more information.
5662

5763
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
5864
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
65+
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-storage
5966
[cloud-platform]: https://cloud.google.com/
6067

6168
[cloud-storage]: https://cloud.google.com/storage/

trunk/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java renamed to trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/testing/RemoteGcsHelper.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.gcloud.storage;
17+
package com.google.gcloud.storage.testing;
1818

1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.gcloud.AuthCredentials;
21+
import com.google.gcloud.storage.BlobInfo;
2122
import com.google.gcloud.RetryParams;
22-
import com.google.gcloud.storage.RemoteGcsHelper.Option.KeyFromClasspath;
23+
import com.google.gcloud.storage.Storage;
24+
import com.google.gcloud.storage.StorageException;
25+
import com.google.gcloud.storage.StorageOptions;
26+
import com.google.gcloud.storage.testing.RemoteGcsHelper.Option.KeyFromClasspath;
2327

2428
import java.io.FileInputStream;
2529
import java.io.FileNotFoundException;
@@ -54,7 +58,7 @@ private RemoteGcsHelper(StorageOptions options) {
5458
}
5559

5660
/**
57-
* Returns a {@StorageOptions} object to be used for testing.
61+
* Returns a {@link StorageOptions} object to be used for testing.
5862
*/
5963
public StorageOptions options() {
6064
return options;
@@ -99,7 +103,7 @@ public static String generateBucketName() {
99103
* @param keyPath path to the JSON key to be used for running the tests
100104
* @param options creation options
101105
* @return A {@code RemoteGcsHelper} object for the provided options.
102-
* @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if the file pointed by
106+
* @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if the file pointed by
103107
* {@code keyPath} does not exist
104108
*/
105109
public static RemoteGcsHelper create(String projectId, String keyPath, Option... options)
@@ -145,7 +149,7 @@ public static RemoteGcsHelper create(String projectId, String keyPath, Option...
145149
*
146150
* @param options creation options
147151
* @return A {@code RemoteGcsHelper} object for the provided options.
148-
* @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables
152+
* @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if environment variables
149153
* {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
150154
* pointed by {@code GCLOUD_TESTS_KEY} does not exist
151155
*/

0 commit comments

Comments
 (0)