package org.graalvm.component.installer.commands;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import org.graalvm.component.installer.CommandTestBase;
import org.graalvm.component.installer.Commands;
import org.graalvm.component.installer.CommonConstants;
import org.graalvm.component.installer.model.ComponentInfo;
import org.graalvm.component.installer.persist.ProxyResource;
import org.graalvm.component.installer.persist.test.Handler;
import org.graalvm.component.installer.remote.GraalEditionList;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
public class AvailableTest extends CommandTestBase {
private static final String GRAALVM_CE_202_CATALOG = "test://www.graalvm.org/graalvm-20.2-with-updates.properties";
private static final String GRAALVM_EE_202_CATALOG = "test://www.graalvm.org/graalvmee-20.2-with-updates.properties";
@Rule public final ProxyResource proxyResource = new ProxyResource();
private void loadReleaseFile(String name) throws IOException {
try (InputStream stm = getClass().getResourceAsStream("data/" + name)) {
Properties props = new Properties();
props.load(stm);
props.stringPropertyNames().forEach((s) -> {
String val = props.getProperty(s);
if (val.startsWith("\"") && val.endsWith("\"")) {
val = val.substring(1, val.length() - 1);
}
this.storage.graalInfo.put(s.toLowerCase(Locale.ENGLISH), val);
});
}
}
private GraalEditionList editionList;
private AvailableCommand cmd;
void setupReleaseAndCatalog() throws Exception {
loadReleaseFile("release21ceWithEE.properties");
Path f = dataFile("data/graalvm-20.2-with-updates.properties");
Handler.bind(GRAALVM_CE_202_CATALOG, f.toUri().toURL());
Path f2 = dataFile("data/graalvmee-20.2-with-updates.properties");
Handler.bind(GRAALVM_EE_202_CATALOG, f2.toUri().toURL());
}
private void initRemoteStorage() {
GraalEditionList list = new GraalEditionList(this, this, getLocalRegistry());
this.registry = list.createComponentCatalog(this);
this.editionList = list;
}
void setupGraalVM202() throws Exception {
setupReleaseAndCatalog();
initRemoteStorage();
cmd = new AvailableCommand();
cmd.init(this, this.withBundle(AvailableCommand.class));
cmd.execute();
}
@Override
public CatalogFactory getCatalogFactory() {
return editionList;
}
@Test
public void testDefaultListWithoutCorePackage() throws Exception {
setupGraalVM202();
List<ComponentInfo> infos = cmd.getComponents();
assertFalse(
infos.stream().filter(i -> CommonConstants.GRAALVM_CORE_PREFIX.equals(i.getId())).findAny().isPresent());
}
@Test
public void testDefaultListOnlyCompatibleVersions() throws Exception {
setupGraalVM202();
List<ComponentInfo> infos = cmd.getComponents();
assertFalse(
infos.stream().filter(i -> !i.getVersion().displayString().contains("20.2.0")).findAny().isPresent());
}
@Test
public void testListCompatibleVersionsPlusCore() throws Exception {
options.put(Commands.OPTION_SHOW_CORE, "");
setupGraalVM202();
List<ComponentInfo> infos = cmd.getComponents();
assertFalse(
infos.stream().filter(i -> !i.getVersion().displayString().contains("20.2.0")).findAny().isPresent());
assertTrue(
infos.stream().filter(i -> CommonConstants.GRAALVM_CORE_PREFIX.equals(i.getId())).findAny().isPresent());
}
@Test
public void testListUpdates() throws Exception {
options.put(Commands.OPTION_SHOW_UPDATES, "");
setupGraalVM202();
List<ComponentInfo> infos = cmd.getComponents();
assertTrue(
infos.stream().filter(i -> !i.getVersion().displayString().contains("20.2.0")).findAny().isPresent());
assertTrue(
infos.stream().filter(i -> CommonConstants.GRAALVM_CORE_PREFIX.equals(i.getId())).findAny().isPresent());
}
private static ComponentInfo findComponent(List<ComponentInfo> list, String id) {
return list.stream().filter(i -> id.equals(i.getId())).findAny().orElse(null);
}
@Test
public void testListEECurrentVersion() throws Exception {
options.put(Commands.OPTION_USE_EDITION, "ee");
setupGraalVM202();
List<ComponentInfo> infos = cmd.getComponents();
assertNull(findComponent(infos, "org.graalvm.python"));
assertNull(findComponent(infos, "org.graalvm.r"));
assertNotNull(findComponent(infos, "org.graalvm.ruby"));
assertNotNull(findComponent(infos, "org.graalvm"));
}
@Test
public void testListEEUpdates() throws Exception {
options.put(Commands.OPTION_SHOW_UPDATES, "");
options.put(Commands.OPTION_USE_EDITION, "ee");
setupGraalVM202();
List<ComponentInfo> infos = cmd.getComponents();
assertNotNull(findComponent(infos, "org.graalvm.python"));
assertNotNull(findComponent(infos, "org.graalvm.R"));
assertNotNull(findComponent(infos, "org.graalvm.ruby"));
assertNotNull(findComponent(infos, "org.graalvm"));
}
@Test
public void testListAll() throws Exception {
options.put(Commands.OPTION_ALL, "");
setupGraalVM202();
List<ComponentInfo> infos = cmd.getComponents();
assertTrue(infos.size() > 8);
}
}