use of io.fabric8.maven.MavenResolver in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testUploadWithMimeMultipartFormData.
@Test
public void testUploadWithMimeMultipartFormData() throws Exception {
new File("target/maven/proxy/tmp/multipart").mkdirs();
System.setProperty("karaf.data", new File("target").getCanonicalPath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream jas = new JarOutputStream(baos);
addEntry(jas, "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n".getBytes());
addEntry(jas, "META-INF/maven/io.fabric8/mybundle/pom.properties", "groupId=io.fabric8\nartifactId=mybundle\nversion=1.0\n".getBytes());
jas.close();
byte[] jarBytes = baos.toByteArray();
RuntimeProperties props = new MockRuntimeProperties();
MavenResolver resolver = EasyMock.createMock(MavenResolver.class);
MavenUploadProxyServlet servlet = new MavenUploadProxyServlet(resolver, props, projectDeployer, new File("target/upload"), 0);
servlet.setFileItemFactory(new DiskFileItemFactory(0, new File("target/maven/proxy/tmp/multipart")));
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
FilePart part = new FilePart("file[]", new ByteArrayPartSource("mybundle-1.0.jar", jarBytes));
MultipartRequestEntity entity = new MultipartRequestEntity(new Part[] { part }, new HttpMethodParams());
final ByteArrayOutputStream requestBytes = new ByteArrayOutputStream();
entity.writeRequest(requestBytes);
final byte[] multipartRequestBytes = requestBytes.toByteArray();
EasyMock.expect(request.getPathInfo()).andReturn("/mybundle-1.0.jar");
EasyMock.expect(request.getHeader(MavenProxyServletSupport.LOCATION_HEADER)).andReturn(null);
EasyMock.expect(request.getParameter("profile")).andReturn("my");
EasyMock.expect(request.getParameter("version")).andReturn("1.0");
EasyMock.expect(request.getContentType()).andReturn(entity.getContentType()).anyTimes();
EasyMock.expect(request.getHeader("Content-length")).andReturn(Long.toString(entity.getContentLength())).anyTimes();
EasyMock.expect(request.getContentLength()).andReturn((int) entity.getContentLength()).anyTimes();
EasyMock.expect(request.getCharacterEncoding()).andReturn("ISO-8859-1").anyTimes();
Capture<String> location = EasyMock.newCapture(CaptureType.ALL);
response.setStatus(HttpServletResponse.SC_ACCEPTED);
EasyMock.expect(request.getInputStream()).andReturn(new ServletInputStream() {
private int pos = 0;
@Override
public int read() throws IOException {
return pos >= multipartRequestBytes.length ? -1 : (multipartRequestBytes[pos++] & 0xFF);
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
}
});
Capture<ProjectRequirements> requirementsCapture = EasyMock.newCapture(CaptureType.FIRST);
EasyMock.expect(projectDeployer.deployProject(EasyMock.capture(requirementsCapture), EasyMock.eq(true))).andReturn(null);
EasyMock.replay(resolver, request, response, projectDeployer);
servlet.doPut(request, response);
FileInputStream fis = new FileInputStream("target/upload/io.fabric8/mybundle/1.0/mybundle-1.0.jar");
ByteArrayOutputStream storedBundleBytes = new ByteArrayOutputStream();
IOUtils.copy(fis, storedBundleBytes);
fis.close();
Assert.assertArrayEquals(jarBytes, storedBundleBytes.toByteArray());
ProjectRequirements pr = requirementsCapture.getValue();
List<String> bundles = pr.getBundles();
assertThat(bundles.size(), equalTo(1));
assertThat(bundles.get(0), equalTo("mvn:io.fabric8/mybundle/1.0"));
assertThat(pr.getProfileId(), equalTo("my"));
assertThat(pr.getVersion(), equalTo("1.0"));
assertThat(pr.getGroupId(), nullValue());
assertThat(pr.getArtifactId(), nullValue());
EasyMock.verify(resolver, request, response, projectDeployer);
}
use of io.fabric8.maven.MavenResolver in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testStartServlet.
@Test(timeout = 30000)
public void testStartServlet() throws Exception {
String old = System.getProperty("karaf.data");
System.setProperty("karaf.data", new File("target").getCanonicalPath());
try {
MavenResolver resolver = createResolver();
MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtimeProperties, projectDeployer, 5, 0);
servlet.start();
} finally {
if (old != null) {
System.setProperty("karaf.data", old);
}
}
}
use of io.fabric8.maven.MavenResolver in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testDownloadMetadata.
@Test(timeout = 30000)
public void testDownloadMetadata() throws Exception {
final String old = System.getProperty("karaf.data");
System.setProperty("karaf.data", new File("target").getCanonicalPath());
FileUtils.deleteDirectory(new File("target/tmp"));
Server server = new Server(0);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String result = null;
if ("/repo1/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + " <groupId>org.apache.camel</groupId>\n" + " <artifactId>camel-core</artifactId>\n" + " <versioning>\n" + " <latest>2.14.0</latest>\n" + " <release>2.14.0</release>\n" + " <versions>\n" + " <version>1.6.1</version>\n" + " <version>1.6.2</version>\n" + " <version>1.6.3</version>\n" + " <version>1.6.4</version>\n" + " <version>2.0-M2</version>\n" + " <version>2.0-M3</version>\n" + " <version>2.0.0</version>\n" + " <version>2.1.0</version>\n" + " <version>2.2.0</version>\n" + " <version>2.3.0</version>\n" + " <version>2.4.0</version>\n" + " <version>2.5.0</version>\n" + " <version>2.6.0</version>\n" + " <version>2.7.0</version>\n" + " <version>2.7.1</version>\n" + " <version>2.7.2</version>\n" + " <version>2.7.3</version>\n" + " <version>2.7.4</version>\n" + " <version>2.7.5</version>\n" + " <version>2.8.0</version>\n" + " <version>2.8.1</version>\n" + " <version>2.8.2</version>\n" + " <version>2.8.3</version>\n" + " <version>2.8.4</version>\n" + " <version>2.8.5</version>\n" + " <version>2.8.6</version>\n" + " <version>2.9.0-RC1</version>\n" + " <version>2.9.0</version>\n" + " <version>2.9.1</version>\n" + " <version>2.9.2</version>\n" + " <version>2.9.3</version>\n" + " <version>2.9.4</version>\n" + " <version>2.9.5</version>\n" + " <version>2.9.6</version>\n" + " <version>2.9.7</version>\n" + " <version>2.9.8</version>\n" + " <version>2.10.0</version>\n" + " <version>2.10.1</version>\n" + " <version>2.10.2</version>\n" + " <version>2.10.3</version>\n" + " <version>2.10.4</version>\n" + " <version>2.10.5</version>\n" + " <version>2.10.6</version>\n" + " <version>2.10.7</version>\n" + " <version>2.11.0</version>\n" + " <version>2.11.1</version>\n" + " <version>2.11.2</version>\n" + " <version>2.11.3</version>\n" + " <version>2.11.4</version>\n" + " <version>2.12.0</version>\n" + " <version>2.12.1</version>\n" + " <version>2.12.2</version>\n" + " <version>2.12.3</version>\n" + " <version>2.12.4</version>\n" + " <version>2.13.0</version>\n" + " <version>2.13.1</version>\n" + " <version>2.13.2</version>\n" + " <version>2.14.0</version>\n" + " </versions>\n" + " <lastUpdated>20140918132816</lastUpdated>\n" + " </versioning>\n" + "</metadata>\n" + "\n";
} else if ("/repo2/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata modelVersion=\"1.1.0\">\n" + " <groupId>org.apache.camel</groupId>\n" + " <artifactId>camel-core</artifactId>\n" + " <versioning>\n" + " <latest>2.14.0.redhat-620034</latest>\n" + " <release>2.14.0.redhat-620034</release>\n" + " <versions>\n" + " <version>2.10.0.redhat-60074</version>\n" + " <version>2.12.0.redhat-610312</version>\n" + " <version>2.12.0.redhat-610328</version>\n" + " <version>2.12.0.redhat-610355</version>\n" + " <version>2.12.0.redhat-610378</version>\n" + " <version>2.12.0.redhat-610396</version>\n" + " <version>2.12.0.redhat-610399</version>\n" + " <version>2.12.0.redhat-610401</version>\n" + " <version>2.12.0.redhat-610402</version>\n" + " <version>2.12.0.redhat-611403</version>\n" + " <version>2.12.0.redhat-611405</version>\n" + " <version>2.12.0.redhat-611406</version>\n" + " <version>2.12.0.redhat-611408</version>\n" + " <version>2.12.0.redhat-611409</version>\n" + " <version>2.12.0.redhat-611410</version>\n" + " <version>2.12.0.redhat-611411</version>\n" + " <version>2.12.0.redhat-611412</version>\n" + " <version>2.14.0.redhat-620031</version>\n" + " <version>2.14.0.redhat-620033</version>\n" + " <version>2.14.0.redhat-620034</version>\n" + " </versions>\n" + " <lastUpdated>20141019130841</lastUpdated>\n" + " </versioning>\n" + "</metadata>\n" + "\n";
}
if (result == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
baseRequest.setHandled(true);
response.getOutputStream().close();
} else {
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getOutputStream().write(result.getBytes());
response.getOutputStream().close();
}
}
});
server.start();
try {
int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
List<String> remoteRepos = Arrays.asList("http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2");
RuntimeProperties props = new MockRuntimeProperties();
// TODO: local repo should point to target/tmp
MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
AsyncContext context = EasyMock.createMock(AsyncContext.class);
EasyMock.makeThreadSafe(context, true);
final Map<String, Object> attributes = new HashMap<>();
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
@Override
public void setAttribute(String name, Object o) {
attributes.put(name, o);
}
};
EasyMock.makeThreadSafe(request, true);
EasyMock.expect(request.getMethod()).andReturn("GET");
EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
EasyMock.makeThreadSafe(response, true);
response.setStatus(EasyMock.anyInt());
EasyMock.expectLastCall().anyTimes();
response.setContentLength(EasyMock.anyInt());
EasyMock.expectLastCall().anyTimes();
response.setContentType((String) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
EasyMock.expectLastCall().anyTimes();
response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
response.flushBuffer();
EasyMock.expectLastCall().anyTimes();
final CountDownLatch latchDispatch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchDispatch.countDown();
return null;
}
});
EasyMock.replay(request, response, context);
servlet.start();
servlet.doGet(requestWrapper, response);
latchDispatch.await();
EasyMock.verify(request, response, context);
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
baos.write(b, off, len);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
}).anyTimes();
response.flushBuffer();
EasyMock.expectLastCall().anyTimes();
final CountDownLatch latchComplete = new CountDownLatch(1);
context.complete();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchComplete.countDown();
return null;
}
});
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
EasyMock.verify(request, response, context);
org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader().read(new ByteArrayInputStream(baos.toByteArray()), false);
assertEquals("2.14.0.redhat-620034", m.getVersioning().getLatest());
assertTrue(m.getVersioning().getVersions().contains("2.10.4"));
assertTrue(m.getVersioning().getVersions().contains("2.12.0.redhat-610399"));
EasyMock.verify(request, response, context);
} finally {
server.stop();
if (old != null) {
System.setProperty("karaf.data", old);
}
}
}
use of io.fabric8.maven.MavenResolver in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testDownload.
private void testDownload(Handler serverHandler) throws Exception {
final String old = System.getProperty("karaf.data");
System.setProperty("karaf.data", new File("target").getCanonicalPath());
FileUtils.deleteDirectory(new File("target/tmp"));
Server server = new Server(0);
server.setHandler(serverHandler);
server.start();
try {
int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
List<String> remoteRepos = Arrays.asList("http://relevant.not/maven2@id=central");
RuntimeProperties props = new MockRuntimeProperties();
// TODO: local repo should point to target/tmp
MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
AsyncContext context = EasyMock.createMock(AsyncContext.class);
EasyMock.makeThreadSafe(context, true);
final Map<String, Object> attributes = new HashMap<>();
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
@Override
public void setAttribute(String name, Object o) {
attributes.put(name, o);
}
};
EasyMock.makeThreadSafe(request, true);
EasyMock.expect(request.getMethod()).andReturn("GET");
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
EasyMock.makeThreadSafe(response, true);
response.setStatus(EasyMock.anyInt());
EasyMock.expectLastCall();
response.setContentLength(EasyMock.anyInt());
EasyMock.expectLastCall();
response.setContentType((String) EasyMock.anyObject());
EasyMock.expectLastCall();
response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
EasyMock.expectLastCall();
response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
final CountDownLatch latchDispatch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchDispatch.countDown();
return null;
}
});
EasyMock.replay(request, response, context);
servlet.start();
servlet.doGet(requestWrapper, response);
latchDispatch.await();
EasyMock.verify(request, response, context);
//
// Subsequent call
//
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ServletOutputStream outputStream = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
baos.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
baos.write(b, off, len);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
};
while (true) {
long size = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".size");
long pos = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".position");
ByteBuffer buffer = (ByteBuffer) attributes.get(ByteBuffer.class.getName());
if (pos + buffer.position() >= size) {
break;
}
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final CountDownLatch latch = new CountDownLatch(1);
context.dispatch();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latch.countDown();
return null;
}
});
EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
latch.await();
EasyMock.verify(request, response, context);
}
//
// Last calls
//
EasyMock.reset(request, response, context);
EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
EasyMock.expect(request.startAsync()).andReturn(context);
context.setTimeout(EasyMock.anyInt());
EasyMock.expectLastCall();
context.addListener((AsyncListener) EasyMock.anyObject());
EasyMock.expectLastCall();
final CountDownLatch latchComplete = new CountDownLatch(1);
context.complete();
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
latchComplete.countDown();
return null;
}
});
EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
response.flushBuffer();
EasyMock.expectLastCall();
EasyMock.replay(request, response, context);
servlet.doGet(requestWrapper, response);
latchComplete.await();
EasyMock.verify(request, response, context);
Assert.assertArrayEquals(new byte[] { 0x42 }, baos.toByteArray());
} finally {
server.stop();
if (old != null) {
System.setProperty("karaf.data", old);
}
}
}
use of io.fabric8.maven.MavenResolver in project fabric8 by jboss-fuse.
the class MavenProxySnapshotResolutionTest method snapshotIsAvailableInTwoDefaultRepositories.
@Test
public void snapshotIsAvailableInTwoDefaultRepositories() throws IOException, InvalidMavenArtifactRequest {
File defaultRepository1 = initFileRepository("dr1");
File defaultRepository2 = initFileRepository("dr2");
MavenResolver resolver = new ResolverBuilder().withDefaultRepositories(Arrays.asList(defaultRepository1, defaultRepository2)).build();
MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtime, null, 1, 0);
servlet.start();
mvnInstall(defaultRepository1, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("10:00"), "a");
mvnInstall(defaultRepository2, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("11:00"), "b");
File file = servlet.download("io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata.xml");
Metadata metadata = readMetadata(file);
assertThat("Resolution done in first default repository, even if second one has newer artifact", metadata.getVersioning().getSnapshotVersions().get(0).getUpdated(), is("20170101100000"));
}
Aggregations