2828
2929import java .io .IOException ;
3030
31- import junit .framework .Assert ;
3231import junit .framework .TestCase ;
3332
3433/**
@@ -69,10 +68,48 @@ public void testJsonHttpClientBuilder() {
6968 .setJsonHttpRequestInitializer (jsonHttpRequestInitializer )
7069 .setApplicationName (applicationName ).build ();
7170
72- Assert .assertEquals (baseUrl .build (), client .getBaseUrl ());
73- Assert .assertEquals (applicationName , client .getApplicationName ());
74- Assert .assertEquals (jsonFactory , client .getJsonFactory ());
75- Assert .assertEquals (jsonHttpRequestInitializer , client .getJsonHttpRequestInitializer ());
71+ assertEquals (baseUrl .build (), client .getBaseUrl ());
72+ assertEquals (applicationName , client .getApplicationName ());
73+ assertEquals (jsonFactory , client .getJsonFactory ());
74+ assertEquals (jsonHttpRequestInitializer , client .getJsonHttpRequestInitializer ());
75+ }
76+
77+ public void testBaseServerAndBasePathBuilder () {
78+ JsonHttpClient client =
79+ JsonHttpClient
80+ .builder (new NetHttpTransport (), new JacksonFactory (),
81+ new GenericUrl ("http://www.testgoogleapis.com/test/path/v1/" ))
82+ .setBaseHost ("www.googleapis.com" )
83+ .setBasePath ("/test/path/v2/" )
84+ .build ();
85+
86+ assertEquals ("http://www.googleapis.com/test/path/v2/" , client .getBaseUrl ());
87+ }
88+
89+ public void testInvalidBasePath () {
90+ JsonHttpClient .Builder builder =
91+ JsonHttpClient .builder (new NetHttpTransport (), new JacksonFactory (), new GenericUrl (
92+ "http://www.testgoogleapis.com/test/path/v1/" ));
93+ try {
94+ builder .setBasePath (null );
95+ fail ("Expected exception not thrown!" );
96+ } catch (NullPointerException e ) {
97+ // Expected because base path cannot be null.
98+ }
99+
100+ try {
101+ builder .setBasePath ("test/path/v2/" );
102+ fail ("Expected exception not thrown!" );
103+ } catch (IllegalArgumentException e ) {
104+ // Expected because base path did not start with "/".
105+ }
106+
107+ try {
108+ builder .setBasePath ("/test/path/v2" );
109+ fail ("Expected exception not thrown!" );
110+ } catch (IllegalArgumentException e ) {
111+ // Expected because base path did not end with "/".
112+ }
76113 }
77114
78115 public void testInitialize () throws IOException {
@@ -83,7 +120,7 @@ public void testInitialize() throws IOException {
83120 .setJsonHttpRequestInitializer (remoteRequestInitializer )
84121 .setApplicationName ("Test Application" ).build ();
85122 client .initialize (null );
86- Assert . assertTrue (remoteRequestInitializer .isCalled );
123+ assertTrue (remoteRequestInitializer .isCalled );
87124 }
88125
89126 public void testExecute () throws IOException {
@@ -97,7 +134,7 @@ public LowLevelHttpRequest buildGetRequest(final String url) {
97134 public LowLevelHttpResponse execute () {
98135 MockLowLevelHttpResponse response = new MockLowLevelHttpResponse ();
99136 // Assert the requested URL is the expected one.
100- Assert . assertEquals (testBaseUrl + testUriTemplate , url );
137+ assertEquals (testBaseUrl + testUriTemplate , url );
101138 return response ;
102139 }
103140 };
0 commit comments