Skip to content

Commit 8458718

Browse files
author
Ajay Kannan
committed
Add unit tests for public methods in LocalGcdHelper
1 parent 0dd3d97 commit 8458718

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx
524524
}
525525
}
526526

527-
public static void sendQuitRequest(int port) {
527+
public static String sendQuitRequest(int port) {
528+
StringBuilder result = new StringBuilder();
528529
try {
529530
URL url = new URL("http", "localhost", port, "/_ah/admin/quit");
530531
HttpURLConnection con = (HttpURLConnection) url.openConnection();
@@ -535,12 +536,14 @@ public static void sendQuitRequest(int port) {
535536
out.write("".getBytes());
536537
out.flush();
537538
InputStream in = con.getInputStream();
538-
while (in.read() != -1) {
539-
// consume input
539+
int currByte = 0;
540+
while ((currByte = in.read()) != -1) {
541+
result.append(((char) currByte));
540542
}
541543
} catch (IOException ignore) {
542544
// ignore
543545
}
546+
return result.toString();
544547
}
545548

546549
public void stop() throws IOException, InterruptedException {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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.google.gcloud.datastore;
18+
19+
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertTrue;
21+
import static org.junit.Assert.fail;
22+
23+
import com.google.gcloud.datastore.testing.LocalGcdHelper;
24+
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
import java.io.IOException;
32+
import java.net.ServerSocket;
33+
34+
@RunWith(JUnit4.class)
35+
public class LocalGcdHelperTest {
36+
37+
private static final String PROJECT_ID = LocalGcdHelper.DEFAULT_PROJECT_ID;
38+
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
39+
40+
@Rule
41+
public ExpectedException thrown = ExpectedException.none();
42+
43+
@Test
44+
public void testFindAvailablePort() {
45+
int chosenPort = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
46+
try (ServerSocket tempSocket = new ServerSocket(chosenPort)) {
47+
// success
48+
} catch (IOException e) {
49+
if (chosenPort != LocalGcdHelper.DEFAULT_PORT) {
50+
fail("Chosen port not free, even though LocalGcdHelper claimed it was.");
51+
}
52+
}
53+
}
54+
55+
@Test
56+
public void testSendQuitRequest() throws IOException, InterruptedException {
57+
LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
58+
assertTrue(LocalGcdHelper.sendQuitRequest(PORT).startsWith("Shutting down local server"));
59+
gcdHelper.stop();
60+
assertTrue(LocalGcdHelper.sendQuitRequest(PORT).isEmpty()); // shouldn't error
61+
}
62+
63+
@Test
64+
public void testStartStop() throws IOException, InterruptedException {
65+
LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
66+
assertFalse(LocalGcdHelper.isActive("wrong-project-id", PORT));
67+
assertTrue(LocalGcdHelper.isActive(PROJECT_ID, PORT));
68+
gcdHelper.stop();
69+
assertFalse(LocalGcdHelper.isActive(PROJECT_ID, PORT));
70+
}
71+
}

0 commit comments

Comments
 (0)