/** * 读取文件到String. */ public static String toString(final File file) throws IOException { return com.google.common.io.Files.toString(file, Charsets.UTF_8); }
@Override protected String readString(URI uri, Charset charset) { try { return Files.toString(new File(uri), charset); } catch (IOException e) { throw Throwables.propagate(e); } }
public BenchmarkQuery(File file) throws IOException { requireNonNull(file, "file is null"); name = Files.getNameWithoutExtension(file.getName()); // file can have 2 sections separated by a line of equals signs String text = Files.toString(file, StandardCharsets.UTF_8); List<String> sections = SECTION_SPLITTER.splitToList(text); if (sections.size() == 2) { this.tags = ImmutableMap.copyOf(TAGS_SPLITTER.split(sections.get(0))); this.sql = sections.get(1); } else { // no tags this.tags = ImmutableMap.of(); this.sql = sections.get(0); } }
public void testToString() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertEquals(ASCII, Files.toString(asciiFile, Charsets.US_ASCII)); assertEquals(I18N, Files.toString(i18nFile, Charsets.UTF_8)); assertThat(Files.toString(i18nFile, Charsets.US_ASCII)).isNotEqualTo(I18N); }
public void testWriteString() throws IOException { File temp = createTempFile(); Files.write(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N, Files.toString(temp, Charsets.UTF_16LE)); }
public void testCopyIdenticalFiles() throws IOException { File temp1 = createTempFile(); Files.write(ASCII, temp1, Charsets.UTF_8); File temp2 = createTempFile(); Files.write(ASCII, temp2, Charsets.UTF_8); Files.copy(temp1, temp2); assertEquals(ASCII, Files.toString(temp1, Charsets.UTF_8)); }
public void testCopySameFile() throws IOException { File temp = createTempFile(); Files.write(ASCII, temp, Charsets.UTF_8); try { Files.copy(temp, temp); fail("Expected an IAE to be thrown but wasn't"); } catch (IllegalArgumentException expected) { } assertEquals(ASCII, Files.toString(temp, Charsets.UTF_8)); }
public void testAppendString() throws IOException { File temp = createTempFile(); Files.append(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N, Files.toString(temp, Charsets.UTF_16LE)); Files.append(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N + I18N, Files.toString(temp, Charsets.UTF_16LE)); Files.append(I18N, temp, Charsets.UTF_16LE); assertEquals(I18N + I18N + I18N, Files.toString(temp, Charsets.UTF_16LE)); }
public void testCopyFile() throws IOException { File i18nFile = getTestFile("i18n.txt"); File temp = createTempFile(); Files.copy(i18nFile, temp); assertEquals(I18N, Files.toString(temp, Charsets.UTF_8)); }
public void testCopyEqualFiles() throws IOException { File temp1 = createTempFile(); File temp2 = file(temp1.getPath()); assertEquals(temp1, temp2); Files.write(ASCII, temp1, Charsets.UTF_8); try { Files.copy(temp1, temp2); fail("Expected an IAE to be thrown but wasn't"); } catch (IllegalArgumentException expected) { } assertEquals(ASCII, Files.toString(temp1, Charsets.UTF_8)); }
@Test public void testWrite() throws IOException { when(options.get(Documentor.DOC_OUTPUT_PROPERTY_NAME)).thenReturn(FILE_PATH); Documentor documentor = new Documentor(processingEnv); documentor.write(getElements()); String actual = Files.toString(new File(FILE_PATH), Charsets.UTF_8); String expected = "airbnb://example.com/{foo}/bar\\n|#|\\nSample doc\\n|#|\\nDocClass\\" + "n|##|\\nairbnb://example.com/{foo}/bar\\n|#|\\n\\n|#|\\nDocClass#DocMethod\\n|##|\\n"; assertEquals(expected, actual); }
@Override public void run() throws IOException { assertThat(helper.postContent(remoteUrl("/proxy"), "proxy"), is("proxy")); assertThat(Files.toString(tempFile, Charset.defaultCharset()), containsString("proxy")); } });
@Test public void should_fire_event_with_post_url_template() throws IOException { runWithConfiguration("event.json"); File file = folder.newFile(); System.setOut(new PrintStream(new FileOutputStream(file))); assertThat(helper.get(remoteUrl("/post-event-with-template-url")), is("post_foo")); idle(IDLE, TimeUnit.MILLISECONDS); assertThat(Files.toString(file, Charset.defaultCharset()), containsString("0XCAFEBABE")); }
@Test public void should_fire_get_event() throws IOException { runWithConfiguration("event.json"); File file = folder.newFile(); System.setOut(new PrintStream(new FileOutputStream(file))); assertThat(helper.get(remoteUrl("/get_event")), is("get_foo")); idle(IDLE, TimeUnit.MILLISECONDS); assertThat(Files.toString(file, Charset.defaultCharset()), containsString("0XCAFEBABE")); }
@Test public void should_fire_get_event_with_template() throws IOException { runWithConfiguration("event.json"); File file = folder.newFile(); System.setOut(new PrintStream(new FileOutputStream(file))); assertThat(helper.get(remoteUrl("/get_event_template")), is("get_foo")); idle(IDLE, TimeUnit.MILLISECONDS); assertThat(Files.toString(file, Charset.defaultCharset()), containsString("0XCAFEBABE")); }
@Test public void should_fire_event_with_unit() throws IOException { runWithConfiguration("event.json"); File file = folder.newFile(); System.setOut(new PrintStream(new FileOutputStream(file))); assertThat(helper.get(remoteUrl("/event-with-unit")), is("post_foo")); idle(IDLE, TimeUnit.MILLISECONDS); assertThat(Files.toString(file, Charset.defaultCharset()), containsString("0XCAFEBABE")); }
@Test public void should_fire_event_with_post_json() throws IOException { runWithConfiguration("event.json"); File file = folder.newFile(); System.setOut(new PrintStream(new FileOutputStream(file))); assertThat(helper.get(remoteUrl("/event-with-json-post")), is("post_json_foo")); idle(IDLE, TimeUnit.MILLISECONDS); assertThat(Files.toString(file, Charset.defaultCharset()), containsString("0XMOCOJSON")); } }
@Test public void export_with_keywords() throws IOException { exporter.setKeywords(Keywords.JPA); exporter.setTargetFolder(folder.getRoot()); exporter.export(getClass().getPackage()); String str = Files.toString(new File(folder.getRoot(), "com/querydsl/codegen/QGroup.java"), Charsets.UTF_8); assertTrue(str.contains("QGroup group = new QGroup(\"group1\");")); }
@Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/proxy")), is("get_proxy")); assertThat(helper.postContent(remoteUrl("/proxy"), "proxy"), is("post_proxy")); String failoverContent = Files.toString(tempFile, Charset.defaultCharset()); assertThat(failoverContent, containsString("get_proxy")); assertThat(failoverContent, containsString("post_proxy")); } });
@Override public void run() throws IOException { assertThat(helper.postContent(remoteUrl("/proxy"), "proxy"), is("0XCAFEBABE")); assertThat(helper.postContent(remoteUrl("/proxy"), "proxy"), is("0XCAFEBABE")); assertThat(Files.toString(tempFile, Charset.defaultCharset()), countString("/proxy", 1)); } });