|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2013 Slava Semushin <slava.semushin@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.util |
| 19 | + |
| 20 | +import spock.lang.Specification |
| 21 | + |
| 22 | +import ru.mystamps.web.entity.GibbonsCatalog |
| 23 | +import ru.mystamps.web.entity.MichelCatalog |
| 24 | +import ru.mystamps.web.entity.ScottCatalog |
| 25 | +import ru.mystamps.web.entity.YvertCatalog |
| 26 | + |
| 27 | +public class CatalogUtilsTest extends Specification { |
| 28 | + |
| 29 | + // |
| 30 | + // Tests for toShortForm() |
| 31 | + // |
| 32 | + |
| 33 | + def "toShortForm() should throw exception if numbers is null"() { |
| 34 | + when: |
| 35 | + CatalogUtils.toShortForm(null) |
| 36 | + then: |
| 37 | + thrown IllegalArgumentException |
| 38 | + } |
| 39 | + |
| 40 | + def "toShortForm() should return empty string for empty numbers"() { |
| 41 | + given: |
| 42 | + Set<MichelCatalog> empty = Collections.<MichelCatalog>emptySet() |
| 43 | + when: |
| 44 | + String numbers = CatalogUtils.toShortForm(empty) |
| 45 | + then: |
| 46 | + numbers == "" |
| 47 | + } |
| 48 | + |
| 49 | + def "toShortForm() should return single number as is"() { |
| 50 | + given: |
| 51 | + Set<MichelCatalog> singleNumber = Collections.singleton(new MichelCatalog("1")) |
| 52 | + when: |
| 53 | + String numbers = CatalogUtils.toShortForm(singleNumber) |
| 54 | + then: |
| 55 | + numbers == "1" |
| 56 | + } |
| 57 | + |
| 58 | + def "toShortForm() should return pair of numbers as comma separated"() { |
| 59 | + given: |
| 60 | + Set<MichelCatalog> setOfNumbers = new LinkedHashSet<MichelCatalog>() |
| 61 | + setOfNumbers.add(new MichelCatalog("1")) |
| 62 | + setOfNumbers.add(new MichelCatalog("2")) |
| 63 | + when: |
| 64 | + String numbers = CatalogUtils.toShortForm(setOfNumbers) |
| 65 | + then: |
| 66 | + numbers == "1, 2" |
| 67 | + } |
| 68 | + |
| 69 | + def "toShortForm() should produce range for sequence"() { |
| 70 | + given: |
| 71 | + Set<MichelCatalog> setOfNumbers = new LinkedHashSet<MichelCatalog>() |
| 72 | + setOfNumbers.add(new MichelCatalog("1")) |
| 73 | + setOfNumbers.add(new MichelCatalog("2")) |
| 74 | + setOfNumbers.add(new MichelCatalog("3")) |
| 75 | + when: |
| 76 | + String numbers = CatalogUtils.toShortForm(setOfNumbers) |
| 77 | + then: |
| 78 | + numbers == "1-3" |
| 79 | + } |
| 80 | + |
| 81 | + def "toShortForm() should return comma separated numbers if they are not a sequence"() { |
| 82 | + given: |
| 83 | + Set<MichelCatalog> setOfNumbers = new LinkedHashSet<MichelCatalog>() |
| 84 | + setOfNumbers.add(new MichelCatalog("1")) |
| 85 | + setOfNumbers.add(new MichelCatalog("2")) |
| 86 | + setOfNumbers.add(new MichelCatalog("4")) |
| 87 | + setOfNumbers.add(new MichelCatalog("5")) |
| 88 | + when: |
| 89 | + String numbers = CatalogUtils.toShortForm(setOfNumbers) |
| 90 | + then: |
| 91 | + numbers == "1, 2, 4, 5" |
| 92 | + } |
| 93 | + |
| 94 | + def "toShortForm() should produce two ranges for two sequences"() { |
| 95 | + given: |
| 96 | + Set<MichelCatalog> setOfNumbers = new LinkedHashSet<MichelCatalog>() |
| 97 | + setOfNumbers.add(new MichelCatalog("1")) |
| 98 | + setOfNumbers.add(new MichelCatalog("2")) |
| 99 | + setOfNumbers.add(new MichelCatalog("3")) |
| 100 | + setOfNumbers.add(new MichelCatalog("10")) |
| 101 | + setOfNumbers.add(new MichelCatalog("19")) |
| 102 | + setOfNumbers.add(new MichelCatalog("20")) |
| 103 | + setOfNumbers.add(new MichelCatalog("21")) |
| 104 | + when: |
| 105 | + String numbers = CatalogUtils.toShortForm(setOfNumbers) |
| 106 | + then: |
| 107 | + numbers == "1-3, 10, 19-21" |
| 108 | + } |
| 109 | + |
| 110 | + // |
| 111 | + // Tests for fromString() |
| 112 | + // |
| 113 | + |
| 114 | + def "fromString() should return empty collection if catalog numbers is null"() { |
| 115 | + when: |
| 116 | + Set<MichelCatalog> numbers = CatalogUtils.fromString(null, MichelCatalog.class) |
| 117 | + then: |
| 118 | + numbers.isEmpty() |
| 119 | + } |
| 120 | + |
| 121 | + def "fromString() should return empty collection if catalog numbers is empty"() { |
| 122 | + when: |
| 123 | + Set<MichelCatalog> numbers = CatalogUtils.fromString("", MichelCatalog.class) |
| 124 | + then: |
| 125 | + numbers.isEmpty() |
| 126 | + } |
| 127 | + |
| 128 | + def "fromString() should throw exception if element class is null"() { |
| 129 | + when: |
| 130 | + CatalogUtils.fromString("1", null) |
| 131 | + then: |
| 132 | + thrown IllegalArgumentException |
| 133 | + } |
| 134 | + |
| 135 | + def "fromString() should return one element if catalog numbers contains one number"() { |
| 136 | + when: |
| 137 | + Set<MichelCatalog> numbers = CatalogUtils.fromString("1", MichelCatalog.class) |
| 138 | + then: |
| 139 | + numbers.size() == 1 |
| 140 | + } |
| 141 | + |
| 142 | + def "fromString() should return one element if catalog numbers contains extra comma"() { |
| 143 | + when: |
| 144 | + Set<MichelCatalog> numbers = CatalogUtils.fromString("1,", MichelCatalog.class) |
| 145 | + then: |
| 146 | + numbers.size() == 1 |
| 147 | + } |
| 148 | + |
| 149 | + def "fromString() should return two elements if catalog numbers contains two numbers"() { |
| 150 | + when: |
| 151 | + Set<MichelCatalog> numbers = CatalogUtils.fromString("1,2", MichelCatalog.class) |
| 152 | + then: |
| 153 | + numbers.size() == 2 |
| 154 | + } |
| 155 | + |
| 156 | + def "fromString() should throw exception if one of catalog numbers is a blank string"() { |
| 157 | + when: |
| 158 | + CatalogUtils.fromString("1, ", MichelCatalog) |
| 159 | + then: |
| 160 | + thrown IllegalStateException |
| 161 | + } |
| 162 | + |
| 163 | + // TODO: fromString() should convert Exception to RuntimeException |
| 164 | + // TODO: fromString() should throw RuntimeException as is |
| 165 | + |
| 166 | + def "fromString() should return set of MichelNumbers for appropriate element class"() { |
| 167 | + when: |
| 168 | + Set<?> numbers = CatalogUtils.fromString("1,2", MichelCatalog.class) |
| 169 | + then: |
| 170 | + numbers.each { |
| 171 | + assert it instanceof MichelCatalog |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + def "fromString() should return set of ScottNumbers for appropriate element class"() { |
| 176 | + when: |
| 177 | + Set<?> numbers = CatalogUtils.fromString("1,2", ScottCatalog.class) |
| 178 | + then: |
| 179 | + numbers.each { |
| 180 | + assert it instanceof ScottCatalog |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + def "fromString() should return set of YvertNumbers for appropriate element class"() { |
| 185 | + when: |
| 186 | + Set<?> numbers = CatalogUtils.fromString("1,2", YvertCatalog.class) |
| 187 | + then: |
| 188 | + numbers.each { |
| 189 | + assert it instanceof YvertCatalog |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + def "fromString() should return set of GibbonsNumbers for appropriate element class"() { |
| 194 | + when: |
| 195 | + Set<?> numbers = CatalogUtils.fromString("1,2", GibbonsCatalog.class) |
| 196 | + then: |
| 197 | + numbers.each { |
| 198 | + assert it instanceof GibbonsCatalog |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments