2020import com .google .cloud .ServiceOptions ;
2121import com .google .cloud .testing .BaseEmulatorHelper .EmulatorRunner ;
2222import com .google .common .collect .ImmutableList ;
23+
2324import java .io .ByteArrayInputStream ;
25+ import java .io .EOFException ;
26+ import java .io .File ;
2427import java .io .IOException ;
2528import java .io .InputStream ;
29+ import java .net .URL ;
30+ import java .net .URLConnection ;
31+ import java .net .URLStreamHandler ;
2632import java .util .List ;
2733import java .util .concurrent .TimeoutException ;
2834import java .util .logging .Logger ;
@@ -96,6 +102,41 @@ public void testEmulatorHelper() throws IOException, InterruptedException, Timeo
96102 EasyMock .verify ();
97103 }
98104
105+ @ Test
106+ public void testEmulatorHelperDownloadWithRetries () throws IOException , InterruptedException , TimeoutException {
107+ String mockExternalForm = "mockExternalForm" ;
108+ String mockInputStream = "mockInputStream" ;
109+ String mockProtocol = "mockProtocol" ;
110+ String mockFile = "mockFile" ;
111+ String mockCommandText = "mockCommandText" ;
112+
113+ MockURLStreamHandler mockURLStreamHandler = EasyMock .createMock (MockURLStreamHandler .class );
114+ URLConnection mockURLConnection = EasyMock .mock (URLConnection .class );
115+
116+ EasyMock .expect (mockURLStreamHandler .toExternalForm (EasyMock .anyObject (URL .class )))
117+ .andReturn (mockExternalForm ).anyTimes ();
118+ EasyMock .expect (mockURLConnection .getInputStream ())
119+ .andReturn (new ByteArrayInputStream (mockInputStream .getBytes ())).anyTimes ();
120+ EasyMock .expect (mockURLStreamHandler .openConnection (EasyMock .anyObject (URL .class )))
121+ .andThrow (new EOFException ()).times (1 );
122+ EasyMock .expect (mockURLStreamHandler .openConnection (EasyMock .anyObject (URL .class )))
123+ .andReturn (mockURLConnection ).times (1 );
124+ EasyMock .replay (mockURLStreamHandler , mockURLConnection );
125+
126+ URL url = new URL (mockProtocol , null , 0 , mockFile , mockURLStreamHandler );
127+ BaseEmulatorHelper .DownloadableEmulatorRunner runner =
128+ new BaseEmulatorHelper .DownloadableEmulatorRunner (ImmutableList .of (mockCommandText ), url , null );
129+
130+ File cachedFile = new File (System .getProperty ("java.io.tmpdir" ), mockExternalForm );
131+ cachedFile .delete (); //Clear the cached version so we're always testing the download
132+
133+ runner .start ();
134+
135+ EasyMock .verify ();
136+
137+ cachedFile .delete (); //Cleanup
138+ }
139+
99140 @ Test
100141 public void testEmulatorHelperMultipleRunners () throws IOException , InterruptedException , TimeoutException {
101142 Process process = EasyMock .createStrictMock (Process .class );
@@ -117,4 +158,19 @@ public void testEmulatorHelperMultipleRunners() throws IOException, InterruptedE
117158 helper .stop (Duration .ofMinutes (1 ));
118159 EasyMock .verify ();
119160 }
161+
162+ /**
163+ * URLStreamHandler has a protected method which needs to be mocked, so we need our own implementation in this package
164+ */
165+ private class MockURLStreamHandler extends URLStreamHandler {
166+ @ Override
167+ protected URLConnection openConnection (URL u ) throws IOException {
168+ return null ;
169+ }
170+
171+ @ Override
172+ protected String toExternalForm (URL u ) {
173+ return null ;
174+ }
175+ }
120176}
0 commit comments