1414 * limitations under the License.
1515 */
1616
17- package com .google .gcloud .storage ;
17+ package com .google .gcloud .storage . testing ;
1818
1919import com .google .common .collect .ImmutableMap ;
2020import com .google .gcloud .AuthCredentials ;
21- import com .google .gcloud .storage .RemoteGcsHelper .Option .KeyFromClasspath ;
21+ import com .google .gcloud .storage .BlobInfo ;
22+ import com .google .gcloud .RetryParams ;
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 ;
2227
2328import java .io .FileInputStream ;
2429import java .io .FileNotFoundException ;
@@ -53,7 +58,7 @@ private RemoteGcsHelper(StorageOptions options) {
5358 }
5459
5560 /**
56- * Returns a {@StorageOptions} object to be used for testing.
61+ * Returns a {@link StorageOptions} object to be used for testing.
5762 */
5863 public StorageOptions options () {
5964 return options ;
@@ -92,50 +97,37 @@ public static String generateBucketName() {
9297 }
9398
9499 /**
95- * Creates a {@code RemoteGcsHelper} object.
100+ * Creates a {@code RemoteGcsHelper} object for the given project id and JSON key path .
96101 *
102+ * @param projectId id of the project to be used for running the tests
103+ * @param keyPath path to the JSON key to be used for running the tests
97104 * @param options creation options
98105 * @return A {@code RemoteGcsHelper} object for the provided options.
99- * @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables
100- * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
101- * pointed by {@code GCLOUD_TESTS_KEY} does not exist
106+ * @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if the file pointed by
107+ * {@code keyPath} does not exist
102108 */
103- public static RemoteGcsHelper create (Option ... options ) throws GcsHelperException {
109+ public static RemoteGcsHelper create (String projectId , String keyPath , Option ... options )
110+ throws GcsHelperException {
104111 boolean keyFromClassPath = false ;
105112 Map <Class <? extends Option >, Option > optionsMap = Option .asImmutableMap (options );
106113 if (optionsMap .containsKey (KeyFromClasspath .class )) {
107114 keyFromClassPath =
108115 ((KeyFromClasspath ) optionsMap .get (KeyFromClasspath .class )).keyFromClasspath ();
109116 }
110- String projectId = System .getenv (PROJECT_ID_ENV_VAR );
111- String stringKeyPath = System .getenv (PRIVATE_KEY_ENV_VAR );
112- if (projectId == null ) {
113- String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set" ;
114- if (log .isLoggable (Level .WARNING )) {
115- log .log (Level .WARNING , message );
116- }
117- throw new GcsHelperException (message );
118- }
119- if (stringKeyPath == null ) {
120- String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set" ;
121- if (log .isLoggable (Level .WARNING )) {
122- log .log (Level .WARNING , message );
123- }
124- throw new GcsHelperException (message );
125- }
126117 try {
127118 InputStream keyFileStream ;
128119 if (keyFromClassPath ) {
129- keyFileStream = RemoteGcsHelper .class .getResourceAsStream (stringKeyPath );
120+ keyFileStream = RemoteGcsHelper .class .getResourceAsStream (keyPath );
130121 if (keyFileStream == null ) {
131- throw new FileNotFoundException (stringKeyPath + " not found in classpath" );
122+ throw new FileNotFoundException (keyPath + " not found in classpath" );
132123 }
133124 } else {
134- keyFileStream = new FileInputStream (stringKeyPath );
125+ keyFileStream = new FileInputStream (keyPath );
135126 }
136127 StorageOptions storageOptions = StorageOptions .builder ()
137128 .authCredentials (AuthCredentials .createForJson (keyFileStream ))
138129 .projectId (projectId )
130+ .retryParams (RetryParams .getDefaultInstance ())
139131 .build ();
140132 return new RemoteGcsHelper (storageOptions );
141133 } catch (FileNotFoundException ex ) {
@@ -151,6 +143,36 @@ public static RemoteGcsHelper create(Option... options) throws GcsHelperExceptio
151143 }
152144 }
153145
146+ /**
147+ * Creates a {@code RemoteGcsHelper} object. Project id and path to JSON key are read from two
148+ * environment variables: {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY}.
149+ *
150+ * @param options creation options
151+ * @return A {@code RemoteGcsHelper} object for the provided options.
152+ * @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if environment variables
153+ * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
154+ * pointed by {@code GCLOUD_TESTS_KEY} does not exist
155+ */
156+ public static RemoteGcsHelper create (Option ... options ) throws GcsHelperException {
157+ String projectId = System .getenv (PROJECT_ID_ENV_VAR );
158+ String keyPath = System .getenv (PRIVATE_KEY_ENV_VAR );
159+ if (projectId == null ) {
160+ String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set" ;
161+ if (log .isLoggable (Level .WARNING )) {
162+ log .log (Level .WARNING , message );
163+ }
164+ throw new GcsHelperException (message );
165+ }
166+ if (keyPath == null ) {
167+ String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set" ;
168+ if (log .isLoggable (Level .WARNING )) {
169+ log .log (Level .WARNING , message );
170+ }
171+ throw new GcsHelperException (message );
172+ }
173+ return create (projectId , keyPath , options );
174+ }
175+
154176 private static class DeleteBucketTask implements Callable <Boolean > {
155177
156178 private Storage storage ;
0 commit comments