/** Returns the lines in the file. */ ImmutableList<String> linesInFile() { if (lines == null) { try { lines = JavaFileObjects.asByteSource(file).asCharSource(UTF_8).readLines(); } catch (IOException e) { throw new UncheckedIOException(e); } } return lines; }
/** Returns the lines in the file. */ ImmutableList<String> linesInFile() { if (lines == null) { try { lines = JavaFileObjects.asByteSource(file).asCharSource(UTF_8).readLines(); } catch (IOException e) { throw new UncheckedIOException(e); } } return lines; }
/** Returns a description of the contents of a given generated file. */ private String describeGeneratedFile(JavaFileObject generatedFile) { try { StringBuilder entry = new StringBuilder("\n").append(generatedFile.getName()).append(":\n"); if (generatedFile.getKind().equals(CLASS)) { entry.append( String.format( " [generated class file (%d bytes)]", asByteSource(generatedFile).size())); } else { entry.append(generatedFile.getCharContent(true)); } return entry.append('\n').toString(); } catch (IOException e) { throw new IllegalStateException( "Couldn't read from JavaFileObject when it was already in memory.", e); } }
boolean wasGenerated(JavaFileObject expected) { ByteSource expectedByteSource = JavaFileObjects.asByteSource(expected); for (JavaFileObject generated : compilation.generatedFiles()) { try { if (generated.getKind().equals(expected.getKind()) && expectedByteSource.contentEquals(JavaFileObjects.asByteSource(generated))) { return true; } } catch (IOException e) { throw new RuntimeException(e); } } return false; }
/** * If {@code other} is a {@link JavaFileObject}, tests that their contents are equal. Otherwise * uses {@link Object#equals(Object)}. */ @Override public void isEqualTo(@Nullable Object other) { if (!(other instanceof JavaFileObject)) { super.isEqualTo(other); } JavaFileObject otherFile = (JavaFileObject) other; try { if (!asByteSource(actual()).contentEquals(asByteSource(otherFile))) { fail("is equal to", other); } } catch (IOException e) { throw new RuntimeException(e); } }
/** Returns a description of the contents of a given generated file. */ private String describeGeneratedFile(JavaFileObject generatedFile) { try { StringBuilder entry = new StringBuilder("\n").append(generatedFile.getName()).append(":\n"); if (generatedFile.getKind().equals(CLASS)) { entry.append( String.format( " [generated class file (%d bytes)]", asByteSource(generatedFile).size())); } else { entry.append(generatedFile.getCharContent(true)); } return entry.append('\n').toString(); } catch (IOException e) { throw new IllegalStateException( "Couldn't read from JavaFileObject when it was already in memory.", e); } }
/** * If {@code other} is a {@link JavaFileObject}, tests that their contents are equal. Otherwise * uses {@link Object#equals(Object)}. */ @Override public void isEqualTo(@Nullable Object other) { if (!(other instanceof JavaFileObject)) { super.isEqualTo(other); } JavaFileObject otherFile = (JavaFileObject) other; try { if (!asByteSource(actual()).contentEquals(asByteSource(otherFile))) { fail("is equal to", other); } } catch (IOException e) { throw new RuntimeException(e); } }
/** Asserts that the actual file's contents are equal to {@code expected}. */ public void hasContents(ByteSource expected) { try { if (!asByteSource(actual()).contentEquals(expected)) { fail("has contents", expected); } } catch (IOException e) { throw new RuntimeException(e); } }
/** Asserts that the actual file's contents are equal to {@code expected}. */ public void hasContents(ByteSource expected) { try { if (!asByteSource(actual()).contentEquals(expected)) { fail("has contents", expected); } } catch (IOException e) { throw new RuntimeException(e); } }
boolean wasGenerated(JavaFileObject expected) { ByteSource expectedByteSource = JavaFileObjects.asByteSource(expected); for (JavaFileObject generated : compilation.generatedFiles()) { try { if (generated.getKind().equals(expected.getKind()) && expectedByteSource.contentEquals(JavaFileObjects.asByteSource(generated))) { return true; } } catch (IOException e) { throw new RuntimeException(e); } } return false; }
/** * Returns a {@link StringSubject} that makes assertions about the contents of the actual file as * a string. */ public StringSubject contentsAsString(Charset charset) { try { return check() .that(JavaFileObjects.asByteSource(actual()).asCharSource(charset).read()) .named("the contents of " + actualAsString()); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Returns a {@link StringSubject} that makes assertions about the contents of the actual file as * a string. */ public StringSubject contentsAsString(Charset charset) { try { return check() .that(JavaFileObjects.asByteSource(actual()).asCharSource(charset).read()) .named("the contents of " + actualAsString()); } catch (IOException e) { throw new RuntimeException(e); } }