refactor testcase

This commit is contained in:
wenshao
2012-08-31 21:49:41 +08:00
parent fd440a2fcf
commit b2e42115df
12 changed files with 37 additions and 63 deletions
@@ -0,0 +1,148 @@
package com.alibaba.druid.bvt.filter.config;
import com.alibaba.druid.filter.config.ConfigFileGenerator;
import com.alibaba.druid.filter.config.ConfigFilter;
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoader;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
import org.junit.Assert;
import org.junit.Test;
import java.sql.SQLException;
/**
* @author Jonas Yang
*/
public class ConfigFilterTest extends ConfigFileGenerator {
String encryptedString = "OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg==";
@Test
public void testInitRemoteConfigFile() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("config");
dataSource.setConnectionProperties("config.file=" + FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
try {
dataSource.init();
Assert.assertEquals("The username is " + dataSource.getUsername(), "test1", dataSource.getUsername());
} finally {
JdbcUtils.close(dataSource);
}
}
@Test
public void testInitRemoteConfigFileBySystemProperty() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("config");
System.setProperty(ConfigFilter.SYS_PROP_CONFIG_FILE, FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
try {
dataSource.init();
Assert.assertEquals("The username is " + dataSource.getUsername(), "test1", dataSource.getUsername());
} finally {
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_FILE);
JdbcUtils.close(dataSource);
}
}
@Test
public void testInitInvalidRemoteConfigFile() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("config");
dataSource.setConnectionProperties("config.file=abcdef");
try {
dataSource.init();
Assert.assertTrue("It is here", false);
} catch (IllegalArgumentException e) {
e.printStackTrace();
Assert.assertTrue(true);
} finally {
JdbcUtils.close(dataSource);
}
}
@Test
public void testInitDecrypt() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriver(MockDriver.instance);
dataSource.setUrl("");
dataSource.setUsername("test");
dataSource.setPassword(encryptedString);
dataSource.setFilters("config");
dataSource.setConnectionProperties("config.decrypt=RSA");
try {
dataSource.init();
Assert.assertEquals("The password is " + dataSource.getPassword() + ", is not xiaoyu", "xiaoyu", dataSource.getPassword());
} finally {
JdbcUtils.close(dataSource);
}
}
@Test
public void testInitInvalidDecrypt() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriver(MockDriver.instance);
dataSource.setUrl("");
dataSource.setUsername("test");
dataSource.setPassword(encryptedString);
dataSource.setFilters("config");
dataSource.setConnectionProperties("config.decrypt=ABCDEF");
try {
dataSource.init();
Assert.assertTrue("It is here", false);
} catch (IllegalArgumentException e) {
e.printStackTrace();
Assert.assertTrue(true);
} finally {
JdbcUtils.close(dataSource);
}
}
@Test
public void testInitRemoteConfigAndDecrypt() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("config");
dataSource.setConnectionProperties("config.decrypt=RSA;config.file=" + FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
try {
dataSource.init();
Assert.assertEquals("The password is " + dataSource.getPassword(), "xiaoyu", dataSource.getPassword());
} finally {
JdbcUtils.close(dataSource);
}
}
@Test
public void testNormalInit() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:oracle:thin:@");
try {
dataSource.init();
} finally {
JdbcUtils.close(dataSource);
}
}
@Test
public void testInvalidInit() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriver(MockDriver.instance);
dataSource.setFilters("config");
dataSource.setConnectionProperties("config.file=abcdefeg");
try {
dataSource.init();
Assert.assertTrue("Get here.", false);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(dataSource);
}
}
}
@@ -0,0 +1,57 @@
package com.alibaba.druid.bvt.filter.config;
import com.alibaba.druid.filter.config.loader.ConfigLoader;
import com.alibaba.druid.filter.config.loader.ConfigLoaderFactory;
import org.junit.Assert;
import org.junit.Test;
import java.util.Properties;
/**
* @author Jonas Yang
*/
public class ConfigLoaderFactoryTest {
@Test
public void testInitConfigLoader() {
ConfigLoader[] configLoaders = ConfigLoaderFactory.getConfigLoaders();
Assert.assertTrue("The size is " + configLoaders.length + " , not 2", configLoaders.length == 2);
}
@Test
public void testGetConfigLoader() {
ConfigLoader configLoader = ConfigLoaderFactory.getConfigLoader("abcde");
Assert.assertEquals("The config is " + configLoader, null, configLoader);
}
@Test
public void testAddConfigLoader() {
int size = ConfigLoaderFactory.getConfigLoaders().length;
ConfigLoaderFactory.addConfigLoader(new ConfigLoader() {
@Override
public String getId() {
return "My Test"; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Properties loadConfig(String protocol) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isSupported(String protocol) {
if (protocol.startsWith("abc")) {
return true;
}
return false; //To change body of implemented methods use File | Settings | File Templates.
}
});
Assert.assertEquals("The size is " + ConfigLoaderFactory.getConfigLoaders().length, ConfigLoaderFactory.getConfigLoaders().length, size + 1);
ConfigLoader configLoader = ConfigLoaderFactory.getConfigLoader("abc");
Assert.assertNotNull("The config loader is null", configLoader);
Assert.assertEquals("The config loader's id is " + configLoader.getId(), "My Test", configLoader.getId());
}
}
@@ -1,25 +0,0 @@
package com.alibaba.druid.bvt.filter.config;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import com.alibaba.druid.filter.config.ConfigFilterTest;
import com.alibaba.druid.filter.config.loader.ConfigLoaderFactoryTest;
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoaderTest;
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoaderTest1;
import com.alibaba.druid.filter.config.loader.impl.HttpConfigLoaderTest;
/**
* @author Jonas Yang
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
ConfigLoaderFactoryTest.class,
FileConfigLoaderTest.class,
FileConfigLoaderTest1.class,
HttpConfigLoaderTest.class,
ConfigFilterTest.class
})
public class ConfigTestSuite {
}
@@ -0,0 +1,22 @@
package com.alibaba.druid.bvt.filter.config;
import junit.framework.Assert;
import org.junit.Test;
import com.alibaba.druid.filter.config.security.decrypter.DecryptException;
import com.alibaba.druid.filter.config.security.decrypter.DecrypterFactory;
import com.alibaba.druid.filter.config.security.decrypter.RsaDecrypter;
/**
* @author Jonas Yang
*/
public class DecrypterFactoryTest {
@Test
public void testGetDecrypter() throws DecryptException {
Assert.assertNotNull(DecrypterFactory.getDecrypter("RSA"));
Assert.assertEquals(DecrypterFactory.getDecrypter("RSA").getClass(), RsaDecrypter.class);
Assert.assertNull(DecrypterFactory.getDecrypter("ajfdjksfklds;"));
}
}
@@ -1,17 +0,0 @@
package com.alibaba.druid.bvt.filter.config;
import com.alibaba.druid.filter.config.security.decrypter.DecrypterFactoryTest;
import com.alibaba.druid.filter.config.security.decrypter.RsaDecrypterTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* @author Jonas Yang
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
RsaDecrypterTest.class,
DecrypterFactoryTest.class
})
public class DecrypterTestSuite {
}
@@ -0,0 +1,29 @@
package com.alibaba.druid.bvt.filter.config;
import org.junit.Assert;
import org.junit.Test;
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoader;
/**
* @author Jonas Yang
*/
public class FileConfigLoaderTest {
@Test
public void testIsSupported() {
FileConfigLoader configLoader = new FileConfigLoader();
boolean isSupported = configLoader.isSupported(FileConfigLoader.PROTOCOL_PREFIX + "/opt/mytest");
Assert.assertTrue("The value is " + isSupported + ", not true", isSupported);
isSupported = configLoader.isSupported("jdbc:/opt/mytest");
Assert.assertFalse("The value is " + isSupported + ", not false", isSupported);
}
@Test
public void testGetFilePath() {
FileConfigLoader configLoader = new FileConfigLoader();
String filePath = configLoader.getFilePath(FileConfigLoader.PROTOCOL_PREFIX + "/opt/test/test.properties");
Assert.assertEquals("The path is " + filePath, filePath, "/opt/test/test.properties");
}
}
@@ -0,0 +1,33 @@
package com.alibaba.druid.bvt.filter.config;
import com.alibaba.druid.filter.config.ConfigFileGenerator;
import com.alibaba.druid.filter.config.loader.impl.FileConfigLoader;
import junit.framework.Assert;
import org.junit.Test;
import java.util.Properties;
/**
* @author Jonas Yang
*/
public class FileConfigLoaderTest1 extends ConfigFileGenerator {
@Test
public void testLoadConfigByFile() {
FileConfigLoader configLoader = new FileConfigLoader();
Properties p = configLoader.loadConfig(FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
Assert.assertNotNull("The properties is null", p);
Assert.assertEquals("The value is " + p.getProperty("username") + ", not test1", "test1", p.getProperty("username"));
}
@Test
public void testLoadConfigByNotExistFile() {
FileConfigLoader configLoader = new FileConfigLoader();
Properties p = configLoader.loadConfig(FileConfigLoader.PROTOCOL_PREFIX + "/test/test/test");
Assert.assertNull("The properties is not null", p);
}
}
@@ -0,0 +1,33 @@
package com.alibaba.druid.bvt.filter.config;
import org.junit.Assert;
import org.junit.Test;
import com.alibaba.druid.filter.config.loader.impl.HttpConfigLoader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author Jonas Yang
*/
public class HttpConfigLoaderTest {
@Test
public void testIsSupported() {
HttpConfigLoader configLoader = new HttpConfigLoader();
boolean isSupported = configLoader.isSupported(HttpConfigLoader.PROTOCOL_PREFIX + "www.5-nb.com");
Assert.assertTrue("The value is " + isSupported + ", not true", isSupported);
isSupported = configLoader.isSupported("jdbc:/opt/mytest");
Assert.assertFalse("The value is " + isSupported + ", not false", isSupported);
}
@Test
public void testGetUrl() throws MalformedURLException {
String protocol = HttpConfigLoader.PROTOCOL_PREFIX + "www.5-nb.com";
HttpConfigLoader configLoader = new HttpConfigLoader();
URL url = configLoader.getUrl(protocol);
Assert.assertEquals("The url is " + url, "http://www.5-nb.com", url.toString());
}
}
@@ -0,0 +1,135 @@
package com.alibaba.druid.bvt.filter.config;
import com.alibaba.druid.filter.config.security.decrypter.DecryptException;
import com.alibaba.druid.filter.config.security.decrypter.RsaDecrypter;
import com.alibaba.druid.filter.config.security.decrypter.SensitiveParameters;
import com.alibaba.druid.util.Base64;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.security.PublicKey;
import java.util.Properties;
/**
* @author Jonas Yang
*/
public class RsaDecrypterTest {
String publicKeyContent = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2YdTlkEw7JfRNItCosRf4QO8vOL54ZEdj0LZm" +
"tKhNFnNRTtHrLDFT44BPYqFSLVv3TOhLxJcEs31kyQhXEC5b9ozpZBvYrJDSt8QQMoYDAMLSojs+" +
"K4vTJavgbRKKads0di+DuoMmJ5g5dcbDNd1Kb4kgWhrZtUwhfSjQklaiIwIDAQAB";
String encryptedString = "Zaa9qpyB9snnBUz7UBvcaHcNfWMKEWvhnx0dbWmwxBOJ9vkeZNXjV9MEJezSzL+6yQh7aO29vD67" +
"qedtD9ublVwwf+jTPuO1NjzAKy0+yWVQ1KdR2KaQuvCxFWiM1n6nbpkUGB6OVpoQgFnFzdKlyC9D" +
"KU3Q6evGa1hlZ8jnHcA=";
String keyFilePath;
@Before
public void setUp() throws IOException {
File tmp = File.createTempFile("druid_public_key", Long.toString(System.currentTimeMillis()));
byte[] keyContent = Base64.base64ToByteArray(this.publicKeyContent);
FileOutputStream out = null;
try {
out = new FileOutputStream(tmp);
out.write(keyContent);
} finally {
if (out != null) {
out.close();
}
}
this.keyFilePath = tmp.getAbsolutePath();
}
@After
public void tearDown() {
File tmp = new File(this.keyFilePath);
if (tmp.exists()) {
tmp.delete();
}
this.keyFilePath = null;
}
@Test
public void testGetPublicKeyByString() {
RsaDecrypter rsaDecrypter = new RsaDecrypter();
PublicKey key = rsaDecrypter.getPublicKeyByString(this.publicKeyContent);
Assert.assertNotNull("It is null", key);
key = rsaDecrypter.getPublicKeyByString(null);
Assert.assertNotNull("It is null", key);
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
try {
key = rsaDecrypter.getPublicKeyByString("abcdef");
Assert.assertTrue("It is here", false);
} catch (Exception e) {
}
}
@Test
public void testGetPublicKeyByPublicKeyFile() {
RsaDecrypter rsaDecrypter = new RsaDecrypter();
PublicKey key = rsaDecrypter.getPublicKeyByPublicKeyFile(this.keyFilePath);
Assert.assertNotNull("It is null", key);
key = rsaDecrypter.getPublicKeyByPublicKeyFile(null);
Assert.assertNotNull("It is null", key);
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
try {
key = rsaDecrypter.getPublicKeyByPublicKeyFile("abcdef");
Assert.assertTrue("It is here", false);
} catch (Exception e) {
}
}
@Test
public void testGetPublicKeyByX509File() {
//No test
}
@Test
public void testGetPublicKey() {
RsaDecrypter rsaDecrypter = new RsaDecrypter();
PublicKey key = rsaDecrypter.getPublicKey(null);
Assert.assertNotNull("It is null", key);
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
Properties info = new Properties();
info.put(RsaDecrypter.KEY, this.publicKeyContent);
key = rsaDecrypter.getPublicKey(info);
Assert.assertNotNull("It is null", key);
Assert.assertEquals(this.publicKeyContent, Base64.byteArrayToBase64(key.getEncoded()));
info = new Properties();
info.put(RsaDecrypter.KEY_FILE, this.keyFilePath);
key = rsaDecrypter.getPublicKey(info);
Assert.assertNotNull("It is null", key);
Assert.assertEquals(this.publicKeyContent, Base64.byteArrayToBase64(key.getEncoded()));
}
@Test
public void testDecrypt() throws DecryptException {
RsaDecrypter decrypter = new RsaDecrypter();
Properties info = new Properties();
info.put(RsaDecrypter.KEY_FILE, this.keyFilePath);
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", encryptedString), info);
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
}
@Test
public void testDecryptWithEmptyKey() throws DecryptException {
RsaDecrypter decrypter = new RsaDecrypter();
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg=="), (Properties) null);
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
}
}