You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AntAssert.java 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. 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. */
  18. package org.apache.tools.ant;
  19. import static org.junit.Assert.assertFalse;
  20. import static org.junit.Assert.assertTrue;
  21. /**
  22. * Provides common assert functions for use across multiple tests, similar to the <tt>Assert</tt>s
  23. * within JUnit.
  24. */
  25. public class AntAssert {
  26. /**
  27. * Assert that a string contains the given substring.
  28. * @param message the message to fail with if the substring is not present in the target string.
  29. * @param needle the string to search for.
  30. * @param haystack the string to search in.
  31. */
  32. public static void assertContains(String message, String needle, String haystack) {
  33. String formattedMessage = (message == null ? "" : message + " ");
  34. assertTrue(formattedMessage + String.format("expected message containing: <%s> but got: <%s>", needle, haystack), haystack.contains(needle));
  35. }
  36. /**
  37. * Assert that a string contains the given substring. A default failure message will be used if the target string
  38. * is not found.
  39. * @param needle the target string to search for.
  40. * @param haystack the string to search in.
  41. */
  42. public static void assertContains(String needle, String haystack) {
  43. assertContains("", needle, haystack);
  44. }
  45. /**
  46. * Assert that a string does not contain the given substring.
  47. * @param message the message to fail with if the substring is present in the target string.
  48. * @param needle the string to search for.
  49. * @param haystack the string to search in.
  50. */
  51. public static void assertNotContains(String message, String needle, String haystack) {
  52. String formattedMessage = (message == null ? "" : message + " ");
  53. assertFalse(formattedMessage + String.format("expected message not to contain: <%s> but got: <%s>", needle, haystack), haystack.contains(needle));
  54. }
  55. /**
  56. * Assert that a string does not contain the given substring. A default failure message will be used if the target
  57. * string is found.
  58. * @param needle the target string to search for.
  59. * @param haystack the string to search in.
  60. */
  61. public static void assertNotContains(String needle, String haystack) {
  62. assertNotContains("", needle, haystack);
  63. }
  64. }