/core/src/test/java/hudson/util/ArgumentListBuilderTest.java

https://github.com/pfeuffer/hudson · Java · 125 lines · 86 code · 16 blank · 23 comment · 2 complexity · 92291899e204d228540fe980e85a778c MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util;
  25. import org.junit.Assert;
  26. import org.junit.Test;
  27. public class ArgumentListBuilderTest extends Assert {
  28. public static void assertArrayEquals(String msg, boolean[] expected, boolean[] actual) {
  29. assertArrayEquals(msg,box(expected),box(actual));
  30. }
  31. private static Boolean[] box(boolean[] a) {
  32. if(a==null) return null;
  33. Boolean[] r = new Boolean[a.length];
  34. for (int i = 0; i < a.length; i++)
  35. r[i] = a[i];
  36. return r;
  37. }
  38. @Test
  39. public void assertEmptyMask() {
  40. ArgumentListBuilder builder = new ArgumentListBuilder();
  41. builder.add("arg");
  42. builder.add("other", "arguments");
  43. assertFalse("There shouldnt be any masked arguments", builder.hasMaskedArguments());
  44. boolean[] array = builder.toMaskArray();
  45. assertNotNull("The mask array should not be null", array);
  46. assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,false}, array);
  47. }
  48. @Test
  49. public void assertLastArgumentIsMasked() {
  50. ArgumentListBuilder builder = new ArgumentListBuilder();
  51. builder.add("arg");
  52. builder.addMasked("ismasked");
  53. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  54. boolean[] array = builder.toMaskArray();
  55. assertNotNull("The mask array should not be null", array);
  56. assertArrayEquals("The mask array was incorrect", new boolean[]{false,true}, array);
  57. }
  58. @Test
  59. public void assertSeveralMaskedArguments() {
  60. ArgumentListBuilder builder = new ArgumentListBuilder();
  61. builder.add("arg");
  62. builder.addMasked("ismasked");
  63. builder.add("non masked arg");
  64. builder.addMasked("ismasked2");
  65. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  66. boolean[] array = builder.toMaskArray();
  67. assertNotNull("The mask array should not be null", array);
  68. assertArrayEquals("The mask array was incorrect", new boolean[]{false,true, false, true}, array);
  69. }
  70. @Test
  71. public void assertPrependAfterAddingMasked() {
  72. ArgumentListBuilder builder = new ArgumentListBuilder();
  73. builder.addMasked("ismasked");
  74. builder.add("arg");
  75. builder.prepend("first", "second");
  76. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  77. boolean[] array = builder.toMaskArray();
  78. assertNotNull("The mask array should not be null", array);
  79. assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,true,false}, array);
  80. }
  81. @Test
  82. public void assertPrependBeforeAddingMasked() {
  83. ArgumentListBuilder builder = new ArgumentListBuilder();
  84. builder.prepend("first", "second");
  85. builder.addMasked("ismasked");
  86. builder.add("arg");
  87. assertTrue("There should be masked arguments", builder.hasMaskedArguments());
  88. boolean[] array = builder.toMaskArray();
  89. assertNotNull("The mask array should not be null", array);
  90. assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,true,false}, array);
  91. }
  92. @Test
  93. public void testToWindowsCommand() {
  94. ArgumentListBuilder builder = new ArgumentListBuilder(
  95. "ant.bat", "-Dfoo1=abc", // nothing special, no quotes
  96. "-Dfoo2=foo bar", "-Dfoo3=/u*r", "-Dfoo4=/us?", // add quotes, no other escaping
  97. "-Dfoo5=foo;bar^baz", "-Dfoo6=<xml>&here;</xml>", // add quotes and ^ escaping
  98. "-Dfoo7=foo|bar\"baz", // add quotes, ^| for | and "" for "
  99. "-Dfoo8=% %QED% %comspec% %-%(%.%", // add quotes, and extra quotes for %Q and %c
  100. "-Dfoo9=%'''%%@%"); // no quotes as none of the % are followed by a letter
  101. assertArrayEquals(new String[] { "cmd.exe", "/C",
  102. "\"ant.bat -Dfoo1=abc \"-Dfoo2=foo bar\""
  103. + " \"-Dfoo3=/u*r\" \"-Dfoo4=/us?\" \"-Dfoo5=foo;bar^^baz\""
  104. + " \"-Dfoo6=^<xml^>^&here;^</xml^>\" \"-Dfoo7=foo^|bar\"\"baz\""
  105. + " \"-Dfoo8=% %\"Q\"ED% %\"c\"omspec% %-%(%.%\""
  106. + " -Dfoo9=%'''%%@% && exit %%ERRORLEVEL%%\"" },
  107. builder.toWindowsCommand().toCommandArray());
  108. }
  109. }