svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / test / src / org / gvsig / i18n / utils / TestProperties.java @ 40559
History | View | Annotate | Download (6.83 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.i18n.utils; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.FileInputStream; |
28 |
import java.io.FileNotFoundException; |
29 |
import java.io.FileOutputStream; |
30 |
import java.io.IOException; |
31 |
import java.io.InputStream; |
32 |
import java.io.OutputStream; |
33 |
|
34 |
import junit.framework.TestCase; |
35 |
|
36 |
/**
|
37 |
* @author cesar
|
38 |
*
|
39 |
*/
|
40 |
public class TestProperties extends TestCase { |
41 |
static final String fileName = "TestProperties-file"; |
42 |
InputStream is;
|
43 |
OutputStream os;
|
44 |
|
45 |
public void initOutputStream() { |
46 |
try {
|
47 |
os = new FileOutputStream(fileName); |
48 |
} catch (FileNotFoundException e) { |
49 |
TestCase.fail(e.getLocalizedMessage()); |
50 |
} |
51 |
} |
52 |
|
53 |
public void discardOutputStream() { |
54 |
try {
|
55 |
os.close(); |
56 |
File file = new File(fileName); |
57 |
file.delete(); |
58 |
} catch (IOException e) { |
59 |
TestCase.fail(e.getLocalizedMessage()); |
60 |
} |
61 |
} |
62 |
|
63 |
public void initInputStream() { |
64 |
try {
|
65 |
is = new FileInputStream(fileName); |
66 |
} catch (FileNotFoundException e) { |
67 |
TestCase.fail(e.getLocalizedMessage()); |
68 |
} |
69 |
} |
70 |
|
71 |
public void discardInputStream() { |
72 |
try {
|
73 |
is.close(); |
74 |
} catch (IOException e) { |
75 |
TestCase.fail(e.getLocalizedMessage()); |
76 |
} |
77 |
} |
78 |
|
79 |
public void testOrderedPropertiesBasic() { |
80 |
// write file
|
81 |
OrderedProperties props1 = new OrderedProperties();
|
82 |
props1.setProperty("prueba", "hola"); |
83 |
props1.setProperty("prueba2", "holas"); |
84 |
props1.setProperty("abrir", "cerrar"); |
85 |
props1.setProperty("puerta", "hembrilla"); |
86 |
props1.setProperty("12libros", "quijote"); |
87 |
props1.setProperty("ca?uto", "\u20acuropeo"); |
88 |
props1.setProperty("confli=cto", "amigo"); |
89 |
props1.setProperty(" confli = :cto mayor ", " peque?o conflicto"); |
90 |
initOutputStream(); |
91 |
try {
|
92 |
props1.store(os, "header");
|
93 |
} catch (IOException e) { |
94 |
TestCase.fail(e.getLocalizedMessage()); |
95 |
} |
96 |
|
97 |
// read file
|
98 |
OrderedProperties props2 = new OrderedProperties();
|
99 |
initInputStream(); |
100 |
try {
|
101 |
props2.load(is); |
102 |
} catch (IOException e) { |
103 |
TestCase.fail(e.getLocalizedMessage()); |
104 |
} |
105 |
TestCase.assertEquals("\u20acuropeo", props2.getProperty("ca?uto")); |
106 |
TestCase.assertEquals("amigo", props2.getProperty("confli=cto")); |
107 |
TestCase.assertEquals(" peque?o conflicto", props2.getProperty(" confli = :cto mayor ")); |
108 |
TestCase.assertEquals("hola", props2.getProperty("prueba")); |
109 |
TestCase.assertEquals("holas", props2.getProperty("prueba2")); |
110 |
TestCase.assertEquals("cerrar", props2.getProperty("abrir")); |
111 |
TestCase.assertEquals("hembrilla", props2.getProperty("puerta")); |
112 |
TestCase.assertEquals("quijote", props2.getProperty("12libros")); |
113 |
discardInputStream(); |
114 |
discardOutputStream(); |
115 |
} |
116 |
|
117 |
|
118 |
public void testOrderedProperties() { |
119 |
// write file
|
120 |
OrderedProperties props1 = new OrderedProperties();
|
121 |
props1.setProperty("prueba", "hola"); |
122 |
props1.setProperty("prueba2", "holas"); |
123 |
props1.setProperty("ca?uto", "\u20acuropeo"); |
124 |
props1.setProperty("confli=cto", "amigo"); |
125 |
props1.setProperty(" conflis = :cto mayor ", " peque?o conflicto"); |
126 |
props1.setProperty("abrir", "cerrar"); |
127 |
props1.setProperty("puerta", "hembrilla"); |
128 |
props1.setProperty("12libros", "quijote"); |
129 |
initOutputStream(); |
130 |
try {
|
131 |
props1.store(os, "header", "8859_15"); |
132 |
} catch (IOException e) { |
133 |
TestCase.fail(e.getLocalizedMessage()); |
134 |
} |
135 |
|
136 |
// read file
|
137 |
OrderedProperties props2 = new OrderedProperties();
|
138 |
initInputStream(); |
139 |
try {
|
140 |
props2.load(is, "8859_15");
|
141 |
} catch (IOException e) { |
142 |
TestCase.fail(e.getLocalizedMessage()); |
143 |
} |
144 |
// estos deber?an salir igual
|
145 |
TestCase.assertEquals("hola", props2.getProperty("prueba")); |
146 |
TestCase.assertEquals("holas", props2.getProperty("prueba2")); |
147 |
TestCase.assertEquals("cerrar", props2.getProperty("abrir")); |
148 |
TestCase.assertEquals("hembrilla", props2.getProperty("puerta")); |
149 |
TestCase.assertEquals("quijote", props2.getProperty("12libros")); |
150 |
// con estos en cambio no obtenemos los mismos valores que escribimos anteriormente. Es normal, porque incluimos
|
151 |
// caracteres especiales como ":" y "=". Para manejarlos correctamente necesitamos usas los m?todos load/store
|
152 |
// sin encoding, los cuales s? escapan los caracteres especiales
|
153 |
TestCase.assertEquals("\u20acuropeo", props2.getProperty("ca?uto")); |
154 |
TestCase.assertEquals("cto=amigo", props2.getProperty("confli")); |
155 |
TestCase.assertEquals(":cto mayor = peque?o conflicto", props2.getProperty("conflis")); |
156 |
|
157 |
discardInputStream(); |
158 |
discardOutputStream(); |
159 |
|
160 |
// write file
|
161 |
props1 = new OrderedProperties();
|
162 |
props1.setProperty("Prueba", "hola"); |
163 |
props1.setProperty("prueba2", "holas"); |
164 |
props1.setProperty("ca?uto", "\u20acuropeo"); |
165 |
props1.setProperty("confli=cto", "amigo"); |
166 |
props1.setProperty(" conflis = :cto mayor ", " peque?o conflicto"); |
167 |
props1.setProperty("abrir", "cerrar"); |
168 |
props1.setProperty("puerta", "hembrilla"); |
169 |
props1.setProperty("12libros", "quijote"); |
170 |
initOutputStream(); |
171 |
try {
|
172 |
props1.store(os, "header", "UTF8"); |
173 |
} catch (IOException e) { |
174 |
TestCase.fail(e.getLocalizedMessage()); |
175 |
} |
176 |
|
177 |
// read file
|
178 |
props2 = new OrderedProperties();
|
179 |
initInputStream(); |
180 |
try {
|
181 |
props2.load(is, "UTF8");
|
182 |
} catch (IOException e) { |
183 |
TestCase.fail(e.getLocalizedMessage()); |
184 |
} |
185 |
// estos deber?an salir igual
|
186 |
TestCase.assertEquals("hola", props2.getProperty("Prueba")); |
187 |
TestCase.assertEquals("holas", props2.getProperty("prueba2")); |
188 |
TestCase.assertEquals("cerrar", props2.getProperty("abrir")); |
189 |
TestCase.assertEquals("hembrilla", props2.getProperty("puerta")); |
190 |
TestCase.assertEquals("quijote", props2.getProperty("12libros")); |
191 |
// con estos en cambio no obtenemos los mismos valores que escribimos anteriormente. Es normal, porque incluimos
|
192 |
// caracteres especiales como ":" y "=". Para manejarlos correctamente necesitamos usas los m?todos load/store
|
193 |
// sin encoding, los cuales s? escapan los caracteres especiales
|
194 |
TestCase.assertEquals("\u20acuropeo", props2.getProperty("ca?uto")); |
195 |
TestCase.assertEquals("cto=amigo", props2.getProperty("confli")); |
196 |
TestCase.assertEquals(":cto mayor = peque?o conflicto", props2.getProperty("conflis")); |
197 |
|
198 |
discardInputStream(); |
199 |
discardOutputStream(); |
200 |
} |
201 |
|
202 |
} |