1 24 25 package org.objectweb.cjdbc.scenario.standalone.driver; 26 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.util.ArrayList ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.StringTokenizer ; 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 47 public class CjdbcUrlTest extends NoTemplate 48 { 49 50 public static final String URLS_FILE = getTextPath("urls.txt"); 51 52 53 public static final String EMPTY_PARAMS = "<empty>"; 54 55 56 private CjdbcUrl cjdbcUrl; 57 58 61 public void testParseUrl() 62 { 63 ArrayList results = new ArrayList (); 65 66 String url = null; 67 try 68 { 69 File file = new File (URLS_FILE); 70 MyBufferedReader in = new MyBufferedReader(new FileReader (file), "URLs"); 71 72 String line; 73 String databaseName = null; 74 String controllerList = null; 75 String 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 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 Iterator 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 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 e) 149 { 150 } 151 } 152 } 153 } 154 155 161 protected class ParsingResult 162 { 163 164 private String url; 165 166 167 private boolean isValid; 168 169 170 private String databaseName; 171 172 173 private String [] hosts; 174 private int[] ports; 175 176 177 private Hashtable parameters; 178 179 184 protected ParsingResult(String url) 185 { 186 this.url = url; 187 this.isValid = false; 188 } 189 190 197 protected ParsingResult(String url, String databaseName, 198 String controllerList, String paramList) 199 { 200 this.url = url; 201 this.isValid = true; 202 this.databaseName = databaseName; 203 204 StringTokenizer tokenizer = new StringTokenizer (controllerList, " "); 206 hosts = new String [tokenizer.countTokens()]; 207 ports = new int[tokenizer.countTokens()]; 208 int i = 0; 209 while (tokenizer.hasMoreTokens()) 210 { 211 StringTokenizer hostPort = new StringTokenizer (tokenizer.nextToken(), 212 ":"); 213 hosts[i] = hostPort.nextToken(); 214 ports[i] = Integer.parseInt(hostPort.nextToken()); 215 i++; 216 } 217 218 parameters = new Hashtable (); 220 tokenizer = new StringTokenizer (paramList, " "); 221 while (tokenizer.hasMoreTokens()) 222 { 223 StringTokenizer pp = new StringTokenizer (tokenizer.nextToken(), "="); 224 if (pp.hasMoreTokens()) 225 { 226 String param = pp.nextToken(); 227 String value = ""; 228 if (pp.hasMoreTokens()) 229 value = pp.nextToken(); 230 parameters.put(param, value); 231 } 232 } 233 } 234 } 235 } 236 | Popular Tags |