use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.
the class SoftwareSystemManagerTest method testUpdateSoftwareSystem.
/**
* Test method for
* {@link org.talend.cwm.management.api.SoftwareSystemManager#updateSoftwareSystem(java.sql.Connection, orgomg.cwm.foundation.softwaredeployment.SoftwareSystem)}
* .
*/
@Test
public void testUpdateSoftwareSystem() {
SoftwareSystemManager softwareSystemManger = SoftwareSystemManager.getInstance();
PowerMockito.mockStatic(EMFSharedResources.class);
EMFSharedResources emfSharedResourceMock = mock(EMFSharedResources.class);
when(EMFSharedResources.getInstance()).thenReturn(emfSharedResourceMock).thenReturn(emfSharedResourceMock);
Resource softwareSysResource = mock(Resource.class);
when(emfSharedResourceMock.getSoftwareDeploymentResource()).thenReturn(softwareSysResource);
EList<EObject> existedSystems = new BasicEList<EObject>();
when(softwareSysResource.getContents()).thenReturn(existedSystems);
when(emfSharedResourceMock.saveResource(softwareSysResource)).thenReturn(Boolean.TRUE);
try {
softwareSystemManger.updateSoftwareSystem(null);
Connection mockConnection = mock(Connection.class);
DatabaseMetaData mockDBMetaData = mock(DatabaseMetaData.class);
// Mocking Static Class
PowerMockito.mockStatic(DriverManager.class);
// $NON-NLS-1$
String sql = "jdbc:mysql://localhost:3306/tbi";
when(DriverManager.getConnection(sql)).thenReturn(mockConnection);
when(mockConnection.getMetaData()).thenReturn(mockDBMetaData);
// Mock database product name.
// $NON-NLS-1$
String mysqlDB = "MYSQL";
when(mockDBMetaData.getDatabaseProductName()).thenReturn(mysqlDB);
// Mock database product version
// $NON-NLS-1$
String version = "5.5";
when(mockDBMetaData.getDatabaseProductVersion()).thenReturn(version);
when(mockDBMetaData.getDatabaseMinorVersion()).thenReturn(5);
when(mockDBMetaData.getDatabaseMajorVersion()).thenReturn(5);
DatabaseConnection dbConn = ConnectionPackage.eINSTANCE.getConnectionFactory().createDatabaseConnection();
TaggedValueHelper.setTaggedValue(dbConn, TaggedValueHelper.DB_PRODUCT_NAME, mysqlDB);
TaggedValueHelper.setTaggedValue(dbConn, TaggedValueHelper.DB_PRODUCT_VERSION, version);
PowerMockito.mockStatic(ConvertionHelper.class);
// $NON-NLS-1$
when(ConvertionHelper.convert(dbConn, false, "")).thenReturn(null);
PowerMockito.mockStatic(MetadataFillFactory.class);
MetadataFillFactory metadataFillFactory = mock(MetadataFillFactory.class);
when(MetadataFillFactory.getDBInstance()).thenReturn(metadataFillFactory);
TypedReturnCode<java.sql.Connection> returnCode = new TypedReturnCode<Connection>(Boolean.TRUE);
returnCode.setObject(mockConnection);
when(metadataFillFactory.createConnection(null)).thenReturn(returnCode);
softwareSystemManger.updateSoftwareSystem(dbConn);
Assert.assertTrue(existedSystems.size() > 0);
SoftwareSystem newSoftwareSys = (SoftwareSystem) existedSystems.get(0);
Assert.assertEquals(mysqlDB, newSoftwareSys.getSubtype());
Assert.assertEquals(version, newSoftwareSys.getVersion());
} catch (SQLException e) {
Assert.fail(e.getMessage());
}
}
use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.
the class UnitTestBuildHelper method getDB2DataManager.
/**
* getDataManager of DB2
*
* @return
*/
public Connection getDB2DataManager() {
// TypedProperties connectionParams = PropertiesLoader.getProperties(IndicatorEvaluator.class, "db.properties"); //$NON-NLS-1$
// $NON-NLS-1$
String driverClassName = "com.ibm.db2.jcc.DB2Driver";
// $NON-NLS-1$
String dbUrl = "jdbc:db2://192.168.31.135:50000/sample";
// $NON-NLS-1$
String sqlTypeName = "IBM DB2";
DBConnectionParameter params = new DBConnectionParameter();
// $NON-NLS-1$
params.setName("DB2_Connection");
params.setDriverClassName(driverClassName);
params.setJdbcUrl(dbUrl);
params.setSqlTypeName(sqlTypeName);
Properties properties = new Properties();
properties.setProperty(TaggedValueHelper.UNIVERSE, "");
properties.setProperty(TaggedValueHelper.DATA_FILTER, "");
properties.setProperty(TaggedValueHelper.USER, "db2inst1");
properties.setProperty(TaggedValueHelper.PASSWORD, "db2inst1");
params.setParameters(properties);
// create connection
ConnectionUtils.setTimeout(false);
MetadataFillFactory instance = MetadataFillFactory.getDBInstance();
IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(params));
ReturnCode rc = null;
try {
rc = instance.checkConnection(metaConnection);
} catch (java.lang.RuntimeException e) {
Assert.fail("connect to " + dbUrl + "failed," + e.getMessage());
}
Connection dataProvider = null;
if (rc.isOk()) {
dataProvider = instance.fillUIConnParams(metaConnection, null);
dataProvider.setName("DB2_Connection");
// because the DI side code is changed, modify the following code.
metaConnection.setCurrentConnection(dataProvider);
try {
ProjectNodeHelper.fillCatalogAndSchemas(metaConnection, (DatabaseConnection) dataProvider);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Assert.assertNotNull("Can not connect to Database: " + dbUrl, dataProvider);
return dataProvider;
}
use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.
the class UnitTestBuildHelper method getRealOracleDatabase.
/**
* get a real database connection,the connection parameters load from a propery file.
*
* @return
*/
public static Connection getRealOracleDatabase() {
Properties connectionParams = DBPropertiesUtils.getDefault().getProperties();
// $NON-NLS-1$
String driverClassName = connectionParams.getProperty("driver_oracle");
// $NON-NLS-1$
String dbUrl = connectionParams.getProperty("url_oracle");
// $NON-NLS-1$
String sqlTypeName = connectionParams.getProperty("sqlTypeName_oracle");
// $NON-NLS-1$
String dbVersion = connectionParams.getProperty("dbVersion_oracle");
// $NON-NLS-1$
String userName = connectionParams.getProperty("user_oracle");
// $NON-NLS-1$
String password = connectionParams.getProperty("password_oracle");
DBConnectionParameter params = new DBConnectionParameter();
// $NON-NLS-1$
params.setName("My connection");
params.setDriverClassName(driverClassName);
params.setJdbcUrl(dbUrl);
params.setSqlTypeName(sqlTypeName);
params.setParameters(connectionParams);
params.getParameters();
Properties properties = new Properties();
properties.setProperty(TaggedValueHelper.USER, userName);
properties.setProperty(TaggedValueHelper.PASSWORD, password);
properties.setProperty(TaggedValueHelper.DB_PRODUCT_VERSION, dbVersion);
params.setParameters(properties);
// create connection
ConnectionUtils.setTimeout(false);
MetadataFillFactory instance = MetadataFillFactory.getDBInstance();
IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(params));
metaConnection.setDbVersionString(dbVersion);
ReturnCode rc = null;
try {
rc = instance.checkConnection(metaConnection);
} catch (java.lang.RuntimeException e) {
// $NON-NLS-1$ //$NON-NLS-2$
Assert.fail("connect to " + dbUrl + "failed," + e.getMessage());
}
Connection dataProvider = null;
if (rc.isOk()) {
dataProvider = instance.fillUIConnParams(metaConnection, null);
dataProvider.setName(DATA_PROVIDER_NAME);
// because the DI side code is changed, modify the following code.
metaConnection.setCurrentConnection(dataProvider);
try {
ProjectNodeHelper.fillCatalogAndSchemas(metaConnection, (DatabaseConnection) dataProvider);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Assert.assertNotNull(dataProvider);
return dataProvider;
}
use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.
the class ExportConnectionToTOSAction method fillCatalogSchema.
/**
* DOC gdbu Comment method "fillCatalogSchema".
*
* @deprecated Won't be used.
* @param tdDataProvider
*/
@Deprecated
protected DatabaseConnection fillCatalogSchema(IMetadataConnection newMetadataConn) {
MetadataFillFactory instance = MetadataFillFactory.getDBInstance(newMetadataConn);
ReturnCode rc = instance.createConnection(newMetadataConn);
DatabaseConnection dbConn = null;
if (rc.isOk()) {
dbConn = (DatabaseConnection) instance.fillUIConnParams(newMetadataConn, null);
DatabaseMetaData dbMetadata = null;
java.sql.Connection sqlConn = null;
try {
if (rc instanceof TypedReturnCode) {
Object sqlConnObject = ((TypedReturnCode) rc).getObject();
if (sqlConnObject instanceof java.sql.Connection) {
sqlConn = (java.sql.Connection) sqlConnObject;
dbMetadata = ExtractMetaDataUtils.getInstance().getConnectionMetadata(sqlConn);
}
}
List<String> packageFilterCatalog = MetadataConnectionUtils.getPackageFilter(dbConn, dbMetadata, true);
instance.fillCatalogs(dbConn, dbMetadata, packageFilterCatalog);
List<String> packageFilterSchema = MetadataConnectionUtils.getPackageFilter(dbConn, dbMetadata, false);
instance.fillSchemas(dbConn, dbMetadata, packageFilterSchema);
} catch (SQLException e) {
log.error(e, e);
} finally {
if (sqlConn != null) {
ConnectionUtils.closeConnection(sqlConn);
}
}
} else {
log.error(rc.getMessage());
}
return dbConn;
}
use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.
the class DQStructureComparer method getRefreshedDataProvider.
public static TypedReturnCode<Connection> getRefreshedDataProvider(Connection prevDataProvider) {
// ADD xqliu 2010-03-29 bug 11951
TypedReturnCode<Connection> returnProvider = new TypedReturnCode<Connection>();
// ~11951
// MOD by zshen 2012-07-05 for bug 5074 remove convert about DatabaseParameter instead
// Connection->DatabaseParameter->ImetadataConnection into Connection->ImetadataConnection
IMetadataConnection metadataConnection = ConvertionHelper.convert((DatabaseConnection) prevDataProvider, false, prevDataProvider.getContextName());
Connection copyedConnection = null;
EDatabaseTypeName currentEDatabaseType = EDatabaseTypeName.getTypeFromDbType(metadataConnection.getDbType());
if (currentEDatabaseType != null) {
MetadataFillFactory dbInstance = MetadataFillFactory.getDBInstance(metadataConnection);
TypedReturnCode<?> trc = (TypedReturnCode<?>) dbInstance.createConnection(metadataConnection);
Object sqlConnObject = trc.getObject();
DatabaseMetaData dbJDBCMetadata = null;
if (trc.isOk() && sqlConnObject instanceof java.sql.Connection) {
java.sql.Connection sqlConn = (java.sql.Connection) sqlConnObject;
// MOD sizhaoliu 2012-5-21 TDQ-4884 reload structure issue
// dbJDBCMetadata = org.talend.utils.sql.ConnectionUtils.getConnectionMetadata(sqlConn);
dbJDBCMetadata = ExtractMetaDataUtils.getInstance().getDatabaseMetaData(sqlConn, (DatabaseConnection) prevDataProvider);
copyedConnection = EObjectHelper.deepCopy(prevDataProvider);
copyedConnection.getDataPackage().clear();
// MOD zshen the parameter for packageFiler need to differnent isCatalog or not.
dbInstance.fillCatalogs(copyedConnection, dbJDBCMetadata, metadataConnection, MetadataConnectionUtils.getPackageFilter(copyedConnection, dbJDBCMetadata, true));
dbInstance.fillSchemas(copyedConnection, dbJDBCMetadata, metadataConnection, MetadataConnectionUtils.getPackageFilter(copyedConnection, dbJDBCMetadata, false));
ConnectionUtils.closeConnection(sqlConn);
} else {
returnProvider.setMessage(trc.getMessage());
}
}
if (copyedConnection == null) {
returnProvider.setOk(false);
} else {
returnProvider.setObject(copyedConnection);
}
// ~11951
return returnProvider;
}
Aggregations