/** * @see #writeString(File, String, String) */ public static void writeString(final String dest, final String data, final String encoding) throws IOException { writeString(file(dest), data, encoding); }
/** * @see #writeString(File, String, String) */ public static void writeString(final File dest, final String data) throws IOException { writeString(dest, data, encoding()); }
/** * @see #writeString(File, String, String) */ public static void writeString(final String dest, final String data) throws IOException { writeString(file(dest), data, encoding()); }
@Test void testCompareWithInputStreams_ExpectedNoSuccessfulCompare(TestInfo testInfo) throws Exception { final String text = "jodd makes fun!"; final File file = new File(StreamUtilTest.BASE_DIR, testInfo.getTestMethod().get().getName() + ".txt"); FileUtil.writeString(file, " " + text, "UTF-8"); boolean actual; try (ByteArrayInputStream in1 = new ByteArrayInputStream(text.getBytes()); FileInputStream in2 = new FileInputStream(file)) { actual = StreamUtil.compare(in1, in2); } // asserts assertFalse(actual); }
@Test void testCompareWithInputStreams_ExpectedSuccessfulCompare(TestInfo testInfo) throws Exception { final String text = "jodd makes fun!" + System.lineSeparator(); final File file = new File(StreamUtilTest.BASE_DIR, testInfo.getTestMethod().get().getName() + ".txt"); FileUtil.writeString(file, text, "UTF-8"); boolean actual; try (ByteArrayInputStream in1 = new ByteArrayInputStream(text.getBytes()); FileInputStream in2 = new FileInputStream(file)) { actual = StreamUtil.compare(in1, in2); } // asserts assertTrue(actual); }
@Test void testReadString_with_new_file() throws Exception { final String expected = "üöä ÜÖÄ ß"; File file = new File(BASE_DIR, "file_with_german_umlaut.txt"); FileUtil.writeString(file, expected, "UTF-8"); final String actual = PathUtil.readString(file.toPath()); // asserts assertEquals(expected, actual); }
@Test void testReadChars_InputStream(TestInfo testInfo) throws Exception { final String text = "jodd - Get things done!" + System.lineSeparator(); final char[] expected = text.toCharArray(); final File file = new File(BASE_DIR, testInfo.getTestMethod().get().getName()); FileUtil.writeString(file, text, "UTF-8"); char[] actual = null; try (FileInputStream inputStream = new FileInputStream(file)) { actual = StreamUtil.readChars(inputStream); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
try { File tempFile = FileUtil.createTempFile(); FileUtil.writeString(tempFile, value.toString()); return tempFile; } catch (IOException ioex) {
@ParameterizedTest @MethodSource("testdata_testReadChars_InputStream_CharCount") void testReadChars_InputStream_CharCount(char[] expected, String text, int charCount, TestInfo testInfo ) throws Exception { final int random = MathUtil.randomInt(1, 2500); final File file = new File(BASE_DIR, testInfo.getTestMethod().get().getName() + "." + random); FileUtil.writeString(file, text, "UTF-8"); char[] actual = null; try (FileInputStream inputStream = new FileInputStream(file)) { actual = StreamUtil.readChars(inputStream, charCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadBytes_Reader_Encoding") void testReadBytes_Reader_Encoding(byte[] expected, String text, String encoding, TestInfo testInfo) throws Exception { final int random = MathUtil.randomInt(1, 2500); final File file = new File(StreamUtilTest.BASE_DIR, testInfo.getTestMethod().get().getName() + random); FileUtil.writeString(file, text, encoding); byte[] actual = null; try (FileReader reader = new FileReader(file)) { actual = StreamUtil.readBytes(reader, encoding); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadBytes_Reader_Encoding_ByteCount") void testReadBytes_Reader_Encoding_ByteCount(byte[] expected, String text, String encoding, int byteCount, TestInfo testInfo) throws Exception { final int random = MathUtil.randomInt(1, 2500); final File file = new File(StreamUtilTest.BASE_DIR, testInfo.getTestMethod().get().getName() + random); FileUtil.writeString(file, text, encoding); byte[] actual = null; try (FileReader reader = new FileReader(file)) { actual = StreamUtil.readBytes(reader, encoding, byteCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
FileUtil.writeString(new File(dataRoot, "test.txt"), s); } catch (Exception ex) { fail("FileUtil.writeString " + ex.toString()); FileUtil.writeString(dataRoot + "/test.txt", s); } catch (Exception ex) { fail("FileUtil.writeString " + ex.toString());
@Test void check_against_created_text_file() throws Exception { final File input = FileUtil.createTempFile(); FileUtil.writeString(input, "jodd makes fun!"); final boolean actual = FileUtil.isBinary(input); // asserts assertEquals(false, actual); }
@Test void testUnicodeString() { String s = "This is a test file\nIt only has\nthree lines!!"; char[] buf = s.toCharArray(); buf[0] = 256; s = new String(buf); try { FileUtil.writeString(dataRoot + "/test2.txt", s, "UTF-16"); } catch (Exception ex) { fail("FileUtil.writeString " + ex.toString()); } String s2 = null; try { s2 = FileUtil.readString(dataRoot + "/test2.txt", "UTF-16"); } catch (Exception ex) { fail("FileUtil.readString " + ex.toString()); } assertEquals(s, s2); try { FileUtil.delete(dataRoot + "/test2.txt"); } catch (IOException ioex) { fail("FileUtil.delete" + ioex.toString()); } }
@Test void testDirWatcher() throws IOException { DirWatcher dirWatcher = new DirWatcher(dataRoot, "*.md"); final StringBuilder sb = new StringBuilder(); dirWatcher.register(event -> sb.append(event.type().name() + ":" + event.target().getName() + "\n")); dirWatcher.start(100); File destFile = new File(dataRoot, "jodd.md"); FileUtil.writeString(destFile, "#Jodd"); ThreadUtil.sleep(600); FileUtil.writeString(destFile, "#Jodd2"); ThreadUtil.sleep(600); FileUtil.delete(destFile); ThreadUtil.sleep(600); dirWatcher.stop(); assertEquals( DirWatcherEvent.Type.CREATED + ":jodd.md\n" + DirWatcherEvent.Type.MODIFIED + ":jodd.md\n" + DirWatcherEvent.Type.DELETED + ":jodd.md\n", sb.toString()); }
@Test void testReadObject() throws Exception { final String expected = "Jodd - The Unbearable Lightness of Java :-)"; final File targetFile = new File(BASE_DIR, "testReadObject.ser"); // ensure that file exists with other data FileUtil.writeString(targetFile, "only test data"); // overwrite given file ObjectUtil.writeObject(targetFile, expected); final Object actual = ObjectUtil.readObject(targetFile); // asserts assertNotNull(actual); assertTrue(actual instanceof String); assertFalse(expected == actual); assertEquals(expected, actual); }
@Test void testDirWatcherWithFile() throws IOException { DirWatcher dirWatcher = new DirWatcher(dataRoot) .monitor("*.md") .useWatchFile("watch.txt"); final StringBuilder sb = new StringBuilder(); dirWatcher.register( event -> sb.append(event.type().name() + ":" + event.target().getName() + "\n")); dirWatcher.start(100); File watchFile = new File(dataRoot, "watch.txt"); File destFile = new File(dataRoot, "jodd.md"); FileUtil.writeString(destFile, "#Jodd"); FileUtil.touch(watchFile); ThreadUtil.sleep(600); FileUtil.writeString(destFile, "#Jodd2"); ThreadUtil.sleep(600); FileUtil.delete(destFile); FileUtil.touch(watchFile); ThreadUtil.sleep(600); dirWatcher.stop(); assertEquals( DirWatcherEvent.Type.CREATED + ":jodd.md\n" + //DirWatcher.Event.MODIFIED + ":jodd.md\n" + DirWatcherEvent.Type.DELETED + ":jodd.md\n", sb.toString()); }
@Test void testUpload() throws IOException { EchoTestServer echoTestServer = new EchoTestServer(); File file = FileUtil.createTempFile(); file.deleteOnExit(); FileUtil.writeString(file, "upload тест"); assertEquals("upload тест", FileUtil.readString(file)); HttpResponse response = HttpRequest .post("http://localhost:8081/hello") .form("id", "12") .form("file", file) .send(); assertEquals(200, response.statusCode()); assertEquals("OK", response.statusPhrase()); assertEquals("POST", echoTestServer.method); assertEquals("12", echoTestServer.params.get("id")); File uploadedFile = new File(echoTestServer.files.get("file").toString()); assertNotNull(uploadedFile); assertEquals("upload тест", FileUtil.readString(uploadedFile)); assertEquals("POST /hello", response.body()); echoTestServer.stop(); file.delete(); }
@Test void testUploadWithMonitor() throws IOException { EchoTestServer echoTestServer = new EchoTestServer(); File file = FileUtil.createTempFile(); file.deleteOnExit(); FileUtil.writeString(file, StringUtil.repeat('A', 1024)); final StringBuilder sb = new StringBuilder(); HttpResponse response = HttpRequest .post("http://localhost:8081/hello") .form("id", "12") .form("file", file) .monitor(new HttpProgressListener() { @Override public void transferred(int len) { sb.append(":" + len); } }) .send(); assertEquals(200, response.statusCode()); assertEquals("OK", response.statusPhrase()); echoTestServer.stop(); file.delete(); assertEquals(":0:512:1024:148", StringUtil.substring(sb.toString(), 0, -1)); }
@Test void testFileUpload() throws IOException { HttpRequest request = HttpRequest.get("http://jodd.org/?id=173"); request.header("User-Agent", "Scaly").form("one", "funny"); File tempFile = FileUtil.createTempFile(); tempFile.deleteOnExit(); FileUtil.writeString(tempFile, "qwerty"); request.form("two", tempFile); byte[] bytes = request.toByteArray(); // read HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes)); HttpMultiMap<?> httpParams2 = request2.form(); assertEquals(request.method(), request2.method()); assertEquals(request.path(), request2.path()); assertEquals(request.queryString(), request2.queryString()); assertEquals(request.header("User-Agent"), request2.header("User-Agent")); assertEquals(request.header("Content-Type"), request2.header("content-type")); assertEquals(request.header("Content-Length"), request2.header("content-length")); HttpMultiMap<?> params1 = request.form(); HttpMultiMap<?> params2 = request2.form(); assertEquals(params1.size(), params2.size()); assertEquals(params2.get("one"), params2.get("one")); FileUpload fu = (FileUpload) httpParams2.get("two"); assertEquals(6, fu.getSize()); String str = new String(fu.getFileContent()); assertEquals("qwerty", str); tempFile.delete(); }