KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > jndi > TestPropertiesLoader


1 /**
2  * Copyright (C) 2001-2004
3  * - France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: P. Dechamboux, S. Chassande-Barrioz
20  *
21  */

22
23 package org.objectweb.perseus.jndi;
24
25 import junit.framework.TestCase;
26 import org.objectweb.perseus.jndi.PropertiesLoader;
27
28 import java.util.Properties JavaDoc;
29
30 /**
31  *
32  */

33 public class TestPropertiesLoader extends TestCase {
34     private static final String JavaDoc PROPFILE = "propfile";
35     private static String JavaDoc propertiesFileName = null;
36     private static Class JavaDoc contextImpl = null;
37
38     protected PropertiesLoader pl = null;
39     
40     /**
41      * Constructor in according to the JUnit constraints
42      */

43     public TestPropertiesLoader(String JavaDoc tn) {
44         super(tn);
45     }
46
47     /**
48      * Initialisation sequence executed before each test case.
49      */

50     protected void setUp() {
51         pl=null;
52         propertiesFileName = System.getProperty(PROPFILE);
53         contextImpl = ContextImpl.class;
54     }
55
56     public void testSimple() {
57         pl = new PropertiesLoader();
58         try {
59             pl.load(propertiesFileName);
60         } catch (Exception JavaDoc e) {
61             System.out.println(
62             "The simple test of PropertiesLoader throws the following error:");
63             e.printStackTrace();
64             assertTrue(false);
65         }
66         assertTrue(true);
67     }
68
69     public void testPropertiesField() {
70         pl = new PropertiesLoader();
71         try {
72             pl.load(propertiesFileName);
73         } catch (Exception JavaDoc e) {
74             e.printStackTrace();
75             assertTrue(false);
76         }
77         Properties JavaDoc p = pl.getProperties();
78         assertNotNull(p);
79         assertEquals("1", p.getProperty("Field1"));
80         assertEquals("200000", p.getProperty("Field2"));
81         assertEquals("azerty", p.getProperty("Field3"));
82         assertEquals("0.45", p.getProperty("Field4"));
83     }
84
85     public void testContextField() {
86         try {
87             pl = new PropertiesLoader(contextImpl);
88         } catch (Exception JavaDoc e) {
89             e.printStackTrace();
90             assertTrue("The class "+contextImpl+" doesn't be found", false);
91         }
92         try {
93             pl.load(propertiesFileName);
94         } catch (Exception JavaDoc e) {
95             e.printStackTrace();
96             assertTrue(false);
97         }
98         assertNotNull("Unexpected null return from the getContext method",
99             pl.getContext());
100         try {
101             assertTrue("1".equals(pl.getContext().lookup("Field1")));
102             assertTrue("200000".equals(pl.getContext().lookup("Field2")));
103             assertTrue("azerty".equals(pl.getContext().lookup("Field3")));
104             assertTrue("0.45".equals(pl.getContext().lookup("Field4")));
105         } catch (Exception JavaDoc e) {
106             e.printStackTrace();
107             assertTrue("A field is unreachable in context", false);
108         }
109     }
110
111     public void testInvokeSetters() {
112         try {
113             pl = new PropertiesLoader(contextImpl);
114         } catch (Exception JavaDoc e) {
115             e.printStackTrace();
116             assertTrue("The class "+contextImpl+" doesn't be found", false);
117         }
118         try {
119             pl.load(propertiesFileName);
120         } catch (Exception JavaDoc e) {
121             e.printStackTrace();
122             assertTrue(false);
123         }
124         try {
125             pl.invokeSetters(new MyObject());
126             assertTrue(true);
127         } catch (Exception JavaDoc e) {
128             e.printStackTrace();
129             assertTrue(e.getMessage(), false);
130         }
131     }
132
133     private class MyObject {
134         public boolean field1set = false;
135         public boolean field2set = false;
136         public boolean field3set = false;
137         public boolean field4set = false;
138
139         public void setField1(Integer JavaDoc i) {
140             assertTrue(new Integer JavaDoc(1).equals(i));
141             field1set = true;
142         }
143         public void setField2(Long JavaDoc i) {
144             assertTrue(new Long JavaDoc(200000).equals(i));
145             field2set = true;
146         }
147         public void setField3(String JavaDoc s) {
148             assertTrue("azerty".equals(s));
149             field3set = true;
150         }
151         public void setField4(Float JavaDoc f) {
152             assertTrue(new Float JavaDoc(0.45).equals(f));
153             field3set = true;
154         }
155     }
156 }
157
Popular Tags