Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libTools / src-test / org / gvsig / tools / exception / BaseExceptionTest.java @ 31544

History | View | Annotate | Download (5.27 KB)

1
/* 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
 */
26
package org.gvsig.tools.exception;
27

    
28
import junit.framework.TestCase;
29

    
30
public class BaseExceptionTest extends TestCase {
31

    
32
        protected void setUp() throws Exception {
33
                super.setUp();
34
        }
35

    
36
        protected void tearDown() throws Exception {
37
                super.tearDown();
38
        }
39
        
40
        public void testSimple(){
41
                try {
42
                        throw new NullPointerException("Excepcion de puntero nulo");
43
                } catch (NullPointerException e){
44
                    IBaseException de = createDriverException("SimpleDriver", e);
45
                        assertEquals("Error in the driver SimpleDrivers", de.getMessage());
46
                        assertEquals(
47
                    "Error in the driver SimpleDrivers\nExcepcion de puntero nulo",
48
                    de.getMessageStack());
49
                }
50
        }
51

    
52
        public void testSimpleLocalized(){
53
                class MyTranslator implements ExceptionTranslator {
54
                        public String getText(String clave) {
55
                                return clave.toUpperCase();
56
                        }
57
                }
58
                BaseException.setTranslator(new MyTranslator());
59
                try {
60
                        throw new NullPointerException("Excepcion de puntero nulo");
61
                } catch (NullPointerException e){
62
            IBaseException de = createDriverException("SimpleDriver", e);
63
                        assertEquals("ERROR_IN_THE_DRIVER_%(DRIVERNAME)S",de.getLocalizedMessage());
64
                        assertEquals(
65
                    "ERROR_IN_THE_DRIVER_%(DRIVERNAME)S\nExcepcion de puntero nulo",
66
                    de.getLocalizedMessageStack());
67
                }
68
                BaseException.setTranslator(null);
69
        }
70

    
71
        public void testSimple2(){
72
                try {
73
                        throw new NullPointerException("Excepcion de puntero nulo");
74
                } catch (NullPointerException e){
75
            IBaseException de = createBadDateException("SimpleDriver", e);
76
                        assertEquals("Driver SimpleDrivers: Formato de fecha incorrecto",
77
                    de.getMessage());
78
                        assertEquals(
79
                    "Driver SimpleDrivers: Formato de fecha incorrecto\nExcepcion de puntero nulo",
80
                    de.getMessageStack());
81
                }
82
        }
83

    
84
        public void testSimpleLocalized2(){
85
                class MyTranslator implements ExceptionTranslator {
86
                        public String getText(String clave) {
87
                                return clave.toUpperCase();
88
                        }
89
                }
90
                BaseException.setTranslator(new MyTranslator());
91
                try {
92
                        throw new NullPointerException("Excepcion de puntero nulo");
93
                } catch (NullPointerException e){
94
            IBaseException de = createBadDateException("SimpleDriver", e);
95
                        assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
96
                        assertEquals(
97
                    "DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo",
98
                    de.getLocalizedMessageStack());
99
                }
100
                BaseException.setTranslator(null);
101
        }
102

    
103
        public void testTranslatorWithoutInterface() {
104
                class MyTranslator {
105
                        public String getText(String clave) {
106
                                return clave.toUpperCase();
107
                        }
108
                }
109
                BaseException.setTranslator(new MyTranslator());
110
                try {
111
                        throw new NullPointerException("Excepcion de puntero nulo");
112
                } catch (NullPointerException e){
113
            IBaseException de = createBadDateException("SimpleDriver", e);
114
                        assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
115
                        assertEquals(
116
                    "DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo",
117
                    de.getLocalizedMessageStack());
118
                }
119
                BaseException.setTranslator(null);
120
                
121
        }
122
        
123
    public void testInsertBlanksAtStart() {
124
        String src = "test";
125

    
126
        assertEquals("test", BaseException.insertBlanksAtStart(src, 0));
127

    
128
        assertEquals("test", BaseException.insertBlanksAtStart(src, -5));
129

    
130
        assertEquals("  test", BaseException.insertBlanksAtStart(src, 2));
131

    
132
        assertEquals("      test", BaseException.insertBlanksAtStart(src, 6));
133
    }
134
    
135
    protected IBaseException createDriverException(String driver,
136
            Throwable throwable) {
137
        return new DriverException(driver, throwable);
138
    }
139
    
140
    protected IBaseException createBadDateException(String driver,
141
            Throwable throwable) {
142
        return new BadDateException(driver, throwable);
143
    }
144

    
145
        class BadDateException extends DriverException {
146
                private static final long serialVersionUID = -8985920349210629998L;
147
            private static final String KEY = "Driver_%(driverName)s_Formato_de_fecha_incorrecto";
148

    
149
            private static final String MESSAGE = "Driver %(driverName)s: Formato de fecha incorrecto";
150

    
151
                public BadDateException(String driverName, Throwable cause) {
152
            super(driverName, MESSAGE, cause, KEY, serialVersionUID);
153
                }
154
        }
155
        
156
}