Skip to content

Commit cf58018

Browse files
committed
Add tests to Storage quickstart.
Had to update the sample to accept a bucket name argument, since bucket names must be globally unique. (And we actually want to be able to run the sample for integration / system tests.)
1 parent 4236fdd commit cf58018

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

storage/cloud-client/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Getting Started with Cloud Storage and the Google Cloud Client libraries
2+
3+
[Google Cloud Storage][storage] is unified object storage for developers and enterprises, from live
4+
data serving to data analytics/ML to data archival.
5+
These sample Java applications demonstrate how to access the Cloud Storage API using
6+
the [Google Cloud Client Library for Java][google-cloud-java].
7+
8+
[storage]: https://cloud.google.com/storage/
9+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
10+
11+
## Quickstart
12+
13+
Install [Maven](http://maven.apache.org/).
14+
15+
Build your project with:
16+
17+
mvn clean package -DskipTests
18+
19+
You can then run a given `ClassName` via:
20+
21+
mvn exec:java -Dexec.mainClass=com.example.storage.ClassName \
22+
-DpropertyName=propertyValue \
23+
-Dexec.args="any arguments to the app"
24+
25+
### Creating a new bucket (using the quickstart sample)
26+
27+
mvn exec:java -Dexec.mainClass=com.example.storage.QuickstartSample \
28+
-Dexec.args="my-bucket-name"

storage/cloud-client/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,19 @@
3939
<artifactId>google-cloud-storage</artifactId>
4040
<version>0.4.0</version>
4141
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.30</version>
54+
<scope>test</scope>
55+
</dependency>
4256
</dependencies>
4357
</project>

storage/cloud-client/src/main/java/com/example/storage/QuickstartSample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void main(String... args) throws Exception {
2929
Storage storage = StorageOptions.defaultInstance().service();
3030

3131
// The name for the new bucket
32-
String bucketName = "my-new-bucket";
32+
String bucketName = args[0];
3333

3434
// Creates the new bucket
3535
Bucket bucket = storage.create(BucketInfo.of(bucketName));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2016, Google, Inc.
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+
package com.example.storage;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.storage.Storage;
22+
import com.google.cloud.storage.StorageOptions;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.PrintStream;
31+
import java.util.UUID;
32+
33+
/**
34+
* Tests for quickstart sample.
35+
*/
36+
@RunWith(JUnit4.class)
37+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
38+
public class QuickstartSampleIT {
39+
private String bucketName;
40+
private ByteArrayOutputStream bout;
41+
private PrintStream out;
42+
43+
private static final void deleteBucket(String bucketName) {
44+
Storage storage = StorageOptions.defaultInstance().service();
45+
storage.delete(bucketName);
46+
}
47+
48+
@Before
49+
public void setUp() {
50+
bucketName = "my-new-bucket-" + UUID.randomUUID().toString();
51+
52+
bout = new ByteArrayOutputStream();
53+
out = new PrintStream(bout);
54+
System.setOut(out);
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
System.setOut(null);
60+
deleteBucket(bucketName);
61+
}
62+
63+
@Test
64+
public void testQuickstart() throws Exception {
65+
QuickstartSample.main(bucketName);
66+
String got = bout.toString();
67+
assertThat(got).contains(String.format("Bucket %s created.", bucketName));
68+
}
69+
}
70+
// [END datastore_quickstart]

0 commit comments

Comments
 (0)