1 19 20 package org.netbeans.modules.tomcat5; 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.InputStream ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.net.URL ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import javax.enterprise.deploy.model.DDBean ; 31 import javax.enterprise.deploy.model.DDBeanRoot ; 32 import javax.enterprise.deploy.model.DeployableObject ; 33 import javax.enterprise.deploy.model.XpathListener ; 34 import javax.enterprise.deploy.model.exceptions.DDBeanCreateException; 35 import javax.enterprise.deploy.shared.ModuleType ; 36 import org.netbeans.api.db.explorer.ConnectionManager; 37 import org.netbeans.api.db.explorer.DatabaseConnection; 38 import org.netbeans.api.db.explorer.JDBCDriver; 39 import org.netbeans.junit.NbTestCase; 40 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 41 import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion; 42 import org.netbeans.modules.tomcat5.config.WebappConfiguration; 43 import org.netbeans.modules.tomcat5.config.gen.Context; 44 import org.netbeans.modules.tomcat5.config.gen.Parameter; 45 import org.netbeans.modules.tomcat5.config.gen.ResourceParams; 46 import org.netbeans.modules.tomcat5.util.TestBase; 47 48 52 public class WebappConfigurationTest extends TestBase { 53 54 final String DRIVER_NAME = "driverName"; 55 final String DRIVER_DISPLAY_NAME = "driverDisplayName"; 56 final String DRIVER_CLASS = "driverClass"; 57 final String USER = "user"; 58 final String PASSWORD = "password"; 59 final String DB_URL = "dburl"; 60 final String SCHEMA = "schema"; 61 62 public WebappConfigurationTest (java.lang.String testName) { 63 super (testName); 64 } 65 66 protected void setUp() throws Exception { 67 super.setUp(); 68 registerConnection(); 69 } 70 71 private void registerConnection() throws Exception { 72 JDBCDriver driver = JDBCDriver.create(DRIVER_NAME, DRIVER_DISPLAY_NAME, DRIVER_CLASS, new URL []{}); 73 DatabaseConnection dbCon = DatabaseConnection.create(driver, DB_URL, USER, SCHEMA, PASSWORD, true); 74 ConnectionManager.getDefault().addConnection(dbCon); 75 } 76 77 private void createJDBCReference(WebappConfiguration conf, String name, String url, 79 String username, String password, String driverClassName) throws Exception { 80 Method addResReference = WebappConfiguration.class.getDeclaredMethod( 81 "createJDBCReference", 82 new Class [] { 83 String .class, String .class, String .class, String .class, String .class 84 } 85 ); 86 addResReference.setAccessible(true); 87 addResReference.invoke(conf, new Object []{name, url, username, password, driverClassName}); 88 } 89 90 public void testAddResReference50() throws Exception { 91 92 final String [][] res = { 93 {"jdbc/myDatabase", "jdbc:derby://localhost:1527/sample", "app", "app", "org.apache.derby.jdbc.ClientDriver"}, 94 {"jdbc/myDatabase1", "jdbc:derby://localhost:1527/sample1", "app1", "app1", "org.apache.derby.jdbc.ClientDriver"}, 95 }; 96 97 DeployableObject depObj = new DeployableObjectImpl(); 98 WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_50); 99 clearWorkDir(); 100 101 conf.init(new File (getWorkDir(), "context.xml")); 102 103 String dbName = ConnectionManager.getDefault().getConnections()[0].getName(); 104 for (int i = 0; i < res.length; i++) { 105 createJDBCReference(conf, res[i][0], res[i][1], res[i][2], res[i][3], res[i][4]); 106 } 107 108 Context ctx = conf.getContext(); 109 boolean[] resource = ctx.getResource(); 110 for (int i = 0; i < res.length; i++) { 111 assertEquals("javax.sql.DataSource", ctx.getResourceType(i)); 112 assertEquals("Container", ctx.getResourceAuth(i)); 113 assertEquals(res[i][0], ctx.getResourceName(i)); 114 115 ResourceParams[] resourceParams = ctx.getResourceParams(); 116 assertEquals(res[i][0], resourceParams[i].getName()); 117 Map <String ,String > paramMap = new HashMap <String ,String >(); 118 for (Parameter param : resourceParams[i].getParameter()) { 119 paramMap.put(param.getName(), param.getValue()); 120 } 121 assertEquals(res[i][1], paramMap.get("url")); 122 assertEquals(res[i][2], paramMap.get("username")); 123 assertEquals(res[i][3], paramMap.get("password")); 124 assertEquals(res[i][4], paramMap.get("driverClassName")); 125 } 126 127 try { 129 createJDBCReference(conf, res[0][0], res[0][1], res[0][2], res[0][3], res[0][4]); 130 fail(); 131 } catch (InvocationTargetException e) { 132 assertTrue(e.getCause() instanceof DatasourceAlreadyExistsException); 133 } 134 } 135 136 public void testAddResReference55() throws Exception { 137 138 final String [][] res = { 139 {"jdbc/myDatabase", "jdbc:derby://localhost:1527/sample", "app", "app", "org.apache.derby.jdbc.ClientDriver"}, 140 {"jdbc/myDatabase1", "jdbc:derby://localhost:1527/sample1", "app1", "app1", "org.apache.derby.jdbc.ClientDriver"}, 141 }; 142 143 DeployableObject depObj = new DeployableObjectImpl(); 144 WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_55); 145 clearWorkDir(); 146 conf.init(new File (getWorkDir(), "context.xml")); 147 148 for (int i = 0; i < res.length; i++) { 149 createJDBCReference(conf, res[i][0], res[i][1], res[i][2], res[i][3], res[i][4]); 150 } 151 152 Context ctx = conf.getContext(); 153 boolean[] resource = ctx.getResource(); 154 for (int i = 0; i < res.length; i++) { 155 assertEquals("javax.sql.DataSource", ctx.getResourceType(i)); 156 assertEquals(res[i][0], ctx.getResourceName(i)); 157 assertEquals(res[i][1], ctx.getResourceUrl(i)); 158 assertEquals(res[i][2], ctx.getResourceUsername(i)); 159 assertEquals(res[i][3], ctx.getResourcePassword(i)); 160 assertEquals(res[i][4], ctx.getResourceDriverClassName(i)); 161 } 162 163 try { 165 createJDBCReference(conf, res[0][0], res[0][1], res[0][2], res[0][3], res[0][4]); 166 fail(); 167 } catch (InvocationTargetException e) { 168 assertTrue(e.getCause() instanceof DatasourceAlreadyExistsException); 169 } 170 } 171 172 173 public void testSetContextPath50() throws Exception { 174 DeployableObject depObj = new DeployableObjectImpl(); 175 WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_50); 176 clearWorkDir(); 177 conf.init(new File (getWorkDir(), "context.xml")); 178 String [][] res = { 179 {"/test", "test."}, 180 {"/test/test", "test_test."}, 181 {"", "ROOT."}, 182 }; 183 for (int i = 0; i < res.length; i++) { 184 conf.setContextPath(res[i][0]); 185 assertEquals(res[i][0], conf.getContextPath()); 186 assertEquals(res[i][1], conf.getContext().getLoggerPrefix()); 187 } 188 } 189 190 public void testSetContextPath55() throws Exception { 191 DeployableObject depObj = new DeployableObjectImpl(); 192 WebappConfiguration conf = new WebappConfiguration(depObj, TomcatVersion.TOMCAT_55); 193 clearWorkDir(); 194 conf.init(new File (getWorkDir(), "context.xml")); 195 String [] res = { 196 "/test", 197 "/test/test", 198 "", 199 }; 200 for (int i = 0; i < res.length; i++) { 201 conf.setContextPath(res[i]); 202 assertEquals(res[i], conf.getContextPath()); 203 } 204 } 205 206 private static class DeployableObjectImpl implements DeployableObject { 207 public ModuleType getType() { 208 return null; 209 } 210 211 public DDBeanRoot getDDBeanRoot() { 212 return new DDBeanRootImpl(); 213 } 214 215 public DDBean [] getChildBean(String string) { 216 return null; 217 } 218 219 public String [] getText(String string) { 220 return null; 221 } 222 223 public Class getClassFromScope(String string) { 224 return null; 225 } 226 227 public String getModuleDTDVersion() { 228 return null; 229 } 230 231 public DDBeanRoot getDDBeanRoot(String string) throws FileNotFoundException , DDBeanCreateException { 232 return null; 233 } 234 235 public Enumeration entries() { 236 return null; 237 } 238 239 public InputStream getEntry(String string) { 240 return null; 241 } 242 243 }; 244 245 private static class DDBeanRootImpl implements DDBeanRoot { 246 public ModuleType getType() { 247 return null; 248 } 249 250 public DeployableObject getDeployableObject() { 251 return null; 252 } 253 254 public String getModuleDTDVersion() { 255 return null; 256 } 257 258 public String getDDBeanRootVersion() { 259 return null; 260 } 261 262 public String getXpath() { 263 return null; 264 } 265 266 public String getFilename() { 267 return null; 268 } 269 270 public String getText() { 271 return null; 272 } 273 274 public String getId() { 275 return null; 276 } 277 278 public DDBeanRoot getRoot() { 279 return null; 280 } 281 282 public DDBean [] getChildBean(String string) { 283 return null; 284 } 285 286 public String [] getText(String string) { 287 return null; 288 } 289 290 public void addXpathListener(String string, XpathListener xpathListener) { 291 } 292 293 public void removeXpathListener(String string, XpathListener xpathListener) { 294 } 295 296 public String [] getAttributeNames() { 297 return null; 298 } 299 300 public String getAttributeValue(String string) { 301 return null; 302 } 303 }; 304 } 305 | Popular Tags |