Skip to content

Commit 923b435

Browse files
committed
CatalogUtilsTest: ported to Spock Framework.
Addressed to #151
1 parent 46de41f commit 923b435

File tree

3 files changed

+247
-175
lines changed

3 files changed

+247
-175
lines changed

pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
<testng.version>6.7</testng.version>
4040
<junit.version>4.10</junit.version>
4141
<mockito.version>1.9.5</mockito.version>
42+
<spock.version>0.7-groovy-2.0</spock.version>
43+
<groovy.version>2.0.8</groovy.version>
4244
<fest.assert.version>2.0M8</fest.assert.version>
4345

4446
<java.version>1.6</java.version>
@@ -52,6 +54,8 @@
5254
<native2ascii.plugin.version>1.0-beta-1</native2ascii.plugin.version>
5355
<tomcat.plugin.version>1.1</tomcat.plugin.version>
5456
<surefire.plugin.version>2.15</surefire.plugin.version>
57+
<gmaven.plugin.version>1.4</gmaven.plugin.version>
58+
<gmaven.runtime.plugin.version>1.5</gmaven.runtime.plugin.version>
5559
<failsafe.plugin.version>2.15</failsafe.plugin.version>
5660
<jetty.plugin.version>8.1.8.v20121106</jetty.plugin.version>
5761
<hibernate.plugin.version>3.0</hibernate.plugin.version>
@@ -263,6 +267,13 @@
263267
<scope>test</scope>
264268
</dependency>
265269

270+
<dependency>
271+
<groupId>org.spockframework</groupId>
272+
<artifactId>spock-core</artifactId>
273+
<version>${spock.version}</version>
274+
<scope>test</scope>
275+
</dependency>
276+
266277
<dependency>
267278
<groupId>org.easytesting</groupId>
268279
<artifactId>fest-assert-core</artifactId>
@@ -457,6 +468,41 @@
457468
</configuration>
458469
</plugin>
459470

471+
<plugin>
472+
<groupId>org.codehaus.gmaven</groupId>
473+
<artifactId>gmaven-plugin</artifactId>
474+
<version>${gmaven.plugin.version}</version>
475+
<configuration>
476+
<providerSelection>2.0</providerSelection>
477+
</configuration>
478+
<executions>
479+
<execution>
480+
<goals>
481+
<goal>compile</goal>
482+
<goal>testCompile</goal>
483+
</goals>
484+
</execution>
485+
</executions>
486+
<dependencies>
487+
<dependency>
488+
<groupId>org.codehaus.gmaven.runtime</groupId>
489+
<artifactId>gmaven-runtime-2.0</artifactId>
490+
<version>${gmaven.runtime.plugin.version}</version>
491+
<exclusions>
492+
<exclusion>
493+
<groupId>org.codehaus.groovy</groupId>
494+
<artifactId>groovy-all</artifactId>
495+
</exclusion>
496+
</exclusions>
497+
</dependency>
498+
<dependency>
499+
<groupId>org.codehaus.groovy</groupId>
500+
<artifactId>groovy-all</artifactId>
501+
<version>${groovy.version}</version>
502+
</dependency>
503+
</dependencies>
504+
</plugin>
505+
460506
<plugin>
461507
<groupId>org.apache.maven.plugins</groupId>
462508
<artifactId>maven-failsafe-plugin</artifactId>
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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

Comments
 (0)