diff --git a/bigquery/pom.xml b/bigquery/pom.xml
index aa2e74c1add..8c2deafcd37 100644
--- a/bigquery/pom.xml
+++ b/bigquery/pom.xml
@@ -45,6 +45,10 @@
junit
junit
+
+ com.jcabi
+ jcabi-matchers
+
com.google.code.gson
gson
diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/ListDatasetsProjects.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/ListDatasetsProjects.java
new file mode 100644
index 00000000000..854e1b6e6ca
--- /dev/null
+++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/ListDatasetsProjects.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 2015 Google Inc.
+ *
+ * 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.cloud.bigquery.samples;
+
+import com.google.api.client.util.Data;
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.Bigquery.Datasets;
+import com.google.api.services.bigquery.model.DatasetList;
+import com.google.api.services.bigquery.model.GetQueryResultsResponse;
+import com.google.api.services.bigquery.model.ProjectList;
+import com.google.api.services.bigquery.model.QueryRequest;
+import com.google.api.services.bigquery.model.QueryResponse;
+import com.google.api.services.bigquery.model.TableCell;
+import com.google.api.services.bigquery.model.TableRow;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.lang.Thread;
+import java.util.List;
+
+/**
+ * Invokes the BigQuery basic APIs for the given project id specified.
+ *
+ * Samples used in this page:
+ *
+ * https://cloud.google.com/bigquery/bigquery-api-quickstart
+ */
+public class ListDatasetsProjects {
+ /**
+ * Run the sample.
+ */
+ public static void main(String[] args) throws IOException, InterruptedException {
+ if (args.length != 1) {
+ System.err.println("Usage: QuickStart ");
+ return;
+ }
+ String projectId = args[0];
+
+ Bigquery bigquery = BigqueryServiceFactory.getService();
+ String query = "SELECT TOP( title, 10) as title, COUNT(*) as revision_count "
+ + "FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;";
+
+ System.out.println();
+ System.out.println("----- Running the asynchronous query and printing it to stdout.");
+ runQueryRpcAndPrint(bigquery, projectId, query, System.out);
+
+ System.out.println();
+ System.out.println("----- Listing all the Datasets in the projectId");
+ listDatasets(bigquery, projectId);
+
+ System.out.println();
+ System.out.println("----- Listing all the Projects");
+ listProjects(bigquery);
+ }
+
+ /**
+ * Lists all Datasets in a project specified by the projectId.
+ *
+ * @param bigquery The BigQuery object.
+ * @param projectId The projectId from which lists the existing Datasets.
+ * @throws IOException if there's trouble with the network request.
+ */
+ // [START listDatasets]
+ public static void listDatasets(Bigquery bigquery, String projectId) throws IOException {
+ Datasets.List datasetRequest = bigquery.datasets().list(projectId);
+ DatasetList datasetList = datasetRequest.execute();
+
+ if (datasetList.getDatasets() != null) {
+ List datasets = datasetList.getDatasets();
+ System.out.println("Dataset list:");
+
+ for (DatasetList.Datasets dataset : datasets) {
+ System.out.format("%s\n", dataset.getDatasetReference().getDatasetId());
+ }
+ }
+ }
+ // [END listDatasets]
+
+ /**
+ * Lists all Projects.
+ *
+ * @param bigquery The BigQuery object.
+ * @throws IOException if there's trouble with the network request.
+ */
+ // [START listProjects]
+ public static void listProjects(Bigquery bigquery) throws IOException {
+ Bigquery.Projects.List projectListRequest = bigquery.projects().list();
+ ProjectList projectList = projectListRequest.execute();
+
+ if (projectList.getProjects() != null) {
+ List projects = projectList.getProjects();
+ System.out.println("Project list:");
+
+ for (ProjectList.Projects project : projects) {
+ System.out.format("%s\n", project.getFriendlyName());
+ }
+ }
+ }
+ // [END listProjects]
+
+ /**
+ * Runs a synchronous BigQuery query and displays the result.
+ *
+ * @param bigquery An authorized BigQuery client
+ * @param projectId The current project id
+ * @param query A String containing a BigQuery SQL statement
+ * @param out A PrintStream for output, normally System.out
+ */
+ static void runQueryRpcAndPrint(Bigquery bigquery, String projectId, String query,
+ PrintStream out) throws IOException, InterruptedException {
+ QueryRequest queryRequest = new QueryRequest().setQuery(query);
+ QueryResponse queryResponse = bigquery.jobs().query(projectId, queryRequest).execute();
+ if (queryResponse.getJobComplete()) {
+ printRows(queryResponse.getRows(), out);
+ if (null == queryResponse.getPageToken()) {
+ return;
+ }
+ }
+ // This loop polls until results are present, then loops over result pages.
+ String pageToken = null;
+ while (true) {
+ GetQueryResultsResponse queryResults = bigquery.jobs()
+ .getQueryResults(projectId, queryResponse.getJobReference().getJobId())
+ .setPageToken(pageToken).execute();
+ if (queryResults.getJobComplete()) {
+ printRows(queryResults.getRows(), out);
+ pageToken = queryResults.getPageToken();
+ if (null == pageToken) {
+ return;
+ }
+ }
+ Thread.sleep(500);
+ }
+ }
+
+ /**
+ * Print the given rows.
+ *
+ * @param rows the rows to print.
+ * @param out the place to print them.
+ */
+ private static void printRows(java.util.List rows, PrintStream out) {
+ if (rows != null) {
+ for (TableRow row : rows) {
+ for (TableCell cell : row.getF()) {
+ // Data.isNull() is the recommended way to check for the 'null object' in TableCell.
+ out.printf("%s, ", Data.isNull(cell.getV()) ? "null" : cell.getV().toString());
+ }
+ out.println();
+ }
+ }
+ }
+}
diff --git a/bigquery/src/test/java/com/google/cloud/bigquery/samples/test/ListDatasetsProjectsTest.java b/bigquery/src/test/java/com/google/cloud/bigquery/samples/test/ListDatasetsProjectsTest.java
new file mode 100644
index 00000000000..354f7913579
--- /dev/null
+++ b/bigquery/src/test/java/com/google/cloud/bigquery/samples/test/ListDatasetsProjectsTest.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2015 Google Inc.
+ *
+ * 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.cloud.bigquery.samples.test;
+
+import static com.jcabi.matchers.RegexMatchers.*;
+import static org.junit.Assert.*;
+import static org.junit.matchers.JUnitMatchers.*;
+
+import com.google.cloud.bigquery.samples.ListDatasetsProjects;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.PrintStream;
+import java.lang.Exception;
+
+/** Unit tests for {@link ListDatasetsProjects}. */
+public class ListDatasetsProjectsTest extends BigquerySampleTest {
+
+ public ListDatasetsProjectsTest() throws FileNotFoundException {
+ super();
+ }
+
+ private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
+ private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
+ private static final PrintStream REAL_OUT = System.out;
+ private static final PrintStream REAL_ERR = System.err;
+
+ @Before
+ public void setUp() {
+ System.setOut(new PrintStream(stdout));
+ System.setErr(new PrintStream(stderr));
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(REAL_OUT);
+ System.setErr(REAL_ERR);
+ }
+
+ @Test
+ public void testUsage() throws Exception {
+ ListDatasetsProjects.main(new String[] {});
+ assertEquals("Usage: QuickStart \n", stderr.toString());
+ }
+
+ @Test
+ public void testMain() throws Exception {
+ ListDatasetsProjects.main(new String[] { CONSTANTS.getProjectId() });
+ String out = stdout.toString();
+ assertThat(out, containsString("Running the asynchronous query"));
+ assertThat(out, containsPattern("George W. Bush, [0-9]+"));
+ assertThat(out, containsPattern("Wikipedia, [0-9]+"));
+
+ assertThat(out, containsString("Listing all the Datasets"));
+ assertThat(out, containsString("test_dataset"));
+
+ assertThat(out, containsString("Listing all the Projects"));
+ assertThat(out, containsString("Project list:"));
+ assertThat(out, containsPattern("Bigquery Samples|cloud-samples-tests"));
+ }
+}
diff --git a/monitoring/pom.xml b/monitoring/pom.xml
index 06b9eb21dcb..0e77711d9b5 100644
--- a/monitoring/pom.xml
+++ b/monitoring/pom.xml
@@ -48,7 +48,6 @@
com.jcabi
jcabi-matchers
- 1.3
diff --git a/monitoring/src/test/java/CloudMonitoringAuthSampleTest.java b/monitoring/src/test/java/CloudMonitoringAuthSampleTest.java
index 4e593fd4aa9..ad5cd9e5f50 100644
--- a/monitoring/src/test/java/CloudMonitoringAuthSampleTest.java
+++ b/monitoring/src/test/java/CloudMonitoringAuthSampleTest.java
@@ -33,6 +33,8 @@ public class CloudMonitoringAuthSampleTest {
new ByteArrayOutputStream();
private final ByteArrayOutputStream stderr =
new ByteArrayOutputStream();
+ private static final PrintStream REAL_OUT = System.out;
+ private static final PrintStream REAL_ERR = System.err;
@Before
public void setUp() {
@@ -42,8 +44,8 @@ public void setUp() {
@After
public void tearDown() {
- System.setOut(null);
- System.setErr(null);
+ System.setOut(this.REAL_OUT);
+ System.setErr(this.REAL_ERR);
}
@Test
diff --git a/pom.xml b/pom.xml
index 3d4efbe1b4a..2982562f6c2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -119,6 +119,11 @@
2.0.28-beta
test
+
+ com.jcabi
+ jcabi-matchers
+ 1.3
+
com.google.appengine
appengine-testing