DefaultUrlResolver implements IUrlResolver { @Override public ResolvedFile resolve( String url ) throws IOException { ResolvedFile resolvedFile; try { if( url.toLowerCase().startsWith( "file:/" )) { File f = new File( UriUtils.urlToUri( url )); resolvedFile = new ResolvedFile( f, true ); } else { URI uri = UriUtils.urlToUri( url ); InputStream in = uri.toURL().openStream(); File f = File.createTempFile( "rbcf_download_", null ); try { Utils.copyStream( in, f ); } finally { Utils.closeQuietly( in ); } resolvedFile = new ResolvedFile( f, false ); } } catch( Exception e ) { throw new IOException( e ); } return resolvedFile; }  @Override ResolvedFile resolve( String url );  }
@Test public void testResolve_fileUrl() throws Exception { DefaultUrlResolver resolver = new DefaultUrlResolver(); File f = this.folder.newFile(); ResolvedFile resolvedFile = resolver.resolve( f.toURI().toURL().toString()); Assert.assertEquals( f, resolvedFile.getFile()); Assert.assertTrue( resolvedFile.existedBefore()); }
@Test public void testResolve_httpUrl() throws Exception { final int portNumber = 11200; final String content = "something"; final CountDownLatch latch = new CountDownLatch( 1 ); Runnable serverRunnable = new Runnable() { @Override public void run() { try { ServerSocket serverSocket = new ServerSocket( portNumber ); latch.countDown(); Socket clientSocket = serverSocket.accept(); clientSocket.getOutputStream().write( "HTTP/1.0 200 OK\r\n\r\n".getBytes( "UTF-8" )); clientSocket.getOutputStream().write( content.getBytes( "UTF-8" )); clientSocket.getOutputStream().flush(); clientSocket.close(); serverSocket.close(); } catch( IOException e ) { e.printStackTrace(); } } }; Thread t = new Thread( serverRunnable ); t.start(); latch.await(); DefaultUrlResolver resolver = new DefaultUrlResolver(); ResolvedFile resolvedFile = null; for( int i=0; i<5; i++ ) { Thread.sleep( 200 ); try { resolvedFile = resolver.resolve( "http: break; } catch( Exception e ) { } } t.join(); Assert.assertNotNull( resolvedFile ); Assert.assertTrue( resolvedFile.getFile().exists()); Assert.assertEquals( content, Utils.readFileContent( resolvedFile.getFile())); Assert.assertFalse( resolvedFile.existedBefore()); }
@Test( expected = IOException.class ) public void testResolve_unreachableUrl() throws Exception { DefaultUrlResolver resolver = new DefaultUrlResolver(); resolver.resolve( "http: }
@Test( expected = IOException.class ) public void testResolve_mavenUrl() throws Exception { DefaultUrlResolver resolver = new DefaultUrlResolver(); resolver.resolve( "mvn:net.roboconf/my-app/1.0" ); }
@Test( expected = IOException.class ) public void testResolve_invalidUrl() throws Exception { DefaultUrlResolver resolver = new DefaultUrlResolver(); resolver.resolve( "localhost" ); }