package org.graalvm.component.installer.model;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.graalvm.component.installer.BundleConstants;
import org.graalvm.component.installer.CommandTestBase;
import org.graalvm.component.installer.Version;
import org.graalvm.component.installer.remote.RemotePropertiesStorage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class MergedCatalogContentsTest extends CommandTestBase {
public MergedCatalogContentsTest() {
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
storage.graalInfo.put(BundleConstants.GRAAL_VERSION, "1.0.0.0");
}
RemotePropertiesStorage remstorage;
CatalogContents coll;
private Version initVersion(String s) throws IOException {
Version v = Version.fromString(s);
storage.graalInfo.put(BundleConstants.GRAAL_VERSION, s);
Path catalogPath = dataFile("catalog-deps.properties");
Properties props = new Properties();
try (InputStream is = Files.newInputStream(catalogPath)) {
props.load(is);
}
remstorage = new RemotePropertiesStorage(this,
getLocalRegistry(),
props, "linux_amd64", v, catalogPath.toUri().toURL());
coll = new CatalogContents(this, remstorage, getLocalRegistry());
return v;
}
@Test
public void testFindDependenciesOneLevel() throws Exception {
initVersion("19.3");
ComponentInfo ci = coll.findComponent("org.graalvm.r", null);
assertNotNull(ci);
Set<ComponentInfo> deps = new HashSet<>();
Set<String> errors = coll.findDependencies(ci, false, null, deps);
assertNull(errors);
assertEquals(1, deps.size());
assertEquals("org.graalvm.llvm-toolchain", deps.iterator().next().getId());
ci = coll.findComponent("org.graalvm.ruby", null);
assertNotNull(ci);
deps = new HashSet<>();
errors = coll.findDependencies(ci, false, null, deps);
assertNull(errors);
assertEquals(1, deps.size());
assertEquals("org.graalvm.native-image", deps.iterator().next().getId());
}
@Test
public void testFindDependenciesOneLevelInstalled() throws Exception {
initVersion("19.3");
ComponentInfo ci = coll.findComponent("org.graalvm.r", null);
ComponentInfo ll = coll.findComponent("org.graalvm.llvm-toolchain", null);
assertNotNull(ci);
assertNotNull(ll);
ll.setInfoPath("");
storage.installed.add(ll);
Set<ComponentInfo> deps = new HashSet<>();
Set<String> errors = coll.findDependencies(ci, false, Boolean.TRUE, deps);
assertNull(errors);
assertEquals(1, deps.size());
assertEquals("org.graalvm.llvm-toolchain", deps.iterator().next().getId());
ci = coll.findComponent("org.graalvm.ruby", null);
assertNotNull(ci);
deps = new HashSet<>();
errors = coll.findDependencies(ci, false, Boolean.TRUE, deps);
assertNotNull(errors);
assertTrue(deps.isEmpty());
assertEquals(1, errors.size());
assertEquals("org.graalvm.native-image", errors.iterator().next());
}
@Test
public void testFindDependenciesRecursive() throws Exception {
initVersion("19.3");
ComponentInfo ci = coll.findComponent("org.graalvm.r", null);
assertNotNull(ci);
Set<ComponentInfo> deps = new HashSet<>();
Set<String> errors = coll.findDependencies(ci, true, null, deps);
assertNull(errors);
assertEquals(1, deps.size());
assertEquals("org.graalvm.llvm-toolchain", deps.iterator().next().getId());
ci = coll.findComponent("org.graalvm.ruby", null);
assertNotNull(ci);
deps = new HashSet<>();
errors = coll.findDependencies(ci, true, null, deps);
assertNull(errors);
assertEquals(2, deps.size());
ComponentInfo ni = coll.findComponent("org.graalvm.native-image", null);
ComponentInfo ll = coll.findComponent("org.graalvm.llvm-toolchain", null);
assertTrue(deps.contains(ni));
assertTrue(deps.contains(ll));
}
@Test
public void testFindDependenciesRecursiveInstalled() throws Exception {
initVersion("19.3");
ComponentInfo ci = coll.findComponent("org.graalvm.ruby", null);
ComponentInfo ni = coll.findComponent("org.graalvm.native-image", null);
assertNotNull(ci);
assertNotNull(ni);
ni.setInfoPath("");
storage.installed.add(ni);
Set<ComponentInfo> deps = new HashSet<>();
Set<String> errors = coll.findDependencies(ci, true, Boolean.TRUE, deps);
assertNotNull(errors);
assertEquals(1, deps.size());
assertEquals(1, errors.size());
assertEquals("org.graalvm.native-image", deps.iterator().next().getId());
assertEquals("org.graalvm.llvm-toolchain", errors.iterator().next());
}
@Test
public void testFindDependenciesOnlyMissing() throws Exception {
initVersion("19.3");
ComponentInfo ci = coll.findComponent("org.graalvm.ruby", null);
ComponentInfo ni = coll.findComponent("org.graalvm.native-image", null);
assertNotNull(ci);
assertNotNull(ni);
ni.setInfoPath("");
storage.installed.add(ni);
Set<ComponentInfo> deps = new HashSet<>();
Set<String> errors = coll.findDependencies(ci, true, Boolean.FALSE, deps);
assertNull(errors);
assertEquals(1, deps.size());
assertEquals("org.graalvm.llvm-toolchain", deps.iterator().next().getId());
}
}