/machinelearning/5.0/drools-templates/src/test/java/org/drools/template/model/SnippetBuilderTest.java

https://github.com/droolsjbpm/droolsjbpm-contributed-experiments · Java · 98 lines · 59 code · 20 blank · 19 comment · 0 complexity · 28891915b173b22efa44602052e09c21 MD5 · raw file

  1. package org.drools.template.model;
  2. /*
  3. * Copyright 2005 JBoss Inc
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import org.drools.template.model.SnippetBuilder;
  18. import junit.framework.TestCase;
  19. /**
  20. * @author <a href="mailto:michael.neale@gmail.com"> Michael Neale</a>
  21. *
  22. */
  23. public class SnippetBuilderTest extends TestCase {
  24. public void testBuildSnippet() {
  25. final String snippet = "something.param.getAnother().equals($param);";
  26. final SnippetBuilder snip = new SnippetBuilder( snippet );
  27. final String cellValue = "$42";
  28. final String result = snip.build( cellValue );
  29. assertNotNull( result );
  30. assertEquals( "something.param.getAnother().equals($42);",
  31. result );
  32. }
  33. public void testBuildSnippetNoPlaceHolder() {
  34. final String snippet = "something.getAnother().equals(blah);";
  35. final SnippetBuilder snip = new SnippetBuilder( snippet );
  36. final String cellValue = "this is ignored...";
  37. final String result = snip.build( cellValue );
  38. assertEquals( snippet,
  39. result );
  40. }
  41. public void testSingleParamMultipleTimes() {
  42. final String snippet = "something.param.getAnother($param).equals($param);";
  43. final SnippetBuilder snip = new SnippetBuilder( snippet );
  44. final String cellValue = "42";
  45. final String result = snip.build( cellValue );
  46. assertNotNull( result );
  47. assertEquals( "something.param.getAnother(42).equals(42);",
  48. result );
  49. }
  50. public void testMultiPlaceHolder() {
  51. final String snippet = "something.getAnother($1,$2).equals($2, '$2');";
  52. final SnippetBuilder snip = new SnippetBuilder( snippet );
  53. final String result = snip.build( "x, y" );
  54. assertEquals( "something.getAnother(x,y).equals(y, 'y');",
  55. result );
  56. }
  57. public void testMultiPlaceHolderSingle() {
  58. final String snippet = "something.getAnother($1).equals($1);";
  59. final SnippetBuilder snip = new SnippetBuilder( snippet );
  60. final String result = snip.build( "x" );
  61. assertEquals( "something.getAnother(x).equals(x);",
  62. result );
  63. }
  64. public void testStartWithParam() {
  65. final String snippet = "$1 goo $2";
  66. final SnippetBuilder snip = new SnippetBuilder( snippet );
  67. final String result = snip.build( "x, y" );
  68. assertEquals( "x goo y",
  69. result );
  70. }
  71. public void testMultiPlaceHolderEscapedComma() {
  72. final String snippet = "rulesOutputRouting.set( $1, $2, $3, $4, $5 );";
  73. final SnippetBuilder snip = new SnippetBuilder( snippet );
  74. final String result = snip.build( "\"80\",\"Department Manager\",toa.getPersonExpense().getEntityCode(\"Part Of\"\\,\"Office\"),10004,30" );
  75. assertEquals( "rulesOutputRouting.set( \"80\", \"Department Manager\", toa.getPersonExpense().getEntityCode(\"Part Of\",\"Office\"), 10004, 30 );",
  76. result );
  77. }
  78. }