/tst/org/diffkit/util/tst/TestStringUtil.groovy

http://diffkit.googlecode.com/ · Groovy · 136 lines · 99 code · 19 blank · 18 comment · 40 complexity · 6b72e0b5223bf48b64da84dbd2020c4f MD5 · raw file

  1. package org.diffkit.util.tst
  2. /**
  3. * Copyright 2010-2011 Joseph Panico
  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 groovy.util.GroovyTestCase;
  18. import org.diffkit.db.DKDBFlavor;
  19. import org.diffkit.diff.testcase.TestCaseRunner;
  20. import org.diffkit.util.DKStringUtil;
  21. /**
  22. * @author jpanico
  23. */
  24. public class TestStringUtil extends GroovyTestCase {
  25. public void testStringNumberComparator() {
  26. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( null, null) == 0
  27. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaa', null) == 0
  28. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( null, 'bbb') == 0
  29. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaa', 'bbb') == 0
  30. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaa', 'bbb') == 0
  31. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aa15aa', 'bbb') == 1
  32. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa', 'bb15bb') == -1
  33. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa15', 'bb15bb') == 0
  34. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa100', 'bb15bb') == 1
  35. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa-100', 'bb15bb') == 1
  36. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa100', 'bb200bb') == -1
  37. assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa-100', 'bb200bb') == -1
  38. }
  39. public void testExtractFirstInteger() {
  40. assert ! DKStringUtil.extractFirstInteger(null)
  41. assert ! DKStringUtil.extractFirstInteger('asdbef-dasdfa')
  42. assert DKStringUtil.extractFirstInteger('asdbef15da25sdfa') == 15
  43. assert DKStringUtil.extractFirstInteger('asdbef-015da25sdfa') == 15
  44. assert DKStringUtil.extractFirstInteger('15asdbef-0da25sdfa') == 15
  45. assert DKStringUtil.extractFirstInteger('sdfa15') == 15
  46. }
  47. public void testParseIntegers() {
  48. assert ! DKStringUtil.parseIntegers(null)
  49. assert DKStringUtil.parseIntegers("1-2") == [1,2]
  50. assert DKStringUtil.parseIntegers(" 0 - 10 ") == [0,1,2,3,4,5,6,7,8,9,10]
  51. assert DKStringUtil.parseIntegers("1,2") == [1,2]
  52. assert DKStringUtil.parseIntegers(" 0, 10 ") == [0,10]
  53. }
  54. public void testParseIntegerRange() {
  55. assert ! DKStringUtil.parseIntegerRange(null)
  56. assert DKStringUtil.parseIntegerRange("1-2") == [1,2]
  57. assert DKStringUtil.parseIntegerRange(" 0 - 10 ") == [0,1,2,3,4,5,6,7,8,9,10]
  58. }
  59. public void testParseEnumList() {
  60. def enumList = DKStringUtil.parseEnumList('ORACLE,DB2,H2', DKDBFlavor)
  61. assert enumList
  62. assert enumList.size() == 3
  63. assert enumList[1] == DKDBFlavor.DB2
  64. }
  65. public void testParseIntegerList() {
  66. def integerList = DKStringUtil.parseIntegerList('1,2,3,4')
  67. assert integerList
  68. assert integerList.size() == 4
  69. assert integerList[2] == 3
  70. assert integerList[2] instanceof Integer
  71. }
  72. public void testToSetString(){
  73. assert DKStringUtil.toSetString(['a','b']) == '(a, b)'
  74. assert DKStringUtil.toSetString([]) == '()'
  75. String[] target = ['a','b']
  76. assert DKStringUtil.toSetString(target) == '(a, b)'
  77. assert DKStringUtil.toSetString(null) == '()'
  78. }
  79. public void testQuote(){
  80. assert ! DKStringUtil.quote(null,DKStringUtil.Quote.DOUBLE)
  81. assert DKStringUtil.quote("",DKStringUtil.Quote.DOUBLE) == '""'
  82. assert DKStringUtil.quote("string",DKStringUtil.Quote.SINGLE) == "'string'"
  83. assert DKStringUtil.quote("'string'",DKStringUtil.Quote.SINGLE) == "'string'"
  84. }
  85. public void testQuoteEach(){
  86. String[] searchStrings = ['xxxx','yyyy']
  87. def target = 'in this age xxxx of yyyy affordable beauty, there was something \n xxxx heraldic'
  88. assert ! DKStringUtil.quoteAllOccurrencesOfEach(null,null,DKStringUtil.Quote.DOUBLE)
  89. assert ! DKStringUtil.quoteAllOccurrencesOfEach(null,searchStrings,DKStringUtil.Quote.DOUBLE)
  90. assert DKStringUtil.quoteAllOccurrencesOfEach(target,null,DKStringUtil.Quote.DOUBLE) == target
  91. assert DKStringUtil.quoteAllOccurrencesOfEach(target,searchStrings,DKStringUtil.Quote.DOUBLE) == 'in this age "xxxx" of "yyyy" affordable beauty, there was something \n "xxxx" heraldic'
  92. }
  93. public void testUnquote() {
  94. assert ! DKStringUtil.unquote((String)null,DKStringUtil.Quote.DOUBLE)
  95. assert DKStringUtil.unquote('hello',DKStringUtil.Quote.DOUBLE) == 'hello'
  96. assert DKStringUtil.unquote('"hello',DKStringUtil.Quote.DOUBLE) == '"hello'
  97. assert DKStringUtil.unquote('"hello"',DKStringUtil.Quote.DOUBLE) == 'hello'
  98. assert DKStringUtil.unquote('""',DKStringUtil.Quote.DOUBLE) == ''
  99. }
  100. public void testReplaceEach(){
  101. def source = '''Beware the Jabberwock, my son!
  102. The jaws that bite, the claws that catch!
  103. Beware the Jubjub bird, and shun
  104. The frumious Bandersnatch!'''
  105. def expected = '''swear the Jabberwock, my son!
  106. The jaws that bite, the claws that catch!
  107. swear the Jubjub bird, and shun
  108. The frumpy Bandersnatch!'''
  109. def substitutions = ['Beware':'swear', 'frumious':'frumpy']
  110. assert DKStringUtil.replaceEach(source, substitutions) == expected
  111. source = '''this is my test @TargetDatabase@'''
  112. expected = '''this is my test mem:testcase;DB_CLOSE_DELAY=-1'''
  113. substitutions = [ : ]
  114. substitutions.put(TestCaseRunner.TARGET_DATABASE_TOKEN,TestCaseRunner.DEFAULT_TESTCASE_DATABASE )
  115. assert DKStringUtil.replaceEach(source, substitutions) == expected
  116. }
  117. }