PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1package org.diffkit.util.tst
  2
  3/**
  4 * Copyright 2010-2011 Joseph Panico
  5 *
  6 * Licensed under the Apache License, Version 2.0 (the "License");
  7 * you may not use this file except in compliance with the License.
  8 * You may obtain a copy of the License at
  9 *
 10 *   http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19
 20import groovy.util.GroovyTestCase;
 21
 22import org.diffkit.db.DKDBFlavor;
 23import org.diffkit.diff.testcase.TestCaseRunner;
 24import org.diffkit.util.DKStringUtil;
 25
 26
 27/**
 28 * @author jpanico
 29 */
 30public class TestStringUtil extends GroovyTestCase {
 31   
 32   public void testStringNumberComparator() {
 33      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( null, null) == 0
 34      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaa', null) == 0
 35      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( null, 'bbb') == 0
 36      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaa', 'bbb') == 0
 37      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaa', 'bbb') == 0
 38      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aa15aa', 'bbb') == 1
 39      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa', 'bb15bb') == -1
 40      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa15', 'bb15bb') == 0
 41      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa100', 'bb15bb') == 1
 42      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa-100', 'bb15bb') == 1
 43      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa100', 'bb200bb') == -1
 44      assert DKStringUtil.StringNumberComparator.INSTANCE.compare( 'aaaa-100', 'bb200bb') == -1
 45   }
 46   
 47   public void testExtractFirstInteger() {
 48      assert ! DKStringUtil.extractFirstInteger(null)
 49      assert ! DKStringUtil.extractFirstInteger('asdbef-dasdfa')
 50      assert DKStringUtil.extractFirstInteger('asdbef15da25sdfa') == 15
 51      assert DKStringUtil.extractFirstInteger('asdbef-015da25sdfa') == 15
 52      assert DKStringUtil.extractFirstInteger('15asdbef-0da25sdfa') == 15
 53      assert DKStringUtil.extractFirstInteger('sdfa15') == 15
 54   }
 55   
 56   public void testParseIntegers() {
 57      assert ! DKStringUtil.parseIntegers(null)
 58      assert  DKStringUtil.parseIntegers("1-2") == [1,2]
 59      assert  DKStringUtil.parseIntegers(" 0 - 10 ") == [0,1,2,3,4,5,6,7,8,9,10]
 60      assert  DKStringUtil.parseIntegers("1,2") == [1,2]
 61      assert  DKStringUtil.parseIntegers(" 0, 10 ") == [0,10]
 62   }
 63   
 64   public void testParseIntegerRange() {
 65      assert ! DKStringUtil.parseIntegerRange(null)
 66      assert  DKStringUtil.parseIntegerRange("1-2") == [1,2]
 67      assert  DKStringUtil.parseIntegerRange(" 0 - 10 ") == [0,1,2,3,4,5,6,7,8,9,10]
 68   }
 69   
 70   public void testParseEnumList() {
 71      def enumList = DKStringUtil.parseEnumList('ORACLE,DB2,H2', DKDBFlavor)
 72      assert enumList
 73      assert enumList.size() == 3
 74      assert enumList[1] == DKDBFlavor.DB2
 75   }
 76   
 77   public void testParseIntegerList() {
 78      def integerList = DKStringUtil.parseIntegerList('1,2,3,4')
 79      assert integerList
 80      assert integerList.size() == 4
 81      assert integerList[2] == 3
 82      assert integerList[2] instanceof Integer
 83   }
 84   
 85   public void testToSetString(){
 86      assert DKStringUtil.toSetString(['a','b']) == '(a, b)'
 87      assert DKStringUtil.toSetString([]) == '()'
 88      String[] target = ['a','b']
 89      assert DKStringUtil.toSetString(target) == '(a, b)'
 90      assert DKStringUtil.toSetString(null) == '()'
 91   }
 92   
 93   public void testQuote(){
 94      assert ! DKStringUtil.quote(null,DKStringUtil.Quote.DOUBLE) 
 95      assert DKStringUtil.quote("",DKStringUtil.Quote.DOUBLE) == '""'
 96      assert DKStringUtil.quote("string",DKStringUtil.Quote.SINGLE) == "'string'"
 97      assert DKStringUtil.quote("'string'",DKStringUtil.Quote.SINGLE) == "'string'"
 98   }
 99   
100   public void testQuoteEach(){
101      String[] searchStrings = ['xxxx','yyyy']
102      def target = 'in this age xxxx of yyyy affordable beauty, there was something \n xxxx heraldic'
103      assert ! DKStringUtil.quoteAllOccurrencesOfEach(null,null,DKStringUtil.Quote.DOUBLE)
104      assert ! DKStringUtil.quoteAllOccurrencesOfEach(null,searchStrings,DKStringUtil.Quote.DOUBLE)
105      assert DKStringUtil.quoteAllOccurrencesOfEach(target,null,DKStringUtil.Quote.DOUBLE) == target
106      assert DKStringUtil.quoteAllOccurrencesOfEach(target,searchStrings,DKStringUtil.Quote.DOUBLE) == 'in this age "xxxx" of "yyyy" affordable beauty, there was something \n "xxxx" heraldic'
107   }
108   
109   public void testUnquote() {
110      assert ! DKStringUtil.unquote((String)null,DKStringUtil.Quote.DOUBLE)
111      assert DKStringUtil.unquote('hello',DKStringUtil.Quote.DOUBLE) == 'hello'
112      assert DKStringUtil.unquote('"hello',DKStringUtil.Quote.DOUBLE) == '"hello'
113      assert DKStringUtil.unquote('"hello"',DKStringUtil.Quote.DOUBLE) == 'hello'
114      assert DKStringUtil.unquote('""',DKStringUtil.Quote.DOUBLE) == ''
115   }
116   
117   public void testReplaceEach(){
118      def source =   '''Beware the Jabberwock, my son!
119                        The jaws that bite, the claws that catch!
120                        Beware the Jubjub bird, and shun
121                        The frumious Bandersnatch!'''
122      def expected =   '''swear the Jabberwock, my son!
123                        The jaws that bite, the claws that catch!
124                        swear the Jubjub bird, and shun
125                        The frumpy Bandersnatch!'''
126      def substitutions = ['Beware':'swear', 'frumious':'frumpy']
127      
128      assert DKStringUtil.replaceEach(source, substitutions) == expected
129      
130      source =   '''this is my test @TargetDatabase@'''
131      expected =   '''this is my test mem:testcase;DB_CLOSE_DELAY=-1'''
132      substitutions = [ : ]
133      substitutions.put(TestCaseRunner.TARGET_DATABASE_TOKEN,TestCaseRunner.DEFAULT_TESTCASE_DATABASE )
134      assert DKStringUtil.replaceEach(source, substitutions) == expected
135   }
136}