/test/org/nutz/walnut/impl/hook/IoHookTest.java

https://gitlab.com/zozoh/walnut · Java · 295 lines · 215 code · 44 blank · 36 comment · 0 complexity · 2110b411ba1eab6020cb274920353a41 MD5 · raw file

  1. package org.nutz.walnut.impl.hook;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertNull;
  5. import org.junit.Test;
  6. import org.nutz.lang.Lang;
  7. import org.nutz.lang.Strings;
  8. import org.nutz.trans.Atom;
  9. import org.nutz.trans.Proton;
  10. import org.nutz.walnut.BaseHookTest;
  11. import org.nutz.walnut.api.box.WnBox;
  12. import org.nutz.walnut.api.io.WnObj;
  13. import org.nutz.walnut.api.io.WnRace;
  14. import org.nutz.walnut.util.Wn;
  15. public class IoHookTest extends BaseHookTest {
  16. @Test
  17. public void test_mount() {
  18. // 准备钩子
  19. WnObj oHook = io.createIfNoExists(oHookHome, "mount/do_log", WnRace.FILE);
  20. io.writeText(oHook, "echo '${nm} mnt:[${mnt}] old:[${_old_mnt}]' >> ~/testlog");
  21. // 执行
  22. Wn.WC().hooking(hc, new Atom() {
  23. public void run() {
  24. WnObj o = io.create(null, "~/mydir", WnRace.DIR);
  25. io.setMount(o, "file://~/tmp/walnuta");
  26. io.setMount(o, null);
  27. }
  28. });
  29. // 查看 log
  30. WnObj oLog = io.check(oHome, "testlog");
  31. String log = io.readText(oLog);
  32. String[] lines = Strings.splitIgnoreBlank(log, "\n");
  33. assertEquals(2, lines.length);
  34. assertEquals("mydir mnt:[file://~/tmp/walnuta] old:[]", lines[0]);
  35. assertEquals("mydir mnt:[] old:[file://~/tmp/walnuta]", lines[1]);
  36. }
  37. @Test
  38. public void test_meta() {
  39. // 准备钩子
  40. WnObj oHook = io.createIfNoExists(oHookHome, "meta/do_log", WnRace.FILE);
  41. io.writeText(oHook, "obj -VN id:${id} -c -e '^x|y|z$' >> ~/testlog");
  42. // 执行
  43. Wn.WC().hooking(hc, new Atom() {
  44. public void run() {
  45. WnObj o = io.create(oHome, "abc.js", WnRace.FILE);
  46. io.appendMeta(o, "x:19");
  47. io.appendMeta(o, "y:89");
  48. io.appendMeta(o, "z:64");
  49. }
  50. });
  51. // 查看 log
  52. WnObj oLog = io.check(oHome, "testlog");
  53. String log = io.readText(oLog);
  54. String[] lines = Strings.splitIgnoreBlank(log, "\n");
  55. assertEquals(3, lines.length);
  56. assertEquals("19", lines[0]);
  57. assertEquals("1989", lines[1]);
  58. assertEquals("198964", lines[2]);
  59. }
  60. @Test
  61. public void test_move() {
  62. // 准备钩子
  63. WnObj oHook = io.createIfNoExists(oHookHome, "move/do_log", WnRace.FILE);
  64. io.writeText(oHook, "echo 'mv:${nm} to ${_mv_dest}' >> ~/testlog");
  65. // 执行
  66. Wn.WC().hooking(hc, new Atom() {
  67. public void run() {
  68. WnObj o = io.create(oHome, "abc.js", WnRace.FILE);
  69. io.move(o, o.path() + ".new.js");
  70. o = io.create(oHome, "bbc.js", WnRace.FILE);
  71. io.move(o, o.path() + ".new.js");
  72. o = io.create(oHome, "cbc.js", WnRace.FILE);
  73. io.move(o, o.path() + ".new.js");
  74. }
  75. });
  76. // 查看 log
  77. WnObj oLog = io.check(oHome, "testlog");
  78. String log = io.readText(oLog);
  79. String[] lines = Strings.splitIgnoreBlank(log, "\n");
  80. assertEquals(3, lines.length);
  81. assertEquals("mv:abc.js to /home/xiaobai/abc.js.new.js", lines[0]);
  82. assertEquals("mv:bbc.js to /home/xiaobai/bbc.js.new.js", lines[1]);
  83. assertEquals("mv:cbc.js to /home/xiaobai/cbc.js.new.js", lines[2]);
  84. }
  85. @Test
  86. public void test_create_in_case() {
  87. // 准备钩子
  88. WnObj oHook = io.createIfNoExists(oHookHome, "create/for_js", WnRace.FILE);
  89. io.writeText(oHook, "echo 'js:${nm}' >> ~/testlog");
  90. io.appendMeta(oHook, "hook_by:[{tp:'js'}]");
  91. oHook = io.createIfNoExists(oHookHome, "create/for_css", WnRace.FILE);
  92. io.writeText(oHook, "echo 'css:${nm}' >> ~/testlog");
  93. io.appendMeta(oHook, "hook_by:[{tp:'css'}]");
  94. // 执行
  95. Wn.WC().hooking(hc, new Atom() {
  96. public void run() {
  97. io.create(oHome, "abc.js", WnRace.FILE);
  98. io.create(oHome, "abc.css", WnRace.FILE);
  99. io.create(oHome, "abc", WnRace.FILE);
  100. io.create(oHome, "last.js", WnRace.FILE);
  101. }
  102. });
  103. // 查看 log
  104. WnObj oLog = io.check(oHome, "testlog");
  105. String log = io.readText(oLog);
  106. String[] lines = Strings.splitIgnoreBlank(log, "\n");
  107. assertEquals(3, lines.length);
  108. assertEquals("js:abc.js", lines[0]);
  109. assertEquals("css:abc.css", lines[1]);
  110. assertEquals("js:last.js", lines[2]);
  111. }
  112. @Test
  113. public void test_delete_in_cmd_pipe() {
  114. // 准备钩子
  115. WnObj oHook = io.createIfNoExists(oHookHome, "delete/before_delete", WnRace.FILE);
  116. io.writeText(oHook, "cp ${ph} ${ph}.bak");
  117. // 准备素材
  118. final WnObj o = io.create(oHome, "abc.txt", WnRace.FILE);
  119. io.writeText(o, "hello");
  120. // 执行创建
  121. Wn.WC().hooking(hc, new Atom() {
  122. public void run() {
  123. WnBox box = _alloc_box();
  124. box.run("rm ~/abc.txt | echo DONE");
  125. boxes.free(box);
  126. }
  127. });
  128. // 验证
  129. assertNull(io.fetch(null, o.path()));
  130. WnObj oBak = io.check(oHome, "abc.txt.bak");
  131. String txt = io.readText(oBak);
  132. assertEquals("hello", txt);
  133. assertFalse(o.isSameId(oBak));
  134. }
  135. @Test
  136. public void test_delete_in_cmd() {
  137. // 准备钩子
  138. WnObj oHook = io.createIfNoExists(oHookHome, "delete/before_delete", WnRace.FILE);
  139. io.writeText(oHook, "cp ${ph} ${ph}.bak");
  140. // 准备素材
  141. final WnObj o = io.create(oHome, "abc.txt", WnRace.FILE);
  142. io.writeText(o, "hello");
  143. // 执行创建
  144. Wn.WC().hooking(hc, new Atom() {
  145. public void run() {
  146. WnBox box = _alloc_box();
  147. box.run("rm ~/abc.txt");
  148. boxes.free(box);
  149. }
  150. });
  151. // 验证
  152. assertNull(io.fetch(null, o.path()));
  153. WnObj oBak = io.check(oHome, "abc.txt.bak");
  154. String txt = io.readText(oBak);
  155. assertEquals("hello", txt);
  156. assertFalse(o.isSameId(oBak));
  157. }
  158. @Test
  159. public void test_delete() {
  160. // 准备钩子
  161. WnObj oHook = io.createIfNoExists(oHookHome, "delete/before_delete", WnRace.FILE);
  162. io.writeText(oHook, "cp ${ph} ${ph}.bak");
  163. // 准备素材
  164. final WnObj o = io.create(oHome, "abc.txt", WnRace.FILE);
  165. io.writeText(o, "hello");
  166. // 执行创建
  167. Wn.WC().hooking(hc, new Atom() {
  168. public void run() {
  169. io.delete(o);
  170. }
  171. });
  172. // 验证
  173. assertNull(io.fetch(null, o.path()));
  174. WnObj oBak = io.check(oHome, "abc.txt.bak");
  175. String txt = io.readText(oBak);
  176. assertEquals("hello", txt);
  177. assertFalse(o.isSameId(oBak));
  178. }
  179. @Test
  180. public void test_create_in_cmd_pipe() {
  181. // 准备钩子
  182. WnObj oHook = io.createIfNoExists(oHookHome, "write/append_sha1", WnRace.FILE);
  183. io.writeText(oHook, "echo '${sha1}' >> id:${id}");
  184. // 执行创建
  185. Wn.WC().hooking(hc, new Atom() {
  186. public void run() {
  187. WnBox box = _alloc_box();
  188. box.run("echo 'hello' | md5sum > ~/abc.txt");
  189. boxes.free(box);
  190. }
  191. });
  192. // 验证
  193. WnObj o = io.check(oHome, "abc.txt");
  194. String txt = io.readText(o);
  195. assertEquals(Lang.md5("hello\n")
  196. + "\n"
  197. + Lang.sha1(Lang.md5("hello\n") + "\n")
  198. + "\n",
  199. txt);
  200. assertEquals(Lang.sha1(txt), o.sha1());
  201. }
  202. @Test
  203. public void test_create_in_cmd() {
  204. // 准备钩子
  205. WnObj oHook = io.createIfNoExists(oHookHome, "create/show_ph", WnRace.FILE);
  206. io.writeText(oHook, "echo '${ph}' | md5sum > id:${id}\n");
  207. // 执行创建
  208. Wn.WC().hooking(hc, new Atom() {
  209. public void run() {
  210. box.run("touch ~/abc.txt");
  211. }
  212. });
  213. // 验证
  214. WnObj o = io.check(oHome, "abc.txt");
  215. String txt = io.readText(o);
  216. assertEquals(Lang.md5("/home/xiaobai/abc.txt\n") + "\n", txt);
  217. assertEquals(Lang.sha1(txt), o.sha1());
  218. }
  219. @Test
  220. public void test_create2() {
  221. // 准备钩子
  222. WnObj oHook = io.createIfNoExists(oHookHome, "create/show_ph", WnRace.FILE);
  223. io.writeText(oHook, "echo '${ph}' | md5sum > id:${id}\n");
  224. // 执行创建
  225. WnObj o = Wn.WC().hooking(hc, new Proton<WnObj>() {
  226. protected WnObj exec() {
  227. return io.create(oHome, "abc.txt", WnRace.FILE);
  228. }
  229. });
  230. // 验证
  231. String txt = io.readText(o);
  232. assertEquals(Lang.md5("/home/xiaobai/abc.txt\n") + "\n", txt);
  233. assertEquals(Lang.sha1(txt), o.sha1());
  234. }
  235. @Test
  236. public void test_create() {
  237. // 准备钩子
  238. WnObj oHook = io.createIfNoExists(oHookHome, "create/show_ph", WnRace.FILE);
  239. io.writeText(oHook, "echo '${ph}' > id:${id}\n");
  240. // 执行创建
  241. WnObj o = Wn.WC().hooking(hc, new Proton<WnObj>() {
  242. protected WnObj exec() {
  243. return io.create(oHome, "abc.txt", WnRace.FILE);
  244. }
  245. });
  246. // 验证
  247. String txt = io.readText(o);
  248. assertEquals("/home/xiaobai/abc.txt\n", txt);
  249. assertEquals(Lang.sha1(txt), o.sha1());
  250. }
  251. }