svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.compat / org.gvsig.compat.se / src / test / java / org / gvsig / compat / lang / StringUtilsTestParent.java @ 40435
History | View | Annotate | Download (3.86 KB)
1 | 40435 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | |||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2008 {{Company}} {{Task}}
|
||
26 | */
|
||
27 | package org.gvsig.compat.lang; |
||
28 | |||
29 | import java.util.Arrays; |
||
30 | import java.util.List; |
||
31 | |||
32 | import junit.framework.TestCase; |
||
33 | |||
34 | /**
|
||
35 | * Parent class for Unit tests for {@link org.gvsig.compat.lang.StringUtils}
|
||
36 | * implementations.
|
||
37 | *
|
||
38 | * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
||
39 | */
|
||
40 | public abstract class StringUtilsTestParent extends TestCase { |
||
41 | |||
42 | private StringUtils utils;
|
||
43 | |||
44 | protected void setUp() throws Exception { |
||
45 | super.setUp();
|
||
46 | utils = createUtils(); |
||
47 | } |
||
48 | |||
49 | /**
|
||
50 | * Return a new instance of an implementation of the StringUtils interface.
|
||
51 | *
|
||
52 | * @return a new StringUtils instance
|
||
53 | */
|
||
54 | protected abstract StringUtils createUtils(); |
||
55 | |||
56 | /**
|
||
57 | * Test method for
|
||
58 | * {@link org.gvsig.compat.se.lang.StandardStringUtils#split(java.lang.String, java.lang.String)}
|
||
59 | * .
|
||
60 | */
|
||
61 | public void testSplitString() { |
||
62 | // Check the result is the same as the returned with the String.split
|
||
63 | // method.
|
||
64 | List list1;
|
||
65 | List list2;
|
||
66 | |||
67 | String testString = "En un lugar de la Mancha"; |
||
68 | String regex = " "; |
||
69 | |||
70 | list1 = Arrays.asList(new String[] { "En", "un", "lugar", "de", "la", |
||
71 | "Mancha" });
|
||
72 | |||
73 | list2 = Arrays.asList(utils.split(testString, regex));
|
||
74 | |||
75 | assertEquals(list1, list2); |
||
76 | |||
77 | list1 = Arrays.asList(new String[] { "En", "un lugar de la Mancha" }); |
||
78 | |||
79 | list2 = Arrays.asList(utils.split(testString, regex, 2)); |
||
80 | |||
81 | assertEquals(list1, list2); |
||
82 | } |
||
83 | |||
84 | /**
|
||
85 | * Test method for
|
||
86 | * {@link org.gvsig.compat.se.lang.StandardStringUtils#replaceAll(java.lang.String, java.lang.String, java.lang.String)}
|
||
87 | * .
|
||
88 | */
|
||
89 | public void testReplaceAll() { |
||
90 | // Check the result is the same as the returned with the
|
||
91 | // String.replaceAll method.
|
||
92 | String testString = "En un lugar de la Mancha"; |
||
93 | String regex = " "; |
||
94 | String replacement = "_"; |
||
95 | |||
96 | assertEquals("En_un_lugar_de_la_Mancha",
|
||
97 | utils.replaceAll(testString, regex, replacement)); |
||
98 | |||
99 | // Check replaceFirst
|
||
100 | assertEquals("En_un lugar de la Mancha",
|
||
101 | utils.replaceFirst(testString, regex, replacement)); |
||
102 | } |
||
103 | |||
104 | /**
|
||
105 | * Test method for
|
||
106 | * {@link org.gvsig.compat.se.lang.StandardStringUtils#compare(String, String, boolean)}
|
||
107 | * .
|
||
108 | */
|
||
109 | public void testCompare() { |
||
110 | String avion = "Avi?n"; |
||
111 | String avionaccent = "Avion"; |
||
112 | String barco = "Barco"; |
||
113 | |||
114 | // Same value
|
||
115 | assertEquals(avion + " is not equal to " + avion, 0, |
||
116 | utils.compare(avion, avion, false));
|
||
117 | assertEquals(avion + " is not equal to " + avion, 0, |
||
118 | utils.compare(avion, avion, true));
|
||
119 | |||
120 | // Non localized comparison
|
||
121 | assertTrue(utils.compare(avion, barco, false) < 0); |
||
122 | assertTrue(utils.compare(avion, avionaccent, false) > 0); |
||
123 | assertTrue(utils.compare(barco, avion, false) > 0); |
||
124 | assertTrue(utils.compare(avionaccent, avion, false) < 0); |
||
125 | |||
126 | // Localized comparison, so accents must not be taken into account
|
||
127 | assertTrue(utils.compare(avion, barco, true) < 0); |
||
128 | assertEquals(0, utils.compare(avion, avionaccent, true)); |
||
129 | assertTrue(utils.compare(barco, avion, true) > 0); |
||
130 | assertEquals(0, utils.compare(avionaccent, avion, true)); |
||
131 | } |
||
132 | } |