Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ServiceOptions.getDefaultProjectId() for project-id #560

Merged
merged 4 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pubsub/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.9.2-alpha</version>
<version>0.9.4-alpha</version>
</dependency>

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
// [START pubsub_quickstart]
// Imports the Google Cloud client library

import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.spi.v1.PublisherClient;
import com.google.pubsub.v1.TopicName;

public class QuickstartSample {

public static void main(String... args) throws Exception {

// Your Google Cloud Platform project ID
String projectId = ServiceOptions.getDefaultProjectId();

// Your topic ID
String topicId = "my-new-topic";

// Create a new topic
String projectId = args[0];
TopicName topic = TopicName.create(projectId, "my-new-topic");
TopicName topic = TopicName.create(projectId, topicId);
try (PublisherClient publisherClient = PublisherClient.create()) {
publisherClient.createTopic(topic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.spi.v1.PublisherClient;
import com.google.pubsub.v1.TopicName;

Expand All @@ -40,11 +41,11 @@ public class QuickstartSampleIT {

private ByteArrayOutputStream bout;
private PrintStream out;
private String projectId;

private void deleteTestTopic(String projectId) throws Exception {
private void deleteTestTopic() throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
publisherClient.deleteTopic(TopicName.create(projectId, "my-new-topic"));
publisherClient.deleteTopic(
TopicName.create(ServiceOptions.getDefaultProjectId(), "my-new-topic"));
} catch (IOException e) {
System.err.println("Error deleting topic " + e.getMessage());
}
Expand All @@ -55,10 +56,8 @@ public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
assertThat(projectId).isNotNull();
try {
deleteTestTopic(projectId);
deleteTestTopic();
} catch (Exception e) {
//empty catch block
}
Expand All @@ -67,12 +66,12 @@ public void setUp() {
@After
public void tearDown() throws Exception {
System.setOut(null);
deleteTestTopic(projectId);
deleteTestTopic();
}

@Test
public void testQuickstart() throws Exception {
QuickstartSample.main(projectId);
QuickstartSample.main();
String got = bout.toString();
assertThat(got).contains("my-new-topic created.");
}
Expand Down