use of org.commonjava.maven.galley.model.ConcreteResource in project galley by Commonjava.
the class AbstractTransferManagerTest method retrieve_cacheIfMissing.
/**
* Test that remote content will be downloaded then cached.
*/
@Test
public void retrieve_cacheIfMissing() throws Exception {
final String testContent = "This is a test " + System.currentTimeMillis();
final Location loc = new SimpleLocation("file:///test-repo");
final String path = "/path/to/test.txt";
final ConcreteResource resource = new ConcreteResource(loc, path);
// put in the content that we want to "download"
getTransport().registerDownload(resource, new TestDownload(testContent.getBytes()));
// now, use the manager to retrieve() the path...the remote content should come through here.
Transfer transfer = getTransferManagerImpl().retrieve(resource);
assertTransferContent(transfer, testContent);
// now, the right content should be cached.
// So, we'll put in some wrong content that will cause problems if the cache isn't used.
getTransport().registerDownload(resource, new TestDownload("This is WRONG".getBytes()));
// now, use the manager to retrieve() the path again...the cached content should come through here.
transfer = getTransferManagerImpl().retrieve(resource);
assertTransferContent(transfer, testContent);
}
use of org.commonjava.maven.galley.model.ConcreteResource in project galley by Commonjava.
the class HttpDownloadTest method IOExceptionDuringDownloadTransferDeletesTargetFile.
@Test
@BMRule(name = "throw IOException during writeTarget copy operation", targetClass = "HttpDownload", targetMethod = "doCopy", targetLocation = "ENTRY", condition = "!flagged(\"firstCopy\")", action = "flag(\"firstCopy\"); throw new IOException(\"BMUnit exception\");")
public void IOExceptionDuringDownloadTransferDeletesTargetFile() throws Exception {
final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
final String path = "/path/to/file";
fixture.getServer().expect("GET", fixture.formatUrl(path), new ExpectationHandler() {
int count = 0;
@Override
public void handle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setStatus(200);
httpServletResponse.setHeader("Content-Length", Integer.toString(content.length()));
PrintWriter writer = httpServletResponse.getWriter();
if (count < 1) {
writer.write(content.substring(0, content.length() / 2));
} else {
writer.write(content);
}
count++;
}
});
final String baseUri = fixture.getBaseUri();
final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, path));
final String url = fixture.formatUrl(path);
assertThat(transfer.exists(), equalTo(false));
// first call, server should quit transferring halfway through the transfer
HttpDownload dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
DownloadJob resultJob = dl.call();
TransferException error = dl.getError();
assertThat(error, notNullValue());
error.printStackTrace();
assertThat(resultJob, notNullValue());
Transfer result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(false));
assertThat(transfer.exists(), equalTo(false));
// second call should hit upstream again and succeed.
dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
resultJob = dl.call();
error = dl.getError();
assertThat(error, nullValue());
assertThat(resultJob, notNullValue());
result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(true));
assertThat(transfer.exists(), equalTo(true));
final String urlPath = fixture.getUrlPath(url);
assertThat(fixture.getAccessesFor(urlPath), equalTo(2));
}
use of org.commonjava.maven.galley.model.ConcreteResource in project galley by Commonjava.
the class HttpDownloadTest method simpleRetriveOfRedirectUrl.
@Test
public void simpleRetriveOfRedirectUrl() throws Exception {
final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
final String redirectPath = "/path/to/file";
final String path = "/redirect/to/file";
fixture.getServer().expect("GET", fixture.formatUrl(path), new ExpectationHandler() {
@Override
public void handle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setStatus(302);
httpServletResponse.setHeader("Location", fixture.formatUrl(redirectPath));
}
});
fixture.getServer().expect("GET", fixture.formatUrl(redirectPath), 200, content);
final String baseUri = fixture.getBaseUri();
final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, path));
final String url = fixture.formatUrl(path);
Map<Transfer, Long> transferSizes = new HashMap<Transfer, Long>();
assertThat(transfer.exists(), equalTo(false));
final HttpDownload dl = new HttpDownload(url, location, transfer, transferSizes, new EventMetadata(), fixture.getHttp(), new ObjectMapper());
final DownloadJob resultJob = dl.call();
final TransferException error = dl.getError();
assertThat(error, nullValue());
assertThat(resultJob, notNullValue());
final Transfer result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(true));
assertThat(transfer.exists(), equalTo(true));
final String postPath = fixture.getUrlPath(url);
assertThat(fixture.getAccessesFor(postPath), equalTo(1));
}
use of org.commonjava.maven.galley.model.ConcreteResource in project pom-manipulation-ext by release-engineering.
the class MavenLocationExpander method expand.
@Override
public VirtualResource expand(final Resource resource) throws TransferException {
final List<ConcreteResource> result = new ArrayList<>();
if (resource instanceof ConcreteResource) {
expandSingle((ConcreteResource) resource, result);
} else {
for (final ConcreteResource cr : ((VirtualResource) resource).toConcreteResources()) {
expandSingle(cr, result);
}
}
logger.debug("Expanded to:\n {}", new JoinString("\n ", result));
return new VirtualResource(result);
}
use of org.commonjava.maven.galley.model.ConcreteResource in project pom-manipulation-ext by release-engineering.
the class MavenLocationExpander method expandSingle.
private void expandSingle(final ConcreteResource cr, final List<ConcreteResource> result) {
final Location loc = cr.getLocation();
final List<Location> expanded = new ArrayList<>();
expandSingle(loc, expanded);
final String path = cr.getPath();
for (final Location location : expanded) {
result.add(new ConcreteResource(location, path));
}
}
Aggregations