package org.graalvm.component.installer.commands;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.graalvm.component.installer.Archive;
import org.graalvm.component.installer.BundleConstants;
import org.graalvm.component.installer.CommandTestBase;
import org.graalvm.component.installer.ComponentParam;
import org.graalvm.component.installer.DependencyException;
import org.graalvm.component.installer.FailedOperationException;
import org.graalvm.component.installer.Version;
import org.graalvm.component.installer.model.CatalogContents;
import org.graalvm.component.installer.model.ComponentInfo;
import org.graalvm.component.installer.persist.MetadataLoader;
import org.graalvm.component.installer.persist.ProxyResource;
import org.graalvm.component.installer.remote.CatalogIterable;
import org.graalvm.component.installer.remote.RemoteCatalogDownloader;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public class InstallVersionsTest extends CommandTestBase {
@Rule public final ProxyResource proxyResource = new ProxyResource();
private InstallCommand cmd;
private List<ComponentInfo> installInfos = new ArrayList<>();
private List<ComponentParam> installParams = new ArrayList<>();
@Before
@Override
public void setUp() throws Exception {
super.setUp();
cmd = new InstallCommand() {
@Override
Installer createInstaller(ComponentParam p, MetadataLoader ldr) throws IOException {
installInfos.add(ldr.getComponentInfo());
installParams.add(p);
Installer override = overrideCreateInstaller(p, ldr);
if (override == null) {
return super.createInstaller(p, ldr);
} else {
configureInstaller(override);
return override;
}
}
};
cmd.init(this, withBundle(InstallCommand.class));
}
@SuppressWarnings("unused")
protected Installer overrideCreateInstaller(ComponentParam p, MetadataLoader ldr) throws IOException {
ComponentInfo partialInfo;
partialInfo = ldr.getComponentInfo();
ldr.loadPaths();
Archive a = null;
Installer inst = new Installer(this, getFileOperations(), partialInfo, getLocalRegistry(),
getRegistry(), a);
inst.setPermissions(ldr.loadPermissions());
inst.setSymlinks(ldr.loadSymlinks());
return inst;
}
private Version initVersion(String s) throws IOException {
Version v = Version.fromString(s);
storage.graalInfo.put(BundleConstants.GRAAL_VERSION, s);
Path catalogPath = dataFile("../repo/catalog.properties");
RemoteCatalogDownloader downloader = new RemoteCatalogDownloader(
this,
this,
catalogPath.toUri().toURL());
registry = new CatalogContents(this, downloader.getStorage(), localRegistry);
paramIterable = new CatalogIterable(this, this);
return v;
}
@Test
public void testMissingComponent() throws Exception {
initVersion("1.0.1.0");
textParams.add("python");
cmd.prepareInstallation();
assertEquals(1, installInfos.size());
ComponentInfo ci = installInfos.get(0);
assertEquals("1.0.1.0", ci.getVersion().toString());
}
@Test
public void testInstallNewUpdatedComponent() throws Exception {
initVersion("1.0.1.0");
textParams.add("ruby");
cmd.prepareInstallation();
assertEquals(1, installInfos.size());
ComponentInfo ci = installInfos.get(0);
assertEquals("1.0.1.1", ci.getVersion().toString());
}
@Test
public void testRefuseCoreUpgrade() throws Exception {
initVersion("1.0.0.0");
exception.expect(FailedOperationException.class);
exception.expectMessage("REMOTE_UpgradeGraalVMCore");
textParams.add("r");
cmd.prepareInstallation();
}
@Test
public void testUnknownComponent() throws Exception {
initVersion("1.0.0.0");
exception.expect(FailedOperationException.class);
exception.expectMessage("REMOTE_UnknownComponentId");
textParams.add("x");
cmd.prepareInstallation();
}
@Test
public void testRefusedDowngrade() throws Exception {
initVersion("1.1.0.0");
exception.expect(FailedOperationException.class);
exception.expectMessage("REMOTE_UnknownComponentId");
textParams.add("ruby");
cmd.prepareInstallation();
}
@Test
public void testDowngradeToSpecificVersion() throws Exception {
initVersion("1.1.0.0");
exception.expect(FailedOperationException.class);
exception.expectMessage("REMOTE_UnknownComponentId");
textParams.add("ruby=1.0.0.0");
cmd.prepareInstallation();
}
@Test
public void testRefuseExplicitUpgrade() throws Exception {
initVersion("1.0.1.0");
exception.expect(DependencyException.Mismatch.class);
exception.expectMessage("VERIFY_UpdateGraalVM");
textParams.add("python=1.1.0.0");
cmd.prepareInstallation();
}
@Test
public void testFailOneOfComponents() throws Exception {
initVersion("1.0.1.0");
textParams.add("ruby");
textParams.add("python=1.1.0.0");
exception.expect(DependencyException.Mismatch.class);
exception.expectMessage("VERIFY_UpdateGraalVM");
cmd.prepareInstallation();
}
}