<commit_before>package aQute.bnd.repository.maven.provider;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.w3c.dom.Document;

import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;
import aQute.bnd.http.HttpClient;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.resource.ResourceUtils;
import aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability;
import aQute.bnd.service.RepositoryPlugin.PutOptions;
import aQute.bnd.service.RepositoryPlugin.PutResult;
import aQute.bnd.service.maven.PomOptions;
import aQute.bnd.service.progress.ProgressPlugin;
import aQute.bnd.service.progress.ProgressPlugin.Task;
import aQute.bnd.version.Version;
import aQute.http.testservers.HttpTestServer.Config;
import aQute.lib.io.IO;
import aQute.maven.api.Archive;
import aQute.maven.api.IPom;
import aQute.maven.api.MavenScope;
import aQute.maven.api.Revision;
import aQute.maven.provider.FakeNexus;
import aQute.maven.provider.MavenRepository;
import aQute.maven.provider.POM;
import junit.framework.TestCase;

/*
 * Create a remote and local repository with a lot of bad stuff. The repo contains:
 *
 */
public class MavenBndRepoTest extends TestCase {
	private static final Version		DTO_VERSION	= Version.parseVersion("1.0.0.201505202023");
	final static DocumentBuilderFactory	dbf			= DocumentBuilderFactory.newInstance();
	final static XPathFactory			xpf			= XPathFactory.newInstance();
	String								tmpName;
	File								tmp;
	File								local;
	File								remote;
	File								index;

	private MavenBndRepository			repo;
	private FakeNexus					fnx;
	private Processor					domain;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		tmpName = "generated/tmp/test/" + getName();
		tmp = IO.getFile(tmpName);
		IO.delete(tmp);
		local = IO.getFile(tmp, "local");
		remote = IO.getFile(tmp, "remote");
		index = IO.getFile(tmp, "index");
		remote.mkdirs();
		local.mkdirs();

		IO.copy(IO.getFile("testresources/mavenrepo"), remote);
		IO.copy(IO.getFile("testresources/mavenrepo/index.maven"), index);

		Config config = new Config();
		fnx = new FakeNexus(config, remote);
		fnx.start();
	}

	@Override
	protected void tearDown() throws Exception {
		domain.close();
		repo.close();
		fnx.close();
		super.tearDown();
	}

	public void testProgramRemoveFromIndex() throws Exception {
		config(null);
		assertNotNull(repo.get("org.osgi.dto", DTO_VERSION, null));
		String indexContent = IO.collect(index);
		assertThat(indexContent).contains("org.osgi.dto:1.0.0");

		Map<String, Runnable> actions = repo.actions("org.osgi.dto");
		assertNotNull(actions);
		actions.get("Delete All from Index")
			.run();
		assertNull(repo.get("org.osgi.dto", DTO_VERSION, null));
		assertThat(repo.list("org.osgi.dto")).isEmpty();
		indexContent = IO.collect(index);
		assertThat(indexContent).doesNotContain("org.osgi.dto:1.0.0");
	}

	public void testRevisionRemoveFromIndex() throws Exception {
		config(null);
		assertNotNull(repo.get("org.osgi.dto", DTO_VERSION, null));
		String indexContent = IO.collect(index);

		Map<String, Runnable> actions = repo.actions("org.osgi.dto", DTO_VERSION);
		assertNotNull(actions);
		actions.get("Delete from Index")
			.run();
		assertThat(repo.list("org.osgi.dto")).isEmpty();
		indexContent = IO.collect(index);
		assertThat(indexContent).doesNotContain("org.osgi.dto:1.0.0");
	}

	public void testAdd() throws Exception {
		config(null);
		assertThat(repo.list("org.osgi.service.log")).isEmpty();
		String indexContent = IO.collect(index);
		assertThat(indexContent).doesNotContain("org.osgi.service.log");
		repo.index.add(Archive.valueOf("org.osgi:org.osgi.service.log:1.3.0"));
		assertThat(repo.list("org.osgi.service.log")).isNotEmpty();
		indexContent = IO.collect(index);
		assertThat(indexContent).contains("org.osgi.service.log:1.3.0");
	}

	public void testUseSource() throws Exception {
		Map<String, String> config = new HashMap<>();
		config.put("source", "org.osgi:org.osgi.service.log:1.3.0, ; \torg.osgi:org.osgi.service.log:1.2.0");
		config(config);
		assertThat(repo.list("org.osgi.service.log")).isNotEmpty();
	}

	public void testTooltip() throws Exception {
		config(null);
		String tooltip = repo.tooltip();
		assertNotNull(tooltip);

		String title = repo.title("org.apache.commons.cli");
		assertEquals("org.apache.commons.cli", title);

		tooltip = repo.tooltip("org.apache.commons.cli", new Version("1.2"));
		assertNotNull(tooltip);
		System.out.println(tooltip);
		title = repo.title("org.apache.commons.cli", new Version("1.2"));
		assertEquals("1.2.0", title);
		tooltip = repo.tooltip("commons-cli:commons-cli", new Version("1.0"));
		assertNotNull(tooltip);
		System.out.println(tooltip);

		title = repo.title("commons-cli:commons-cli");
		assertEquals("commons-cli:commons-cli [!]", title);

		title = repo.title("commons-cli:commons-cli", new Version("1.0"));
		assertEquals("1.0.0 [Not a bundle]", title);

		title = repo.title("commons-cli:commons-cli", new Version("1.4.0.SNAPSHOT"));
		assertEquals("1.4.0.SNAPSHOT [Not found]", title);

	}

	public void testZipFileWithContents() throws Exception {
		IO.copy(IO.getFile("testresources/mavenrepo2"), remote);
		IO.copy(IO.getFile("testresources/mavenrepo2/index.maven"), index);
		Map<String, String> config = new HashMap<>();
		config.put("index", index.getAbsolutePath());
		config.put("multi", " zip , par, foo");
		config(config);
		String multi_version = "1.0.0";
		domain.setProperty("multi_version", multi_version);
		File file = repo.get("group:artifact:zip:", Version.parseVersion(multi_version), null);
		assertNotNull(file);
		assertTrue(file.isFile());

		File file2 = repo.get("name.njbartlett.eclipse.macbadge", new Version("1.0.0.201110100042"), null);
		assertNotNull(file2);

		File file3 = repo.get("group:artifact:jar:1003", Version.parseVersion(multi_version), null);
		assertNotNull(file3);
		assertTrue(file3.isFile());

		assertTrue(file2.equals(file3));

		repo.index.remove(Archive.valueOf("group:artifact:zip:" + multi_version));

		file2 = repo.get("name.njbartlett.eclipse.macbadge", new Version("1.0.0.201110100042"), null);
		assertNull(file2);
		file3 = repo.get("group:artifact:jar:1003", Version.parseVersion(multi_version), null);
		assertNull(file3);
	}

	public void testPut() throws Exception {

		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(map);
		File jar = IO.getFile("testresources/release.jar");
		PutResult put = repo.put(new FileInputStream(jar), null);

		assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.jar", 140212);
		File file = assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.pom", 1470);
		IPom pom = repo.storage.getPom(new FileInputStream(file));
		assertEquals("jar", pom.getPackaging());
		assertEquals(true, pom.hasValidGAV());
		assertEquals("biz.aQute.bnd:biz.aQute.bnd.maven:3.2.0", pom.getRevision()
			.toString());

		Document doc = dbf.newDocumentBuilder()
			.parse(file);
		XPath xp = xpf.newXPath();

		assertEquals("biz.aQute.bnd.maven", xp.evaluate("/project/artifactId", doc)
			.trim());
		assertEquals("3.2.0", xp.evaluate("/project/version", doc)
			.trim());
		assertEquals("biz.aQute.bnd", xp.evaluate("/project/groupId", doc)
			.trim());

		assertEquals("http://bnd.bndtools.org/", xp.evaluate("/project/url", doc)
			.trim());
		assertEquals("Bndtools", xp.evaluate("/project/organization/name", doc)
			.trim());
		assertEquals("http://bndtools.org/", xp.evaluate("/project/organization/url", doc)
			.trim());

		assertEquals("https://github.com/bndtools/bnd", xp.evaluate("/project/scm/url", doc)
			.trim());
		assertEquals("scm:git:https://github.com/bndtools/bnd.git", xp.evaluate("/project/scm/connection", doc)
			.trim());
		assertEquals("scm:git:<EMAIL>:bndtools/bnd.git", xp.evaluate("/project/scm/developerConnection", doc)
			.trim());

		// IO.copy(file, System.out);

		put = repo.put(new FileInputStream(jar), null);

		Requirement wc = ResourceUtils.createWildcardRequirement();
		Collection<Capability> caps = repo.findProviders(Collections.singleton(wc))
			.get(wc);
		Set<Resource> resources = ResourceUtils.getResources(caps);
		assertEquals(3, resources.size());
		IdentityCapability bc = ResourceUtils.getIdentityCapability(resources.iterator()
			.next());
		assertEquals("biz.aQute.bnd.maven", bc.osgi_identity());
	}

	public void testPutMavenReleaseSources() throws Exception {
		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(map);
		try (Processor context = new Processor()) {
			context.setProperty("-maven-release", "sources;path=\"testresources/src\",javadoc;packages=all");
			File jar = IO.getFile("testresources/release.jar");
			PutOptions options = new PutOptions();
			options.context = context;
			PutResult put = repo.put(new FileInputStream(jar), options);

			assertIsFile(remote, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.jar", 140212);
			File sources = assertIsFile(remote,
				"biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0-sources.jar", 0);
			try (Jar sourcesJar = new Jar(sources)) {
				assertThat(sourcesJar.exists("X.java")).isTrue();
			}
			File javadoc = assertIsFile(remote,
				"biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0-javadoc.jar", 0);
			try (Jar javadocJar = new Jar(javadoc)) {
				assertThat(javadocJar.exists("X.html")).isTrue();
			}
		}
	}

	public void testPomGenerate() throws Exception {
		config(null);
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		PomOptions po = new PomOptions();
		po.gav = "test:test:1.0";
		po.parent = null;
		po.dependencyManagement = false;

		repo.toPom(bout, po);
		ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
		POM pom = new POM((MavenRepository) repo.storage, bin);

		assertEquals(Revision.valueOf("test:test:1.0"), pom.getRevision());
		assertEquals(4, pom.getDependencies(EnumSet.of(MavenScope.runtime), false)
			.size());
		System.out.println(new String(bout.toByteArray(), StandardCharsets.UTF_8));
	}

	public void testPutLocalTwiceNoSnapshot() throws Exception {
		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(map);
		Requirement wc = ResourceUtils.createWildcardRequirement();
		Collection<Capability> caps = repo.findProviders(Collections.singleton(wc))
			.get(wc);
		Set<Resource> resources = ResourceUtils.getResources(caps);
		assertThat(resources)
			.extracting(ResourceUtils::getIdentityCapability)
			.filteredOn(Objects::nonNull)
			.extracting(IdentityCapability::osgi_identity)
			.containsExactlyInAnyOrder("org.osgi.dto", "org.apache.commons.cli");

		File jar = IO.getFile("testresources/release.jar");
		PutResult put = repo.put(new FileInputStream(jar), null);

		assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.jar", 140212);
		assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.pom", 1470);
		put = repo.put(new FileInputStream(jar), null);

		caps = repo.findProviders(Collections.singleton(wc))
			.get(wc);
		resources = ResourceUtils.getResources(caps);
		assertThat(resources)
			.extracting(ResourceUtils::getIdentityCapability)
			.filteredOn(Objects::nonNull)
			.extracting(IdentityCapability::osgi_identity)
			.containsExactlyInAnyOrder("org.osgi.dto", "org.apache.commons.cli", "biz.aQute.bnd.maven");
	}

	public void testPutReleaseAndThenIndex() throws Exception {
		Workspace ws = new Workspace(IO.getFile("testdata/releasews"));
		Project p1 = ws.getProject("p1");
		Project indexProject = ws.getProject("index");

		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(ws, map);

		repo.begin(indexProject);
		File jar = IO.getFile("testresources/release.jar");
		PutOptions po = new PutOptions();
		po.context = p1;
		PutResult put = repo.put(new FileInputStream(jar), po);

		File demoJar = IO.getFile("testresources/demo.jar");
		PutOptions indexPo = new PutOptions();
		indexPo.context = indexProject;
		put = repo.put(new FileInputStream(demoJar), indexPo);

		repo.end(indexProject);

		assertTrue(indexProject.check());
		assertTrue(IO.getFile(remote, "biz/aQute/bnd/demo/1.0.0/demo-1.0.0-index.xml")
			.isFile());
	}

	public void testNoIndexFile() throws Exception {<commit_msg>maven repo test: Simplify test which intermittently fails<commit_after>package aQute.bnd.repository.maven.provider;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.w3c.dom.Document;

import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;
import aQute.bnd.http.HttpClient;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.resource.ResourceUtils;
import aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability;
import aQute.bnd.service.RepositoryPlugin.PutOptions;
import aQute.bnd.service.RepositoryPlugin.PutResult;
import aQute.bnd.service.maven.PomOptions;
import aQute.bnd.service.progress.ProgressPlugin;
import aQute.bnd.service.progress.ProgressPlugin.Task;
import aQute.bnd.version.Version;
import aQute.http.testservers.HttpTestServer.Config;
import aQute.lib.io.IO;
import aQute.maven.api.Archive;
import aQute.maven.api.IPom;
import aQute.maven.api.MavenScope;
import aQute.maven.api.Revision;
import aQute.maven.provider.FakeNexus;
import aQute.maven.provider.MavenRepository;
import aQute.maven.provider.POM;
import junit.framework.TestCase;

/*
 * Create a remote and local repository with a lot of bad stuff. The repo contains:
 *
 */
public class MavenBndRepoTest extends TestCase {
	private static final Version		DTO_VERSION	= Version.parseVersion("1.0.0.201505202023");
	final static DocumentBuilderFactory	dbf			= DocumentBuilderFactory.newInstance();
	final static XPathFactory			xpf			= XPathFactory.newInstance();
	String								tmpName;
	File								tmp;
	File								local;
	File								remote;
	File								index;

	private MavenBndRepository			repo;
	private FakeNexus					fnx;
	private Processor					domain;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		tmpName = "generated/tmp/test/" + getName();
		tmp = IO.getFile(tmpName);
		IO.delete(tmp);
		local = IO.getFile(tmp, "local");
		remote = IO.getFile(tmp, "remote");
		index = IO.getFile(tmp, "index");
		remote.mkdirs();
		local.mkdirs();

		IO.copy(IO.getFile("testresources/mavenrepo"), remote);
		IO.copy(IO.getFile("testresources/mavenrepo/index.maven"), index);

		Config config = new Config();
		fnx = new FakeNexus(config, remote);
		fnx.start();
	}

	@Override
	protected void tearDown() throws Exception {
		domain.close();
		repo.close();
		fnx.close();
		super.tearDown();
	}

	public void testProgramRemoveFromIndex() throws Exception {
		config(null);
		assertNotNull(repo.get("org.osgi.dto", DTO_VERSION, null));
		String indexContent = IO.collect(index);
		assertThat(indexContent).contains("org.osgi.dto:1.0.0");

		Map<String, Runnable> actions = repo.actions("org.osgi.dto");
		assertNotNull(actions);
		actions.get("Delete All from Index")
			.run();
		assertNull(repo.get("org.osgi.dto", DTO_VERSION, null));
		assertThat(repo.list("org.osgi.dto")).isEmpty();
		indexContent = IO.collect(index);
		assertThat(indexContent).doesNotContain("org.osgi.dto:1.0.0");
	}

	public void testRevisionRemoveFromIndex() throws Exception {
		config(null);
		assertNotNull(repo.get("org.osgi.dto", DTO_VERSION, null));
		String indexContent = IO.collect(index);

		Map<String, Runnable> actions = repo.actions("org.osgi.dto", DTO_VERSION);
		assertNotNull(actions);
		actions.get("Delete from Index")
			.run();
		assertThat(repo.list("org.osgi.dto")).isEmpty();
		indexContent = IO.collect(index);
		assertThat(indexContent).doesNotContain("org.osgi.dto:1.0.0");
	}

	public void testAdd() throws Exception {
		config(null);
		assertThat(repo.list("org.osgi.service.log")).isEmpty();
		String indexContent = IO.collect(index);
		assertThat(indexContent).doesNotContain("org.osgi.service.log");
		repo.index.add(Archive.valueOf("org.osgi:org.osgi.service.log:1.3.0"));
		assertThat(repo.list("org.osgi.service.log")).isNotEmpty();
		indexContent = IO.collect(index);
		assertThat(indexContent).contains("org.osgi.service.log:1.3.0");
	}

	public void testUseSource() throws Exception {
		Map<String, String> config = new HashMap<>();
		config.put("source", "org.osgi:org.osgi.service.log:1.3.0, ; \torg.osgi:org.osgi.service.log:1.2.0");
		config(config);
		assertThat(repo.list("org.osgi.service.log")).isNotEmpty();
	}

	public void testTooltip() throws Exception {
		config(null);
		String tooltip = repo.tooltip();
		assertNotNull(tooltip);

		String title = repo.title("org.apache.commons.cli");
		assertEquals("org.apache.commons.cli", title);

		tooltip = repo.tooltip("org.apache.commons.cli", new Version("1.2"));
		assertNotNull(tooltip);
		System.out.println(tooltip);
		title = repo.title("org.apache.commons.cli", new Version("1.2"));
		assertEquals("1.2.0", title);
		tooltip = repo.tooltip("commons-cli:commons-cli", new Version("1.0"));
		assertNotNull(tooltip);
		System.out.println(tooltip);

		title = repo.title("commons-cli:commons-cli");
		assertEquals("commons-cli:commons-cli [!]", title);

		title = repo.title("commons-cli:commons-cli", new Version("1.0"));
		assertEquals("1.0.0 [Not a bundle]", title);

		title = repo.title("commons-cli:commons-cli", new Version("1.4.0.SNAPSHOT"));
		assertEquals("1.4.0.SNAPSHOT [Not found]", title);

	}

	public void testZipFileWithContents() throws Exception {
		IO.copy(IO.getFile("testresources/mavenrepo2"), remote);
		IO.copy(IO.getFile("testresources/mavenrepo2/index.maven"), index);
		Map<String, String> config = new HashMap<>();
		config.put("index", index.getAbsolutePath());
		config.put("multi", " zip , par, foo");
		config(config);
		String multi_version = "1.0.0";
		domain.setProperty("multi_version", multi_version);
		File file = repo.get("group:artifact:zip:", Version.parseVersion(multi_version), null);
		assertNotNull(file);
		assertTrue(file.isFile());

		File file2 = repo.get("name.njbartlett.eclipse.macbadge", new Version("1.0.0.201110100042"), null);
		assertNotNull(file2);

		File file3 = repo.get("group:artifact:jar:1003", Version.parseVersion(multi_version), null);
		assertNotNull(file3);
		assertTrue(file3.isFile());

		assertTrue(file2.equals(file3));

		repo.index.remove(Archive.valueOf("group:artifact:zip:" + multi_version));

		file2 = repo.get("name.njbartlett.eclipse.macbadge", new Version("1.0.0.201110100042"), null);
		assertNull(file2);
		file3 = repo.get("group:artifact:jar:1003", Version.parseVersion(multi_version), null);
		assertNull(file3);
	}

	public void testPut() throws Exception {

		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(map);
		File jar = IO.getFile("testresources/release.jar");
		PutResult put = repo.put(new FileInputStream(jar), null);

		assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.jar", 140212);
		File file = assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.pom", 1470);
		IPom pom = repo.storage.getPom(new FileInputStream(file));
		assertEquals("jar", pom.getPackaging());
		assertEquals(true, pom.hasValidGAV());
		assertEquals("biz.aQute.bnd:biz.aQute.bnd.maven:3.2.0", pom.getRevision()
			.toString());

		Document doc = dbf.newDocumentBuilder()
			.parse(file);
		XPath xp = xpf.newXPath();

		assertEquals("biz.aQute.bnd.maven", xp.evaluate("/project/artifactId", doc)
			.trim());
		assertEquals("3.2.0", xp.evaluate("/project/version", doc)
			.trim());
		assertEquals("biz.aQute.bnd", xp.evaluate("/project/groupId", doc)
			.trim());

		assertEquals("http://bnd.bndtools.org/", xp.evaluate("/project/url", doc)
			.trim());
		assertEquals("Bndtools", xp.evaluate("/project/organization/name", doc)
			.trim());
		assertEquals("http://bndtools.org/", xp.evaluate("/project/organization/url", doc)
			.trim());

		assertEquals("https://github.com/bndtools/bnd", xp.evaluate("/project/scm/url", doc)
			.trim());
		assertEquals("scm:git:https://github.com/bndtools/bnd.git", xp.evaluate("/project/scm/connection", doc)
			.trim());
		assertEquals("scm:git:git@github.com:bndtools/bnd.git", xp.evaluate("/project/scm/developerConnection", doc)
			.trim());

		// IO.copy(file, System.out);

		put = repo.put(new FileInputStream(jar), null);

		Requirement wc = ResourceUtils.createWildcardRequirement();
		Collection<Capability> caps = repo.findProviders(Collections.singleton(wc))
			.get(wc);
		Set<Resource> resources = ResourceUtils.getResources(caps);
		assertEquals(3, resources.size());
		IdentityCapability bc = ResourceUtils.getIdentityCapability(resources.iterator()
			.next());
		assertEquals("biz.aQute.bnd.maven", bc.osgi_identity());
	}

	public void testPutMavenReleaseSources() throws Exception {
		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(map);
		try (Processor context = new Processor()) {
			context.setProperty("-maven-release", "sources;path=\"testresources/src\",javadoc;packages=all");
			File jar = IO.getFile("testresources/release.jar");
			PutOptions options = new PutOptions();
			options.context = context;
			PutResult put = repo.put(new FileInputStream(jar), options);

			assertIsFile(remote, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.jar", 140212);
			File sources = assertIsFile(remote,
				"biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0-sources.jar", 0);
			try (Jar sourcesJar = new Jar(sources)) {
				assertThat(sourcesJar.exists("X.java")).isTrue();
			}
			File javadoc = assertIsFile(remote,
				"biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0-javadoc.jar", 0);
			try (Jar javadocJar = new Jar(javadoc)) {
				assertThat(javadocJar.exists("X.html")).isTrue();
			}
		}
	}

	public void testPomGenerate() throws Exception {
		config(null);
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		PomOptions po = new PomOptions();
		po.gav = "test:test:1.0";
		po.parent = null;
		po.dependencyManagement = false;

		repo.toPom(bout, po);
		ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
		POM pom = new POM((MavenRepository) repo.storage, bin);

		assertEquals(Revision.valueOf("test:test:1.0"), pom.getRevision());
		assertEquals(4, pom.getDependencies(EnumSet.of(MavenScope.runtime), false)
			.size());
		System.out.println(new String(bout.toByteArray(), StandardCharsets.UTF_8));
	}

	public void testPutLocalTwiceNoSnapshot() throws Exception {
		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(map);
		Requirement wc = ResourceUtils.createWildcardRequirement();
		Collection<Capability> caps = repo.findProviders(Collections.singleton(wc))
			.get(wc);
		Set<Resource> resources = ResourceUtils.getResources(caps);
		int size = resources.size();
		assertThat(resources)
			.extracting(ResourceUtils::getIdentityCapability)
			.filteredOn(Objects::nonNull)
			.extracting(IdentityCapability::osgi_identity)
			.doesNotContain("biz.aQute.bnd.maven");

		File jar = IO.getFile("testresources/release.jar");
		PutResult put = repo.put(new FileInputStream(jar), null);

		assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.jar", 140212);
		assertIsFile(local, "biz/aQute/bnd/biz.aQute.bnd.maven/3.2.0/biz.aQute.bnd.maven-3.2.0.pom", 1470);
		put = repo.put(new FileInputStream(jar), null);

		caps = repo.findProviders(Collections.singleton(wc))
			.get(wc);
		resources = ResourceUtils.getResources(caps);
		assertThat(resources)
			.extracting(ResourceUtils::getIdentityCapability)
			.filteredOn(Objects::nonNull)
			.extracting(IdentityCapability::osgi_identity)
			.contains("biz.aQute.bnd.maven")
			.hasSize(size + 1);
	}

	public void testPutReleaseAndThenIndex() throws Exception {
		Workspace ws = new Workspace(IO.getFile("testdata/releasews"));
		Project p1 = ws.getProject("p1");
		Project indexProject = ws.getProject("index");

		Map<String, String> map = new HashMap<>();
		map.put("releaseUrl", remote.toURI()
			.toString());
		config(ws, map);

		repo.begin(indexProject);
		File jar = IO.getFile("testresources/release.jar");
		PutOptions po = new PutOptions();
		po.context = p1;
		PutResult put = repo.put(new FileInputStream(jar), po);

		File demoJar = IO.getFile("testresources/demo.jar");
		PutOptions indexPo = new PutOptions();
		indexPo.context = indexProject;
		put = repo.put(new FileInputStream(demoJar), indexPo);

		repo.end(indexProject);

		assertTrue(indexProject.check());
		assertTrue(IO.getFile(remote, "biz/aQute/bnd/demo/1.0.0/demo-1.0.0-index.xml")
			.isFile());
	}

	public void testNoIndexFile() throws Exception {
{'fraction_non_alphanumeric': 0.16070954781488078, 'fraction_numerical': 0.015426600462418982, 'mean_word_length': 4.992277992277992, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '379892', 'n_tokens_mistral': 10098, 'n_tokens_neox': 9078, 'n_words': 1471}