Skip to content

Commit

Permalink
[GR-28793] JSON output for Graal Updater.
Browse files Browse the repository at this point in the history
PullRequest: graal/11908
  • Loading branch information
Ondrej-Douda committed Jun 16, 2022
2 parents a569fd3 + 4128980 commit d0f332c
Show file tree
Hide file tree
Showing 17 changed files with 469 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.junit.AfterClass;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -189,7 +190,6 @@ public void testErrorMessageWithException() throws Exception {
env.setErr(new PrintStream(errBuffer));
env.error("ERROR_UserInput", new ClassCastException(), "Foobar");
// Windows compat: CRLF -> LF conversion
// Windows compat: CRLF -> LF conversion
String all = readLines(errBuffer.toByteArray());
String[] lines = all.split("\n");
assertEquals(B1.getString("ERROR_UserInput").replace("{0}", "Foobar"), lines[0]);
Expand All @@ -207,4 +207,35 @@ public void testFailureOperation() throws Exception {
assertNotSame(t, t.getCause());
assertEquals("Foo", t.getCause().getLocalizedMessage());
}

@Test
public void testOutput() throws Exception {
setupEmptyEnv();

env.setOut(new PrintStream(outBuffer));
env.output("ERROR_MissingCommand");
// Windows compat: CRLF -> LF conversion
String s = readLines(outBuffer.toByteArray());
assertEquals(B1.getString("ERROR_MissingCommand") + "\n", s);
}

@Test
public void testSilentOutput() throws Exception {
setupEmptyEnv();
assertFalse(env.setSilent(true));

// output error even when silent
env.setErr(new PrintStream(errBuffer));
env.error("ERROR_UserInput", new ClassCastException(), "Foobar");
// Windows compat: CRLF -> LF conversion
String s = readLines(errBuffer.toByteArray());
assertEquals(B1.getString("ERROR_UserInput").replace("{0}", "Foobar") + "\n", s);

// don't output normal output when silent
env.setOut(new PrintStream(outBuffer));
env.output("ERROR_MissingCommand");
// Windows compat: CRLF -> LF conversion
s = readLines(outBuffer.toByteArray());
assertEquals("", s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,44 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.component.installer;

package org.graalvm.component.installer.gds.rest;

import org.graalvm.component.installer.Feedback;
import static org.junit.Assert.assertEquals;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.graalvm.component.installer.MemoryFeedback.Memory;

/**
*
* @author odouda
*/
final class MemoryFeedback implements Feedback {
enum Case {
public final class MemoryFeedback implements Feedback, Iterable<Memory> {

@Override
public Iterator<Memory> iterator() {
return new Iterator<>() {
int i = 0;

@Override
public boolean hasNext() {
return i < mem.size();
}

@Override
public Memory next() {
return mem.get(i++);
}
};
}

public enum Case {
ERR("Error"),
FLR("Failure"),
MSG("Message"),
Expand All @@ -63,19 +81,23 @@ public String toString() {

final List<Memory> mem = new ArrayList<>();
boolean verb = true;
boolean silent = false;
Function<Boolean, String> lineAccept = null;

static final class Memory {
final Case cas;
final String key;
final Object[] params;
final Throwable t;
public final class Memory {

public final boolean silent;
public final Case cas;
public final String key;
public final Object[] params;
public final Throwable t;

Memory(final Case cas, final String key, final Object[] params, Throwable t) {
this.key = key;
this.params = params;
this.cas = cas;
this.t = t;
this.silent = MemoryFeedback.this.silent;
}

Memory(final Case cas, final String key, final Object[] params) {
Expand All @@ -88,10 +110,18 @@ static final class Memory {

@Override
public String toString() {
return cas + ": " + key + ": " + Arrays.toString(params) + (t == null ? "" : ": " + t.getClass());
return cas + ": " + key + ": " + Arrays.toString(params) + (t == null ? "" : ": " + t.getClass()) + "; silent=" + this.silent;
}
}

public boolean isEmpty() {
return mem.isEmpty();
}

public int size() {
return mem.size();
}

private static void assEq(Object exp, Object obj, Supplier<String> msg) {
try {
assertEquals(exp, obj);
Expand Down Expand Up @@ -183,9 +213,7 @@ public boolean verboseOutput(String bundleKey, Object... params) {

@Override
public <T> Feedback withBundle(Class<T> clazz) {
throw new UnsupportedOperationException("Not supported yet."); // To change body of
// generated methods, choose
// Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
Expand Down Expand Up @@ -222,22 +250,28 @@ public String acceptLine(boolean autoYes) {

@Override
public char[] acceptPassword() {
throw new UnsupportedOperationException("Not supported yet."); // To change body of
// generated methods, choose
// Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void addLocalFileCache(URL location, Path local) {
throw new UnsupportedOperationException("Not supported yet."); // To change body of
// generated methods, choose
// Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Path getLocalCache(URL location) {
throw new UnsupportedOperationException("Not supported yet."); // To change body of
// generated methods, choose
// Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean isSilent() {
return this.silent;
}

@Override
public boolean setSilent(boolean silent) {
boolean wasSilent = this.silent;
this.silent = silent;
return wasSilent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public Enumeration<String> getKeys() {
public TestBase() {
}

@Override
public boolean isSilent() {
return feedbackDelegate == null ? false : feedbackDelegate.isSilent();
}

@Override
public boolean setSilent(boolean silent) {
return feedbackDelegate == null ? false : feedbackDelegate.setSilent(silent);
}

static class ClassTempFolder extends TemporaryFolder {
ThreadLocal<File> root = new ThreadLocal<>();

Expand Down Expand Up @@ -512,10 +522,21 @@ public Path getLocalCache(URL location) {
public boolean isNonInteractive() {
return TestBase.this.isNonInteractive();
}

@Override
public boolean isSilent() {
return TestBase.this.isSilent();
}

@Override
public boolean setSilent(boolean silent) {
return TestBase.this.setSilent(silent);
}
}

public class FeedbackAdapter implements Feedback {
private ResourceBundle currentBundle;
private boolean silent;

@Override
public boolean verbatimOut(String msg, boolean beVerbose) {
Expand Down Expand Up @@ -620,6 +641,18 @@ public Path getLocalCache(URL location) {
public boolean isNonInteractive() {
return TestBase.this.isNonInteractive();
}

@Override
public boolean isSilent() {
return silent;
}

@Override
public boolean setSilent(boolean silent) {
boolean wasSilent = this.silent;
this.silent = silent;
return wasSilent;
}
}

@Override
Expand Down

0 comments on commit d0f332c

Please sign in to comment.