PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/contentcouch/active/ActiveUriTest.java

https://github.com/TOGoS/ContentCouch
Java | 106 lines | 82 code | 19 blank | 5 comment | 1 complexity | 4bc1299af5553bd8eb84793ba0353548 MD5 | raw file
  1. package contentcouch.active;
  2. import java.util.TreeMap;
  3. import junit.framework.TestCase;
  4. import togos.mf.base.BaseRequest;
  5. import contentcouch.active.expression.Expression;
  6. import contentcouch.active.expression.FunctionByNameExpression;
  7. import contentcouch.active.expression.FunctionCallExpression;
  8. import contentcouch.active.expression.UriExpression;
  9. import contentcouch.activefunctions.Hello;
  10. import contentcouch.misc.UriUtil;
  11. public class ActiveUriTest extends TestCase {
  12. public void testResolveUriExpressionToString() {
  13. assertEquals("http://www.nuke24.net/", new UriExpression("http://www.nuke24.net/").toString());
  14. }
  15. public void testUriEncode() {
  16. assertEquals("ABC%20%2B%20123", UriUtil.uriEncode("ABC + 123"));
  17. }
  18. public void testUriDecode() {
  19. assertEquals("ABC + 123", UriUtil.uriDecode("ABC%20%2B%20123"));
  20. assertEquals("ABC + 123", UriUtil.uriDecode("ABC%20%2b%20123"));
  21. }
  22. public void testActiveUriParse() {
  23. Expression e = ActiveUtil.parseExpression("active:foo+bar@x:baz+quux@x:quuux");
  24. assertEquals("(foo bar=x:baz quux=x:quuux)", e.toString());
  25. assertEquals("active:foo+bar@x%3Abaz+quux@x%3Aquuux", e.toUri());
  26. }
  27. public void testActiveUriParse2() {
  28. Expression e = ActiveUtil.parseExpression("active:active:get-func%2bname%40data:,foofunc+bar@x:baz+quux@x:quuux");
  29. assertEquals("((get-func name=\"foofunc\") bar=x:baz quux=x:quuux)", e.toString());
  30. }
  31. public void testParenExpressionParse() {
  32. Expression e = ActiveUtil.parseParenExpression("(foo bar=x:baz quux=x:quuux)");
  33. assertEquals("(foo bar=x:baz quux=x:quuux)", e.toString());
  34. }
  35. public void testParenDefaultKeyExpressionParse() {
  36. Expression e = ActiveUtil.parseParenExpression("(foo x:baz x:quuux)");
  37. assertEquals("(foo operand=x:baz operand1=x:quuux)", e.toString());
  38. }
  39. public void testParenMixedKeyExpressionParse() {
  40. Expression e = ActiveUtil.parseParenExpression("(foo bar=x:BAR x:OPERAND baz=x:BAZ x:OPERAND1)");
  41. assertEquals("(foo bar=x:BAR baz=x:BAZ operand=x:OPERAND operand1=x:OPERAND1)", e.toString());
  42. }
  43. public void testParenExpressionParse2() {
  44. Expression e = ActiveUtil.parseParenExpression("((get-func name=data:,foofunc) bar=x:baz quux=x:quuux)");
  45. assertEquals("((get-func name=\"foofunc\") bar=x:baz quux=x:quuux)", e.toString());
  46. }
  47. /** Test that ResolveUriExpression sanitizes itself on toString() */
  48. public void testResolveUriSanitization() {
  49. Expression e = new UriExpression("http://www.nuke24.net/Biggs *(like a cemetary) ++/foobar");
  50. assertEquals("http://www.nuke24.net/Biggs%20%2A%28like%20a%20cemetary%29%20++/foobar", e.toString());
  51. }
  52. /** Test that active URIs get sanitized properly itself on toString() */
  53. public void testExpressionSanitization() {
  54. TreeMap args = new TreeMap();
  55. args.put("bar%20* ", new UriExpression("http://example.org/(%20* )"));
  56. Expression e = new FunctionCallExpression( new FunctionByNameExpression("foo%20* "), args );
  57. assertEquals("(foo%2520%2A%20 bar%2520%2A%20=http://example.org/%28%20%2A%20%29)", e.toString());
  58. }
  59. /** Test that active URIs within (expressions) are parsed */
  60. public void testParenExpressionParse3() {
  61. Expression e = ActiveUtil.parseParenExpression("((get-func name=data:,foofunc) bar=x:baz quux=active:foo+bar@x:baz)");
  62. assertEquals("((get-func name=\"foofunc\") bar=x:baz quux=(foo bar=x:baz))", e.toString());
  63. }
  64. /** Test that active URIs as functions within (expressions) are parsed */
  65. public void testParenExpressionParse4() {
  66. Expression e = ActiveUtil.parseParenExpression("(active:get-func+name@data:,foofunc bar=x:baz quux=active:foo+bar@x:baz)");
  67. assertEquals("((get-func name=\"foofunc\") bar=x:baz quux=(foo bar=x:baz))", e.toString());
  68. }
  69. /** Test that (expressions) within active URIs are parsed */
  70. public void testParenExpressionInActiveUriParsed() {
  71. Expression e = ActiveUtil.parseActiveUriExpression("active:foo+bar@%28baz%20quux%3Dx%3Aqu%2Bux%29");
  72. assertEquals("(foo bar=(baz quux=x:qu+ux))", e.toString());
  73. }
  74. public void testGetFunctionByClassName() {
  75. Object f = new FunctionByNameExpression("contentcouch.hello").eval(new BaseRequest()).getContent();
  76. assertTrue("Got " + (f == null ? "null" : "a " + f.getClass()) + " but expected a Hello", f instanceof Hello);
  77. assertEquals("Hello, world", ((ActiveFunction)f).call(new BaseRequest(), new TreeMap()).getContent());
  78. }
  79. public void testParseQuotedString() {
  80. Expression e = ActiveUtil.parseExpression("\"foo\nbar\"");
  81. assertEquals("foo\nbar", e.eval(new BaseRequest()).getContent());
  82. }
  83. public void testParseQuotedString2() {
  84. Expression e = ActiveUtil.parseExpression("(blah \"foo\nbar\\\\\")");
  85. assertEquals("(blah operand=\"foo\\nbar\\\\\")", e.toString());
  86. }
  87. }