Skip to content

Commit 0ebc649

Browse files
committed
lint, add test
1 parent 227dd88 commit 0ebc649

2 files changed

Lines changed: 85 additions & 31 deletions

File tree

bigtable/hbase/snippets/src/main/java/com/example/cloud/bigtable/helloworld/HelloWorld.java renamed to bigtable/hbase/snippets/src/main/java/com/example/bigtable/HelloWorld.java

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
/**
22
* Copyright 2016 Google Inc. All Rights Reserved.
33
*
4-
* Licensed to the Apache Software Foundation (ASF) under one
5-
* or more contributor license agreements. See the NOTICE file
6-
* distributed with this work for additional information
7-
* regarding copyright ownership. The ASF licenses this file
8-
* to you under the Apache License, Version 2.0 (the
9-
* "License"); you may not use this file except in compliance
10-
* with the License. You may obtain a copy of the License at
4+
* <p>Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5+
* agreements. See the NOTICE file distributed with this work for additional information regarding
6+
* copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance with the License. You may obtain a
8+
* copy of the License at
119
*
12-
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>http://www.apache.org/licenses/LICENSE-2.0
1311
*
14-
* Unless required by applicable law or agreed to in writing, software
15-
* distributed under the License is distributed on an "AS IS" BASIS,
16-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17-
* See the License for the specific language governing permissions and
12+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
13+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing permissions and
1815
* limitations under the License.
1916
*/
20-
package com.example.cloud.bigtable.helloworld;
17+
package com.example.bigtable;
2118

2219
// [START bigtable_hw_imports_hbase]
2320
import com.google.cloud.bigtable.hbase.BigtableConfiguration;
24-
21+
import java.io.IOException;
22+
// [END bigtable_hw_imports_hbase]
2523
import org.apache.hadoop.hbase.HColumnDescriptor;
2624
import org.apache.hadoop.hbase.HTableDescriptor;
2725
import org.apache.hadoop.hbase.TableName;
@@ -35,9 +33,6 @@
3533
import org.apache.hadoop.hbase.client.Table;
3634
import org.apache.hadoop.hbase.util.Bytes;
3735

38-
import java.io.IOException;
39-
// [END bigtable_hw_imports_hbase]
40-
4136
/**
4237
* A minimal application that connects to Cloud Bigtable using the native HBase API and performs
4338
* some basic operations.
@@ -55,7 +50,7 @@ public class HelloWorld {
5550
};
5651

5752
/** Connects to Cloud Bigtable, runs some basic operations and prints the results. */
58-
private static void doHelloWorld(String projectId, String instanceId) {
53+
protected static void doHelloWorld(String projectId, String instanceId) {
5954

6055
// [START bigtable_hw_connect_hbase]
6156
// Create the Bigtable connection, use try-with-resources to make sure it gets closed
@@ -71,7 +66,7 @@ private static void doHelloWorld(String projectId, String instanceId) {
7166
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
7267
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
7368

74-
print("Create table " + descriptor.getNameAsString());
69+
System.out.println("HelloWorld: Create table " + descriptor.getNameAsString());
7570
admin.createTable(descriptor);
7671
// [END bigtable_hw_create_table_hbase]
7772

@@ -80,7 +75,7 @@ private static void doHelloWorld(String projectId, String instanceId) {
8075
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
8176

8277
// Write some rows to the table
83-
print("Write some greetings to the table");
78+
System.out.println("HelloWorld: Write some greetings to the table");
8479
for (int i = 0; i < GREETINGS.length; i++) {
8580
// Each row has a unique row key.
8681
//
@@ -115,7 +110,7 @@ private static void doHelloWorld(String projectId, String instanceId) {
115110
// Now scan across all rows.
116111
Scan scan = new Scan();
117112

118-
print("Scan for all greetings:");
113+
System.out.println("HelloWorld: Scan for all greetings:");
119114
ResultScanner scanner = table.getScanner(scan);
120115
for (Result row : scanner) {
121116
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
@@ -125,13 +120,13 @@ private static void doHelloWorld(String projectId, String instanceId) {
125120

126121
// [START bigtable_hw_delete_table_hbase]
127122
// Clean up by disabling and then deleting the table
128-
print("Delete the table");
123+
System.out.println("HelloWorld: Delete the table");
129124
admin.disableTable(table.getName());
130125
admin.deleteTable(table.getName());
131126
// [END bigtable_hw_delete_table_hbase]
132127
} catch (IOException e) {
133128
if (admin.tableExists(TableName.valueOf(TABLE_NAME))) {
134-
print("Cleaning up table");
129+
System.out.println("HelloWorld: Cleaning up table");
135130
admin.disableTable(TableName.valueOf(TABLE_NAME));
136131
admin.deleteTable(TableName.valueOf(TABLE_NAME));
137132
}
@@ -140,15 +135,7 @@ private static void doHelloWorld(String projectId, String instanceId) {
140135
} catch (IOException e) {
141136
System.err.println("Exception while running HelloWorld: " + e.getMessage());
142137
e.printStackTrace();
143-
144-
System.exit(1);
145138
}
146-
147-
System.exit(0);
148-
}
149-
150-
private static void print(String msg) {
151-
System.out.println("HelloWorld: " + msg);
152139
}
153140

154141
public static void main(String[] args) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2022 Google LLC
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.example.bigtable;
17+
18+
import static org.junit.Assert.assertNotNull;
19+
20+
import com.google.common.truth.Truth;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
28+
public class HelloWorldTest {
29+
private static String projectId;
30+
private static String instanceId;
31+
private ByteArrayOutputStream bout;
32+
33+
@BeforeClass
34+
public static void beforeClass() throws IOException {
35+
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
36+
instanceId = requireEnv("BIGTABLE_TESTING_INSTANCE");
37+
}
38+
39+
@Before
40+
public void setupStream() {
41+
bout = new ByteArrayOutputStream();
42+
System.setOut(new PrintStream(bout));
43+
}
44+
45+
@Test
46+
public void helloWorld() {
47+
HelloWorld.doHelloWorld(projectId, instanceId);
48+
49+
Truth.assertThat(bout.toString()).contains("HelloWorld: Create table Hello-Bigtable");
50+
Truth.assertThat(bout.toString()).contains("HelloWorld: Write some greetings to the table");
51+
Truth.assertThat(bout.toString()).contains("Get a single greeting by row key");
52+
Truth.assertThat(bout.toString()).contains("greeting0 = Hello World!");
53+
Truth.assertThat(bout.toString()).contains("HelloWorld: Scan for all greetings:");
54+
Truth.assertThat(bout.toString()).contains("Hello World!");
55+
Truth.assertThat(bout.toString()).contains("Hello Cloud Bigtable!");
56+
Truth.assertThat(bout.toString()).contains("Hello HBase!");
57+
Truth.assertThat(bout.toString()).contains("HelloWorld: Delete the table");
58+
}
59+
60+
private static String requireEnv(String varName) {
61+
String value = System.getenv(varName);
62+
assertNotNull(
63+
String.format("Environment variable '%s' is required to perform these tests.", varName),
64+
value);
65+
return value;
66+
}
67+
}

0 commit comments

Comments
 (0)