Statistics
| Revision:

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

History | View | Annotate | Download (4.48 KB)

1
package org.gvsig.tools.exception;
2

    
3
import junit.framework.TestCase;
4

    
5
public class BaseExceptionTest extends TestCase {
6

    
7
        protected void setUp() throws Exception {
8
                super.setUp();
9
        }
10

    
11
        protected void tearDown() throws Exception {
12
                super.tearDown();
13
        }
14
        
15
        public void testSimple(){
16
                try {
17
                        throw new NullPointerException("Excepcion de puntero nulo");
18
                } catch (NullPointerException e){
19
                    IBaseException de = createDriverException("SimpleDriver", e);
20
                        assertEquals("Error in the driver SimpleDrivers", de.getMessage());
21
                        assertEquals(
22
                    "Error in the driver SimpleDrivers\nExcepcion de puntero nulo",
23
                    de.getMessageStack());
24
                }
25
        }
26

    
27
        public void testSimpleLocalized(){
28
                class MyTranslator implements IExceptionTranslator {
29
                        public String getText(String clave) {
30
                                return clave.toUpperCase();
31
                        }
32
                }
33
                BaseException.setTranslator(new MyTranslator());
34
                try {
35
                        throw new NullPointerException("Excepcion de puntero nulo");
36
                } catch (NullPointerException e){
37
            IBaseException de = createDriverException("SimpleDriver", e);
38
                        assertEquals("ERROR_IN_THE_DRIVER_%(DRIVERNAME)S",de.getLocalizedMessage());
39
                        assertEquals(
40
                    "ERROR_IN_THE_DRIVER_%(DRIVERNAME)S\nExcepcion de puntero nulo",
41
                    de.getLocalizedMessageStack());
42
                }
43
                BaseException.setTranslator(null);
44
        }
45

    
46
        public void testSimple2(){
47
                try {
48
                        throw new NullPointerException("Excepcion de puntero nulo");
49
                } catch (NullPointerException e){
50
            IBaseException de = createBadDateException("SimpleDriver", e);
51
                        assertEquals("Driver SimpleDrivers: Formato de fecha incorrecto",
52
                    de.getMessage());
53
                        assertEquals(
54
                    "Driver SimpleDrivers: Formato de fecha incorrecto\nExcepcion de puntero nulo",
55
                    de.getMessageStack());
56
                }
57
        }
58

    
59
        public void testSimpleLocalized2(){
60
                class MyTranslator implements IExceptionTranslator {
61
                        public String getText(String clave) {
62
                                return clave.toUpperCase();
63
                        }
64
                }
65
                BaseException.setTranslator(new MyTranslator());
66
                try {
67
                        throw new NullPointerException("Excepcion de puntero nulo");
68
                } catch (NullPointerException e){
69
            IBaseException de = createBadDateException("SimpleDriver", e);
70
                        assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
71
                        assertEquals(
72
                    "DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo",
73
                    de.getLocalizedMessageStack());
74
                }
75
                BaseException.setTranslator(null);
76
        }
77

    
78
        public void testTranslatorWithoutInterface() {
79
                class MyTranslator {
80
                        public String getText(String clave) {
81
                                return clave.toUpperCase();
82
                        }
83
                }
84
                BaseException.setTranslator(new MyTranslator());
85
                try {
86
                        throw new NullPointerException("Excepcion de puntero nulo");
87
                } catch (NullPointerException e){
88
            IBaseException de = createBadDateException("SimpleDriver", e);
89
                        assertEquals("DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO",de.getLocalizedMessage());
90
                        assertEquals(
91
                    "DRIVER_%(DRIVERNAME)S_FORMATO_DE_FECHA_INCORRECTO\nExcepcion de puntero nulo",
92
                    de.getLocalizedMessageStack());
93
                }
94
                BaseException.setTranslator(null);
95
                
96
        }
97
        
98
    /**
99
     * Test method for
100
     * {@link org.gvsig.exceptions.ExceptionUtils#insertBlanksAtStart(java.lang.String, int)}
101
     * .
102
     */
103
    public void testInsertBlanksAtStart() {
104
        String src = "test";
105

    
106
        assertEquals("test", BaseException.insertBlanksAtStart(src, 0));
107

    
108
        assertEquals("test", BaseException.insertBlanksAtStart(src, -5));
109

    
110
        assertEquals("  test", BaseException.insertBlanksAtStart(src, 2));
111

    
112
        assertEquals("      test", BaseException.insertBlanksAtStart(src, 6));
113
    }
114
    
115
    protected IBaseException createDriverException(String driver,
116
            Throwable throwable) {
117
        return new DriverException(driver, throwable);
118
    }
119
    
120
    protected IBaseException createBadDateException(String driver,
121
            Throwable throwable) {
122
        return new BadDateException(driver, throwable);
123
    }
124

    
125
        class BadDateException extends DriverException {
126
                private static final long serialVersionUID = -8985920349210629998L;
127
            private static final String KEY = "Driver_%(driverName)s_Formato_de_fecha_incorrecto";
128

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

    
131
                public BadDateException(String driverName, Throwable cause) {
132
            super(driverName, MESSAGE, cause, KEY, serialVersionUID);
133
                }
134
        }
135
        
136
}