Skip to content

Commit bc3d17b

Browse files
committed
---
yaml --- r: 907 b: refs/heads/master c: 9ea8d66 h: refs/heads/master i: 905: c30461d 903: a86c540 v: v3
1 parent 17e3868 commit bc3d17b

6 files changed

Lines changed: 879 additions & 90 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
refs/heads/master: 0de314146f83752c02582c403b5c93546871bbde
2+
refs/heads/master: 9ea8d6690dc3243b9c25b793b309e244948eb2d6
33
refs/heads/travis: 0fa997e2fc9c6b61b2d91e6d163655aae67d44b6
44
refs/heads/gh-pages: 5a10432ecc75f29812e33a8236c900379509fe99

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
public class DatastoreOptionsTest {
3535

3636
private static final String PROJECT_ID = "project_id";
37-
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
3837
private DatastoreRpcFactory datastoreRpcFactory;
3938
private DatastoreRpc datastoreRpc;
4039
private DatastoreOptions.Builder options;
@@ -47,7 +46,7 @@ public void setUp() throws IOException, InterruptedException {
4746
.normalizeDataset(false)
4847
.serviceRpcFactory(datastoreRpcFactory)
4948
.projectId(PROJECT_ID)
50-
.host("http://localhost:" + PORT);
49+
.host("http://localhost:" + LocalGcdHelper.PORT);
5150
EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreOptions.class)))
5251
.andReturn(datastoreRpc)
5352
.anyTimes();
@@ -61,7 +60,7 @@ public void testProjectId() throws Exception {
6160

6261
@Test
6362
public void testHost() throws Exception {
64-
assertEquals("http://localhost:" + PORT, options.build().host());
63+
assertEquals("http://localhost:" + LocalGcdHelper.PORT, options.build().host());
6564
}
6665

6766
@Test

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,22 @@ public class DatastoreTest {
103103
private Datastore datastore;
104104

105105
private static LocalGcdHelper gcdHelper;
106-
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
107106

108107
@Rule
109108
public ExpectedException thrown = ExpectedException.none();
110109

111110
@BeforeClass
112111
public static void beforeClass() throws IOException, InterruptedException {
113-
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
114-
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
112+
if (!LocalGcdHelper.isActive(PROJECT_ID)) {
113+
gcdHelper = LocalGcdHelper.start(PROJECT_ID);
115114
}
116115
}
117116

118117
@Before
119118
public void setUp() throws IOException, InterruptedException {
120119
options = DatastoreOptions.builder()
121120
.projectId(PROJECT_ID)
122-
.host("http://localhost:" + PORT)
121+
.host("http://localhost:" + LocalGcdHelper.PORT)
123122
.build();
124123
datastore = DatastoreFactory.instance().get(options);
125124
StructuredQuery<Key> query = Query.keyQueryBuilder().build();

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java

Lines changed: 39 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.math.BigInteger;
3636
import java.net.HttpURLConnection;
3737
import java.net.MalformedURLException;
38-
import java.net.ServerSocket;
3938
import java.net.URL;
4039
import java.nio.channels.Channels;
4140
import java.nio.channels.ReadableByteChannel;
@@ -50,10 +49,8 @@
5049
import java.security.NoSuchAlgorithmException;
5150
import java.util.ArrayList;
5251
import java.util.Arrays;
53-
import java.util.HashMap;
5452
import java.util.List;
5553
import java.util.Locale;
56-
import java.util.Map;
5754
import java.util.regex.Pattern;
5855
import java.util.zip.ZipEntry;
5956
import java.util.zip.ZipInputStream;
@@ -71,10 +68,9 @@ public class LocalGcdHelper {
7168
private final String projectId;
7269
private Path gcdPath;
7370
private ProcessStreamReader processReader;
74-
private final int port;
7571

7672
public static final String DEFAULT_PROJECT_ID = "projectid1";
77-
public static final int DEFAULT_PORT = 8080;
73+
public static final int PORT = 8080;
7874
private static final String GCD_VERSION = "v1beta2";
7975
private static final String GCD_BUILD = "rev1-2.1.2b";
8076
private static final String GCD_BASENAME = "gcd-" + GCD_VERSION + "-" + GCD_BUILD;
@@ -98,14 +94,6 @@ public class LocalGcdHelper {
9894
}
9995
}
10096

101-
public static int findAvailablePort(int defaultPort) {
102-
try (ServerSocket tempSocket = new ServerSocket(0)) {
103-
return tempSocket.getLocalPort();
104-
} catch (IOException e) {
105-
return defaultPort;
106-
}
107-
}
108-
10997
private static Path installedGcdPath() {
11098
String gcloudExecutableName;
11199
if (isWindows()) {
@@ -295,9 +283,8 @@ public static CommandWrapper create() {
295283
}
296284
}
297285

298-
public LocalGcdHelper(String projectId, int port) {
286+
public LocalGcdHelper(String projectId) {
299287
this.projectId = projectId;
300-
this.port = port;
301288
}
302289

303290
/**
@@ -310,7 +297,7 @@ public LocalGcdHelper(String projectId, int port) {
310297
*/
311298
public void start() throws IOException, InterruptedException {
312299
// send a quick request in case we have a hanging process from a previous run
313-
sendQuitRequest(port);
300+
sendQuitRequest();
314301
// Each run is associated with its own folder that is deleted once test completes.
315302
gcdPath = Files.createTempDirectory("gcd");
316303
File gcdFolder = gcdPath.toFile();
@@ -392,12 +379,13 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
392379
if (log.isLoggable(Level.FINE)) {
393380
log.log(Level.FINE, "Starting datastore emulator for the project: {0}", projectId);
394381
}
395-
Process startProcess = CommandWrapper.create()
396-
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
397-
"--port=" + Integer.toString(port), projectId)
398-
.directory(gcdPath)
399-
.redirectErrorStream()
400-
.start();
382+
Process startProcess =
383+
CommandWrapper.create()
384+
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
385+
projectId)
386+
.directory(gcdPath)
387+
.redirectErrorStream()
388+
.start();
401389
processReader = ProcessStreamReader.start(startProcess, "Dev App Server is now running");
402390
}
403391

@@ -431,9 +419,9 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx
431419
}
432420
}
433421

434-
public static void sendQuitRequest(int port) {
422+
public static void sendQuitRequest() {
435423
try {
436-
URL url = new URL("http", "localhost", port, "/_ah/admin/quit");
424+
URL url = new URL("http", "localhost", PORT, "/_ah/admin/quit");
437425
HttpURLConnection con = (HttpURLConnection) url.openConnection();
438426
con.setRequestMethod("POST");
439427
con.setDoOutput(true);
@@ -451,7 +439,7 @@ public static void sendQuitRequest(int port) {
451439
}
452440

453441
public void stop() throws IOException, InterruptedException {
454-
sendQuitRequest(port);
442+
sendQuitRequest();
455443
if (processReader != null) {
456444
processReader.terminate();
457445
}
@@ -480,70 +468,44 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
480468
});
481469
}
482470

483-
public static LocalGcdHelper start(String projectId, int port)
484-
throws IOException, InterruptedException {
485-
LocalGcdHelper helper = new LocalGcdHelper(projectId, port);
471+
public static LocalGcdHelper start(String projectId) throws IOException, InterruptedException {
472+
LocalGcdHelper helper = new LocalGcdHelper(projectId);
486473
helper.start();
487474
return helper;
488475
}
489476

490477
public static void main(String... args) throws IOException, InterruptedException {
491-
Map<String, String> parsedArgs = parseArgs(args);
492-
String action = parsedArgs.get("action");
493-
int port = (parsedArgs.get("port") == null) ? DEFAULT_PORT
494-
: Integer.parseInt(parsedArgs.get("port"));
495-
switch (action) {
496-
case "START":
497-
if (!isActive(DEFAULT_PROJECT_ID, port)) {
498-
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, port);
499-
try (FileWriter writer = new FileWriter(".local_gcd_helper")) {
500-
writer.write(
501-
helper.gcdPath.toAbsolutePath().toString() + System.lineSeparator());
502-
writer.write(Integer.toString(port));
478+
if (args.length == 1) {
479+
switch (args[0]) {
480+
case "START":
481+
if (!isActive(DEFAULT_PROJECT_ID)) {
482+
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID);
483+
try (FileWriter writer = new FileWriter(".local_gcd_helper")) {
484+
writer.write(helper.gcdPath.toAbsolutePath().toString());
485+
}
503486
}
504-
}
505-
return;
506-
case "STOP":
507-
File file = new File(".local_gcd_helper");
508-
String path = null;
509-
boolean fileExists = file.exists();
510-
if (fileExists) {
511-
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
512-
path = reader.readLine();
513-
port = Integer.parseInt(reader.readLine());
487+
return;
488+
case "STOP":
489+
sendQuitRequest();
490+
File file = new File(".local_gcd_helper");
491+
if (file.exists()) {
492+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
493+
String path = reader.readLine();
494+
deleteRecurse(Paths.get(path));
495+
}
496+
file.delete();
514497
}
515-
}
516-
sendQuitRequest(port);
517-
if (fileExists) {
518-
deleteRecurse(Paths.get(path));
519-
file.delete();
520-
}
521-
return;
522-
default:
523-
break;
524-
}
525-
}
526-
527-
private static Map<String, String> parseArgs(String[] args) {
528-
Map<String, String> parsedArgs = new HashMap<String, String>();
529-
for (String arg : args) {
530-
if (arg.startsWith("--port=")) {
531-
parsedArgs.put("port", arg.substring("--port=".length()));
532-
} else if (arg.equals("START") || arg.equals("STOP")) {
533-
parsedArgs.put("action", arg);
534-
} else {
535-
throw new RuntimeException("Only accepts START, STOP, and --port=<port #> as arguments");
498+
return;
499+
default:
500+
break;
536501
}
537502
}
538-
if (parsedArgs.get("action") == null) {
539-
throw new RuntimeException("EXPECTING START | STOP");
540-
}
541-
return parsedArgs;
503+
throw new RuntimeException("expecting only START | STOP");
542504
}
543505

544-
public static boolean isActive(String projectId, int port) {
506+
public static boolean isActive(String projectId) {
545507
try {
546-
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(port);
508+
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT);
547509
urlBuilder.append("/datastore/v1beta2/datasets/").append(projectId).append("/lookup");
548510
URL url = new URL(urlBuilder.toString());
549511
try (BufferedReader reader =

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,10 @@ public StorageObject copy(StorageObject source, Map<Option, ?> sourceOptions,
331331
.copy(source.getBucket(), source.getName(), target.getBucket(), target.getName(),
332332
target.getContentType() != null ? target : null)
333333
.setProjection(DEFAULT_PROJECTION)
334-
.setIfSourceMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(sourceOptions))
335-
.setIfSourceMetagenerationNotMatch(
336-
IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(sourceOptions))
337-
.setIfSourceGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(sourceOptions))
338-
.setIfSourceGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(sourceOptions))
334+
.setIfMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(sourceOptions))
335+
.setIfMetagenerationNotMatch(IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(sourceOptions))
336+
.setIfGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(sourceOptions))
337+
.setIfGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(sourceOptions))
339338
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(targetOptions))
340339
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(targetOptions))
341340
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(targetOptions))

0 commit comments

Comments
 (0)