KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > driver > CjdbcUrlTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.driver;
26
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import org.objectweb.cjdbc.driver.CjdbcUrl;
36 import org.objectweb.cjdbc.driver.ControllerInfo;
37 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
38 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader;
39
40 /**
41  * <code>CjdbcUrl</code> test class.
42  *
43  * @author <a HREF="mailto:Mathieu.Peltier@emicnetworks.com">Mathieu Peltier
44  * </a>
45  * @see org.objectweb.cjdbc.driver.Driver
46  */

47 public class CjdbcUrlTest extends NoTemplate
48 {
49   /** File name containing the C-JDBC URLs to test. */
50   public static final String JavaDoc URLS_FILE = getTextPath("urls.txt");
51
52   /** Empty params token to use in the URLs file. */
53   public static final String JavaDoc EMPTY_PARAMS = "<empty>";
54
55   /** Driver to test. */
56   private CjdbcUrl cjdbcUrl;
57
58   /**
59    * @see org.objectweb.cjdbc.driver.CjdbcUrl#parseUrl()
60    */

61   public void testParseUrl()
62   {
63     // parse urls file
64
ArrayList JavaDoc results = new ArrayList JavaDoc();
65
66     String JavaDoc url = null;
67     try
68     {
69       File JavaDoc file = new File JavaDoc(URLS_FILE);
70       MyBufferedReader in = new MyBufferedReader(new FileReader JavaDoc(file), "URLs");
71
72       String JavaDoc line;
73       String JavaDoc databaseName = null;
74       String JavaDoc controllerList = null;
75       String JavaDoc paramList = null;
76
77       while ((line = in.readLine()) != null)
78       {
79         if (line.equals("") || line.startsWith("//"))
80           continue;
81
82         url = line;
83         if (in.readBoolean())
84         {
85           databaseName = in.readString("database name");
86           controllerList = in.readString("controller list");
87           paramList = in.readString("param list");
88           if (paramList.equals(EMPTY_PARAMS))
89           {
90             paramList = "";
91           }
92           results.add(new ParsingResult(url, databaseName, controllerList,
93               paramList));
94         }
95       }
96     }
97     catch (IOException JavaDoc e)
98     {
99       if (url == null)
100         fail("An error occurs while parsing urls file: " + e);
101       else
102         fail("An error occurs while parsing urls file: " + e + " (URL: '" + url
103             + "')");
104     }
105
106     // perform test on urls
107
Iterator JavaDoc it = results.iterator();
108     ParsingResult result;
109     ControllerInfo[] controllerList;
110
111     int countTest = 0;
112     while (it.hasNext())
113     {
114       result = (ParsingResult) it.next();
115       if (result.isValid)
116       {
117         try
118         {
119           System.out.println("Test[" + (countTest++) + "]:" + result.url);
120           cjdbcUrl = new CjdbcUrl(result.url);
121           assertEquals("Incorrect database name", result.databaseName, cjdbcUrl
122               .getDatabaseName());
123
124           controllerList = cjdbcUrl.getControllerList();
125           for (int i = 0; i < controllerList.length; i++)
126           {
127             assertEquals("Incorrect hostname in controller list",
128                 result.hosts[i], controllerList[i].getHostname());
129             assertEquals("Incorrect hostname in controller list",
130                 result.ports[i], controllerList[i].getPort());
131           }
132
133           assertEquals("Params list was different than expected", cjdbcUrl
134               .getParameters(), result.parameters);
135         }
136         catch (Exception JavaDoc e)
137         {
138           fail("Unexpected exception thrown: " + e);
139         }
140       }
141       else
142       {
143         try
144         {
145           cjdbcUrl = new CjdbcUrl(result.url);
146           fail("Exception not thrown with illegal URL '" + result.url + "'");
147         }
148         catch (Exception JavaDoc e)
149         {
150         }
151       }
152     }
153   }
154
155   /**
156    * Stores the expected result of the call to
157    * {@link org.objectweb.cjdbc.driver.CjdbcUrl#parseUrl()}method.
158    *
159    * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
160    */

161   protected class ParsingResult
162   {
163     /** URL to test. */
164     private String JavaDoc url;
165
166     /** <code>true</code> if the URL is valid. */
167     private boolean isValid;
168
169     /** Database name. */
170     private String JavaDoc databaseName;
171
172     /** Controllers list. */
173     private String JavaDoc[] hosts;
174     private int[] ports;
175
176     /** Parameters list */
177     private Hashtable JavaDoc parameters;
178
179     /**
180      * Creates a new <code>ParsingResult</code> instance for bad URL.
181      *
182      * @param url URL to test.
183      */

184     protected ParsingResult(String JavaDoc url)
185     {
186       this.url = url;
187       this.isValid = false;
188     }
189
190     /**
191      * Creates a new <code>ParsingResult</code> instance for valid URL.
192      *
193      * @param url URL to test.
194      * @param databaseName database name.
195      * @param controllerList controllers list.
196      */

197     protected ParsingResult(String JavaDoc url, String JavaDoc databaseName,
198         String JavaDoc controllerList, String JavaDoc paramList)
199     {
200       this.url = url;
201       this.isValid = true;
202       this.databaseName = databaseName;
203
204       // parse controller list
205
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(controllerList, " ");
206       hosts = new String JavaDoc[tokenizer.countTokens()];
207       ports = new int[tokenizer.countTokens()];
208       int i = 0;
209       while (tokenizer.hasMoreTokens())
210       {
211         StringTokenizer JavaDoc hostPort = new StringTokenizer JavaDoc(tokenizer.nextToken(),
212             ":");
213         hosts[i] = hostPort.nextToken();
214         ports[i] = Integer.parseInt(hostPort.nextToken());
215         i++;
216       }
217
218       // parse param list
219
parameters = new Hashtable JavaDoc();
220       tokenizer = new StringTokenizer JavaDoc(paramList, " ");
221       while (tokenizer.hasMoreTokens())
222       {
223         StringTokenizer JavaDoc pp = new StringTokenizer JavaDoc(tokenizer.nextToken(), "=");
224         if (pp.hasMoreTokens())
225         {
226           String JavaDoc param = pp.nextToken();
227           String JavaDoc value = "";
228           if (pp.hasMoreTokens())
229             value = pp.nextToken();
230           parameters.put(param, value);
231         }
232       }
233     }
234   }
235 }
236
Popular Tags