1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.XAJNDITest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.jdbcapi;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;
import javax.sql.XADataSource;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.J2EEDataSource;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.JDBCDataSource;
import org.apache.derbyTesting.junit.TestConfiguration;
public class XAJNDITest extends BaseJDBCTestCase {
private static String ldapServer;
private static String ldapPort;
private static String dnString;
private InitialDirContext ic = getInitialDirContext();
public XAJNDITest(String name) {
super(name);
}
public static Test suite() {
// the test requires XADataSource to run, so check for JDBC3 support
if (JDBC.vmSupportsJDBC3()) {
ldapServer=getSystemProperty("derbyTesting.ldapServer");
if (ldapServer == null || ldapServer.length() < 1) {
return new BaseTestSuite(
"XAJNDITest requires property derbyTesting.ldapServer " +
"set, eg: -DderbyTesting.ldapServer=myldapserver." +
"myorg.org");
}
ldapPort=getSystemProperty("derbyTesting.ldapPort");
if (ldapPort == null || ldapPort.length() < 1) {
return new BaseTestSuite(
"XAJNDITest requires property derbyTesting.ldapPort " +
"set, eg: -DderbyTesting.ldapPort=333");
}
dnString=getSystemProperty("derbyTesting.dnString");
if (dnString == null || dnString.length() < 1) {
return new BaseTestSuite(
"XAJNDITest requires property derbyTesting.dnString " +
"for setting o=, eg: -DderbyTesting.dnString=myJNDIstring");
}
return TestConfiguration.defaultSuite(XAJNDITest.class);
}
return new BaseTestSuite("XAJNDITest cannot run without XA support");
}
public void tearDown() throws Exception {
ldapServer=null;
ldapPort=null;
// need to hold on to dnString value and ic as the fixture runs
// twice (embedded & networkserver) and they're used inside it
super.tearDown();
}
private InitialDirContext getInitialDirContext()
{
try {
Hashtable<String, String> env = new Hashtable<String, String>();
// using properties - these will have been passed in.
String ldapContextFactory=getSystemProperty("derbyTesting.ldapContextFactory");
if (ldapContextFactory == null || ldapContextFactory.length() < 1)
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
else
env.put(Context.INITIAL_CONTEXT_FACTORY, ldapContextFactory);
env.put(Context.PROVIDER_URL, "ldap://" + ldapServer + ":" + ldapPort);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
return new InitialDirContext(env);
} catch (NamingException ne) {
fail("naming exception ");
return null;
}
}
public void testCompareXADataSourcewithJNDIDataSource()
throws Exception
{
XADataSource xads = J2EEDataSource.getXADataSource();
String dbName = TestConfiguration.getCurrent().getDefaultDatabaseName();
JDBCDataSource.setBeanProperty(xads, "databaseName", dbName);
JDBCDataSource.setBeanProperty(xads, "createDatabase", "create");
JDBCDataSource.setBeanProperty(xads, "description", "XA DataSource");
ic.rebind("cn=compareDS, o=" + dnString, xads);
javax.sql.XADataSource ads =
(javax.sql.XADataSource)ic.lookup("cn=compareDS, o=" + dnString);
// Embedded data sources implement equals(), so use it to check
// that the two data sources are equal.
if (usingEmbedded())
{
assertEquals(xads, ads);
}
// Client data sources don't implement equals(), so compare each
// property manually. And by the way, we don't trust that equals()
// in embedded data sources checks all the properties, so do a
// full check for embedded as well.
String[] orgprops = getPropertyBeanList(xads);
String[] bindprops = getPropertyBeanList(ads);
assertEquals(orgprops.length, bindprops.length);
// Check that all properties are equal.
for (int i=0;i<orgprops.length;i++){
assertEquals(orgprops[i], bindprops[i]);
assertEquals(
JDBCDataSource.getBeanProperty(xads, orgprops[i]),
JDBCDataSource.getBeanProperty(ads, bindprops[i]));
}
// modify something essential of the original XADataSource
JDBCDataSource.clearStringBeanProperty(xads, "createDatabase");
// Now the ads is no longer the same
assertFalse(xads.equals(ads));
}
/**
* Obtains a list of bean properties through reflection.
*
*
* @param ds the data source to investigate
* @return A list of bean property names.
*/
private static String[] getPropertyBeanList(Object ds) {
Method[] allMethods = ds.getClass().getMethods();
ArrayList<String> properties = new ArrayList<String>();
for (int i = 0; i < allMethods.length; i++) {
Method method = allMethods[i];
String methodName = method.getName();
// Need at least getXX
if (methodName.length() < 5 || !methodName.startsWith("get") ||
method.getParameterTypes().length != 0) {
continue;
}
Class rt = method.getReturnType();
if (rt.equals(Integer.TYPE) || rt.equals(String.class) ||
rt.equals(Boolean.TYPE) || rt.equals(Short.TYPE) ||
rt.equals(Long.TYPE)) {
// Valid Java Bean property.
// Convert name:
// getPassword -> password
// getRetrieveMessageText -> retrieveMessageText
String beanName = methodName.substring(3,4).toLowerCase()
+ methodName.substring(4);
properties.add(beanName);
} else {
assertFalse("Method '" + methodName + "' with primitive " +
"return type not supported - update test!!",
rt.isPrimitive());
}
}
return properties.toArray(new String[properties.size()]);
}
}
|