diff options
Diffstat (limited to 'value/src/test/java/com/google/auto/value/processor/TemplateVarsTest.java')
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/TemplateVarsTest.java | 146 |
1 files changed, 0 insertions, 146 deletions
diff --git a/value/src/test/java/com/google/auto/value/processor/TemplateVarsTest.java b/value/src/test/java/com/google/auto/value/processor/TemplateVarsTest.java index bca7bcab..621a4122 100644 --- a/value/src/test/java/com/google/auto/value/processor/TemplateVarsTest.java +++ b/value/src/test/java/com/google/auto/value/processor/TemplateVarsTest.java @@ -15,29 +15,15 @@ */ package com.google.auto.value.processor; -import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH; -import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR; import static com.google.common.truth.Truth.assertThat; -import static java.util.logging.Level.WARNING; import static org.junit.Assert.fail; -import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; -import com.google.common.reflect.Reflection; import com.google.escapevelocity.Template; -import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.Reader; import java.io.StringReader; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; import java.util.List; -import java.util.Set; -import java.util.TreeSet; -import java.util.concurrent.Callable; -import java.util.logging.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -172,136 +158,4 @@ public class TemplateVarsTest { } catch (IllegalArgumentException expected) { } } - - @Test - public void testBrokenInputStream_IOException() throws Exception { - doTestBrokenInputStream(new IOException("BrokenInputStream")); - } - - @Test - public void testBrokenInputStream_NullPointerException() throws Exception { - doTestBrokenInputStream(new NullPointerException("BrokenInputStream")); - } - - @Test - public void testBrokenInputStream_IllegalStateException() throws Exception { - doTestBrokenInputStream(new IllegalStateException("BrokenInputStream")); - } - - // This is a complicated test that tries to simulates the failures that are worked around in - // Template.parsedTemplateForResource. Those failures means that the InputStream returned by - // ClassLoader.getResourceAsStream sometimes throws IOException or NullPointerException or - // IllegalStateException while it is being read. To simulate that, we make a second ClassLoader - // with the same configuration as the one that runs this test, and we override getResourceAsStream - // so that it wraps the returned InputStream in a BrokenInputStream, which throws an exception - // after a certain number of characters. We check that that exception was indeed seen, and that - // we did indeed try to read the resource we're interested in, and that we succeeded in loading a - // Template nevertheless. - private void doTestBrokenInputStream(Exception exception) throws Exception { - URLClassLoader shadowLoader = new ShadowLoader(getClass().getClassLoader(), exception); - Runnable brokenInputStreamTest = - (Runnable) - shadowLoader - .loadClass(BrokenInputStreamTest.class.getName()) - .getConstructor() - .newInstance(); - brokenInputStreamTest.run(); - } - - private static class ShadowLoader extends URLClassLoader implements Callable<Set<String>> { - - private static final Logger logger = Logger.getLogger(ShadowLoader.class.getName()); - - private final Exception exception; - private final Set<String> result = new TreeSet<String>(); - - ShadowLoader(ClassLoader original, Exception exception) { - super(getClassPathUrls(original), original.getParent()); - this.exception = exception; - } - - private static URL[] getClassPathUrls(ClassLoader original) { - return original instanceof URLClassLoader - ? ((URLClassLoader) original).getURLs() - : parseJavaClassPath(); - } - - /** - * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain - * System#getProperty system property}. - */ - // TODO(b/65488446): Use a new public API. - private static URL[] parseJavaClassPath() { - ImmutableList.Builder<URL> urls = ImmutableList.builder(); - for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) { - try { - try { - urls.add(new File(entry).toURI().toURL()); - } catch (SecurityException e) { // File.toURI checks to see if the file is a directory - urls.add(new URL("file", null, new File(entry).getAbsolutePath())); - } - } catch (MalformedURLException e) { - logger.log(WARNING, "malformed classpath entry: " + entry, e); - } - } - return urls.build().toArray(new URL[0]); - } - - @Override - public Set<String> call() throws Exception { - return result; - } - - @Override - public InputStream getResourceAsStream(String resource) { - // Make sure this is actually the resource we are expecting. If we're using JaCoCo or the - // like, we might end up reading some other resource, and we don't want to break that. - if (resource.startsWith("com/google/auto")) { - result.add(resource); - return new BrokenInputStream(super.getResourceAsStream(resource)); - } else { - return super.getResourceAsStream(resource); - } - } - - private class BrokenInputStream extends InputStream { - private final InputStream original; - private int count = 0; - - BrokenInputStream(InputStream original) { - this.original = original; - } - - @Override - public int read() throws IOException { - if (++count > 10) { - result.add("threw"); - if (exception instanceof IOException) { - throw (IOException) exception; - } - throw (RuntimeException) exception; - } - return original.read(); - } - } - } - - public static class BrokenInputStreamTest implements Runnable { - @Override - public void run() { - Template template = TemplateVars.parsedTemplateForResource("autovalue.vm"); - assertThat(template).isNotNull(); - String resourceName = - Reflection.getPackageName(getClass()).replace('.', '/') + "/autovalue.vm"; - @SuppressWarnings("unchecked") - Callable<Set<String>> myLoader = (Callable<Set<String>>) getClass().getClassLoader(); - try { - Set<String> result = myLoader.call(); - assertThat(result).contains(resourceName); - assertThat(result).contains("threw"); - } catch (Exception e) { - throw new AssertionError(e); - } - } - } } |