From 215ad2ed76f2a7dc5b193615d549abdc104f9e9a Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 15 Mar 2016 16:03:31 -0700 Subject: [PATCH 1/2] Checkstyle fixes for Cloud Storage transfer sample. --- storage/storage-transfer/pom.xml | 13 ------ .../samples/test/AwsRequesterTest.java | 23 +++++------ .../samples/test/NearlineRequesterTest.java | 38 ++++++++--------- .../samples/test/RequestCheckerTest.java | 13 +++--- .../test/TransferClientCreatorTest.java | 41 +++++++++---------- .../samples/test/TransferJobUtilsTest.java | 41 +++++++++---------- 6 files changed, 76 insertions(+), 93 deletions(-) diff --git a/storage/storage-transfer/pom.xml b/storage/storage-transfer/pom.xml index a93abe75607..8b2f46ee990 100644 --- a/storage/storage-transfer/pom.xml +++ b/storage/storage-transfer/pom.xml @@ -59,17 +59,4 @@ test - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - - - - diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java index 203fde4514b..cac2eaa8918 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +16,7 @@ package com.google.cloud.storage.storagetransfer.samples.test; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; import com.google.api.services.storagetransfer.Storagetransfer; @@ -30,8 +31,6 @@ import com.google.cloud.storage.storagetransfer.samples.TransferClientCreator; import com.google.cloud.storage.storagetransfer.samples.TransferJobUtils; -import junit.framework.TestCase; - import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; @@ -42,7 +41,7 @@ @RunWith(PowerMockRunner.class) @PrepareForTest({ TransferJobUtils.class, TransferClientCreator.class }) -public class AwsRequesterTest extends TestCase { +public class AwsRequesterTest { /** * Tests whether AwsRequester executes a request to create a TransferJob. @@ -52,13 +51,13 @@ public void testTest() throws Exception { Date date = TransferJobUtils.createDate("2000-1-1"); TimeOfDay time = TransferJobUtils.createTimeOfDay("1:1:1"); TransferJob dummyJob = TransferJob.class - .newInstance() - .setDescription("DUMMY DESCRIPTION") - .setProjectId("DUMMY_PROJECT_ID") - .setTransferSpec(TransferSpec.class.newInstance()) - .setSchedule( - Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) - .setStartTimeOfDay(time)).setStatus("ENABLED"); + .newInstance() + .setDescription("DUMMY DESCRIPTION") + .setProjectId("DUMMY_PROJECT_ID") + .setTransferSpec(TransferSpec.class.newInstance()) + .setSchedule( + Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) + .setStartTimeOfDay(time)).setStatus("ENABLED"); PowerMockito.mockStatic(TransferClientCreator.class); PowerMockito.mockStatic(TransferJobUtils.class); @@ -75,4 +74,4 @@ public void testTest() throws Exception { assertEquals(returnedJob, dummyJob); } -} \ No newline at end of file +} diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java index 0420cde19a4..4816ac1cd3c 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,16 +16,8 @@ package com.google.cloud.storage.storagetransfer.samples.test; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; -import junit.framework.TestCase; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import com.google.api.services.storagetransfer.Storagetransfer; import com.google.api.services.storagetransfer.Storagetransfer.TransferJobs; @@ -39,9 +31,17 @@ import com.google.cloud.storage.storagetransfer.samples.TransferClientCreator; import com.google.cloud.storage.storagetransfer.samples.TransferJobUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + @RunWith(PowerMockRunner.class) @PrepareForTest({ TransferJobUtils.class, TransferClientCreator.class }) -public class NearlineRequesterTest extends TestCase { +public class NearlineRequesterTest { /** * Tests whether NearlineRequester executes a request to create a TransferJob. @@ -51,13 +51,13 @@ public void testTest() throws Exception { Date date = TransferJobUtils.createDate("2000-1-1"); TimeOfDay time = TransferJobUtils.createTimeOfDay("1:1:1"); TransferJob dummyJob = TransferJob.class - .newInstance() - .setDescription("DUMMY DESCRIPTION") - .setProjectId("DUMMY_PROJECT_ID") - .setTransferSpec(TransferSpec.class.newInstance()) - .setSchedule( - Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) - .setStartTimeOfDay(time)).setStatus("ENABLED"); + .newInstance() + .setDescription("DUMMY DESCRIPTION") + .setProjectId("DUMMY_PROJECT_ID") + .setTransferSpec(TransferSpec.class.newInstance()) + .setSchedule( + Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) + .setStartTimeOfDay(time)).setStatus("ENABLED"); PowerMockito.mockStatic(TransferClientCreator.class); PowerMockito.mockStatic(TransferJobUtils.class); @@ -74,4 +74,4 @@ public void testTest() throws Exception { assertEquals(returnedJob, dummyJob); } -} \ No newline at end of file +} diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java index 61382119765..d82eb2535f8 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,18 +18,17 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import junit.framework.TestCase; - -import org.junit.Test; -import org.mockito.Matchers; -import org.mockito.Mockito; import com.google.api.services.storagetransfer.Storagetransfer; import com.google.api.services.storagetransfer.Storagetransfer.TransferOperations; import com.google.api.services.storagetransfer.Storagetransfer.TransferOperations.List; import com.google.cloud.storage.storagetransfer.samples.RequestChecker; -public class RequestCheckerTest extends TestCase { +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mockito; + +public class RequestCheckerTest { private Storagetransfer mockClient = Mockito.mock(Storagetransfer.class); private List mockList = Mockito.mock(List.class); private TransferOperations mockOps = Mockito.mock(TransferOperations.class); diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferClientCreatorTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferClientCreatorTest.java index 042085b06b3..d36d342082a 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferClientCreatorTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferClientCreatorTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +18,14 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import junit.framework.TestCase; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.util.Utils; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.services.storagetransfer.Storagetransfer.Builder; +import com.google.cloud.storage.storagetransfer.samples.RetryHttpInitializerWrapper; +import com.google.cloud.storage.storagetransfer.samples.TransferClientCreator; import org.junit.Before; import org.junit.Test; @@ -29,32 +36,24 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.googleapis.util.Utils; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.services.storagetransfer.Storagetransfer.Builder; -import com.google.cloud.storage.storagetransfer.samples.RetryHttpInitializerWrapper; -import com.google.cloud.storage.storagetransfer.samples.TransferClientCreator; - @RunWith(PowerMockRunner.class) @PrepareForTest({ TransferClientCreator.class, Builder.class }) -public class TransferClientCreatorTest extends TestCase { +public class TransferClientCreatorTest { private Builder mockBuilder = PowerMockito.mock(Builder.class); private GoogleCredential mockCredential = Mockito.mock(GoogleCredential.class); - private RetryHttpInitializerWrapper mockInitializer = Mockito - .mock(RetryHttpInitializerWrapper.class); + private RetryHttpInitializerWrapper mockInitializer = + Mockito.mock(RetryHttpInitializerWrapper.class); private HttpTransport httpTransport = Utils.getDefaultTransport(); private JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); @Before public void setUp() throws Exception { PowerMockito.whenNew(RetryHttpInitializerWrapper.class).withArguments(mockCredential) - .thenReturn(mockInitializer); + .thenReturn(mockInitializer); PowerMockito.mockStatic(Builder.class); PowerMockito.whenNew(Builder.class).withArguments(httpTransport, jsonFactory, mockInitializer) - .thenReturn(mockBuilder); + .thenReturn(mockBuilder); when(mockBuilder.setApplicationName(Matchers.anyString())).thenReturn(mockBuilder); PowerMockito.mockStatic(GoogleCredential.class); } @@ -67,8 +66,8 @@ public void setUp() throws Exception { public void testCreateStorageTransferClientScopedRequiredFalse() throws Exception { when(mockCredential.createScopedRequired()).thenReturn(false); - TransferClientCreator.createStorageTransferClient(Utils.getDefaultTransport(), - Utils.getDefaultJsonFactory(), mockCredential); + TransferClientCreator.createStorageTransferClient( + Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), mockCredential); verify(mockBuilder).build(); } @@ -80,11 +79,11 @@ public void testCreateStorageTransferClientScopedRequiredFalse() throws Exceptio @Test public void testCreateStorageTransferClientScopedRequiredTrue() throws Exception { when(mockCredential.createScopedRequired()).thenReturn(true); - when(mockCredential.createScoped(Matchers.anyCollectionOf(String.class))).thenReturn( - mockCredential); + when(mockCredential.createScoped(Matchers.anyCollectionOf(String.class))) + .thenReturn(mockCredential); - TransferClientCreator.createStorageTransferClient(Utils.getDefaultTransport(), - Utils.getDefaultJsonFactory(), mockCredential); + TransferClientCreator.createStorageTransferClient( + Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), mockCredential); verify(mockBuilder).build(); } diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java index dfb03495aa5..631132436d8 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,29 +16,29 @@ package com.google.cloud.storage.storagetransfer.samples.test; -import java.util.Random; - -import junit.framework.TestCase; - -import org.junit.Test; +import static org.junit.Assert.assertEquals; import com.google.api.services.storagetransfer.model.Date; import com.google.api.services.storagetransfer.model.TimeOfDay; import com.google.cloud.storage.storagetransfer.samples.TransferJobUtils; -public class TransferJobUtilsTest extends TestCase { - private Random r = new Random(); +import org.junit.Test; + +import java.util.Random; + +public class TransferJobUtilsTest { + private Random rand = new Random(); /** * Tests whether createDate() builds the correct date from a formatted String. */ @Test public void testCreateDate() throws Exception { - int year = r.nextInt(2000) + 1; - int month = r.nextInt(12) + 1; - int day = r.nextInt(30) + 1; - String dateString = Integer.toString(year) + "-" + Integer.toString(month) + "-" - + Integer.toString(day); + int year = rand.nextInt(2000) + 1; + int month = rand.nextInt(12) + 1; + int day = rand.nextInt(30) + 1; + String dateString = + Integer.toString(year) + "-" + Integer.toString(month) + "-" + Integer.toString(day); Date date = TransferJobUtils.createDate(dateString); @@ -50,16 +50,15 @@ public void testCreateDate() throws Exception { */ @Test public void testCreateTimeOfDay() throws Exception { - int hour = r.nextInt(24); - int minute = r.nextInt(60); - int second = r.nextInt(60); - String timeString = Integer.toString(hour) + ":" + Integer.toString(minute) + ":" - + Integer.toString(second); + int hour = rand.nextInt(24); + int minute = rand.nextInt(60); + int second = rand.nextInt(60); + String timeString = + Integer.toString(hour) + ":" + Integer.toString(minute) + ":" + Integer.toString(second); TimeOfDay time = TransferJobUtils.createTimeOfDay(timeString); assertEquals(time, - TimeOfDay.class.newInstance().setHours(hour).setMinutes(minute).setSeconds(second)); - + TimeOfDay.class.newInstance().setHours(hour).setMinutes(minute).setSeconds(second)); } -} \ No newline at end of file +} From 7d4d832fb4337d8a603083ac383146f6b9ad30a3 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 15 Mar 2016 16:16:17 -0700 Subject: [PATCH 2/2] Checkstyle fix for Storage XML command line sample. --- storage/xml-api/cmdline-sample/pom.xml | 17 +++-------------- .../src/test/java/StorageSampleTest.java | 17 +++++++---------- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/storage/xml-api/cmdline-sample/pom.xml b/storage/xml-api/cmdline-sample/pom.xml index fe0faaaf755..300792ac037 100644 --- a/storage/xml-api/cmdline-sample/pom.xml +++ b/storage/xml-api/cmdline-sample/pom.xml @@ -65,22 +65,11 @@ test - org.hamcrest - hamcrest-core - 1.3 + com.google.truth + truth + 0.28 test - - org.hamcrest - hamcrest-library - 1.3 - test - - - com.jcabi - jcabi-matchers - 1.3 - 1.20.0 diff --git a/storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java b/storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java index a1030b6d8e5..94b160664e8 100644 --- a/storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java +++ b/storage/xml-api/cmdline-sample/src/test/java/StorageSampleTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,22 +15,19 @@ */ // [START StorageSampleTest] - -import static com.jcabi.matchers.RegexMatchers.*; -import static org.junit.Assert.assertThat; +import static com.google.common.truth.Truth.assertThat; import org.junit.Test; -import java.util.regex.Pattern; - public class StorageSampleTest { @Test public void testListBucket() throws Exception { String listing = StorageSample.listBucket("cloud-samples-tests"); - assertThat(listing, matchesPattern( - ".*cloud-samples-tests.*" - + ".*")); + assertThat(listing) + .containsMatch( + ".*cloud-samples-tests.*" + + ".*"); } }