package org.graalvm.component.installer;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import org.junit.AfterClass;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class EnvironmentTest {
@Rule public ExpectedException exception = ExpectedException.none();
private static ResourceBundle B1;
private static Locale saveLocale;
private Environment env;
private Map<String, String> initOptions = new HashMap<>();
@BeforeClass
public static void setUp() {
saveLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
B1 = ResourceBundle.getBundle("org.graalvm.component.installer.Bundle", Locale.US);
}
@AfterClass
public static void tearDown() {
Locale.setDefault(saveLocale);
}
class FailInputStream extends FilterInputStream {
FailInputStream() {
super(System.in);
}
@Override
public int read() throws IOException {
Assert.fail("Unexpected read");
return super.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
Assert.fail("Unexpected read");
return super.read(b, off, len);
}
}
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
@Test
public void testNonInteractiveLineFails() {
setupEmptyEnv();
env.setNonInteractive(true);
env.setIn(new FailInputStream());
exception.expect(NonInteractiveException.class);
assertNull(env.acceptLine(false));
}
@Test
public void testNonInteractiveYesLineOK() {
setupEmptyEnv();
env.setNonInteractive(true);
env.setAutoYesEnabled(true);
env.setIn(new FailInputStream());
assertSame(Feedback.AUTO_YES, env.acceptLine(true));
}
@Test
public void testNonInteractiveNormalLineFails() {
setupEmptyEnv();
env.setNonInteractive(true);
env.setAutoYesEnabled(true);
env.setIn(new FailInputStream());
exception.expect(NonInteractiveException.class);
exception.expectMessage(B1.getString("ERROR_NoninteractiveInput"));
assertSame(Feedback.AUTO_YES, env.acceptLine(false));
}
@Test
public void testAutoConfirmSkipsInput() {
setupEmptyEnv();
env.setAutoYesEnabled(true);
env.setIn(new FailInputStream());
assertSame(Feedback.AUTO_YES, env.acceptLine(true));
}
@Test
public void testNonInteractivePasswordFails() {
setupEmptyEnv();
env.setNonInteractive(true);
env.setIn(new FailInputStream());
exception.expect(NonInteractiveException.class);
assertNull(env.acceptPassword());
}
void setupEmptyEnv() {
List<String> parameters = Arrays.asList("param");
env = new Environment("test", "org.graalvm.component.installer", parameters, initOptions);
}
private static String readLines(byte[] arr) throws IOException {
try (BufferedReader bfr = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(arr)))) {
return bfr.lines().map(l -> l + "\n").collect(Collectors.joining());
}
}
@Test
public void testErrorMessagePlain() throws Exception {
setupEmptyEnv();
env.setErr(new PrintStream(errBuffer));
env.error("ERROR_UserInput", new ClassCastException(), "Foobar");
String s = readLines(errBuffer.toByteArray());
assertEquals(B1.getString("ERROR_UserInput").replace("{0}", "Foobar") + "\n", s);
}
@Test
public void testErrorMessageWithException() throws Exception {
initOptions.put(Commands.OPTION_DEBUG, "");
setupEmptyEnv();
env.setErr(new PrintStream(errBuffer));
env.error("ERROR_UserInput", new ClassCastException(), "Foobar");
String all = readLines(errBuffer.toByteArray());
String[] lines = all.split("\n");
assertEquals(B1.getString("ERROR_UserInput").replace("{0}", "Foobar"), lines[0]);
assertTrue(lines[1].contains("ClassCastException"));
}
@Test
public void testFailureOperation() throws Exception {
setupEmptyEnv();
Throwable t = env.failure("URL_InvalidDownloadURL", new MalformedURLException("Foo"), "Bar", "Baz");
assertTrue(t instanceof FailedOperationException);
String s = MessageFormat.format(B1.getString("URL_InvalidDownloadURL"), "Bar", "Baz");
assertEquals(t.getLocalizedMessage(), s);
assertNotSame(t, t.getCause());
assertEquals("Foo", t.getCause().getLocalizedMessage());
}
}