javax.resource.cci.ConnectionFactory Java Examples

The following examples show how to use javax.resource.cci.ConnectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EisOperationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleRecordOperationWithInputOutputRecord() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputOutputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	SimpleRecordOperation query = new SimpleRecordOperation(connectionFactory, interactionSpec);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputOutputRecord, inputOutputRecord)).willReturn(true);

	query.execute(inputOutputRecord, inputOutputRecord);

	verify(interaction).execute(interactionSpec, inputOutputRecord, inputOutputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #2
Source File: CciTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateExecuteInputTrueTrue() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	Record inputOutputRecord = mock(Record.class);
	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputOutputRecord, inputOutputRecord)).willReturn(true);

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionSpec, inputOutputRecord, inputOutputRecord);

	verify(interaction).execute(interactionSpec, inputOutputRecord, inputOutputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #3
Source File: CciTemplateTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testTemplateExecuteConnectionCallback()
		throws ResourceException, SQLException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	ConnectionCallback<Object> connectionCallback = mock(ConnectionCallback.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connectionCallback.doInConnection(connection, connectionFactory)).willReturn(new Object());

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(connectionCallback);

	verify(connectionCallback).doInConnection(connection, connectionFactory);
	verify(connection).close();
}
 
Example #4
Source File: CciTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateExecuteInputFalse() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionSpec, inputRecord);

	verify(interaction).execute(interactionSpec, inputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #5
Source File: CciTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testTemplateExecuteInteractionCallback()
		throws ResourceException, SQLException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	InteractionCallback<Object> interactionCallback = mock(InteractionCallback.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interactionCallback.doInInteraction(interaction,connectionFactory)).willReturn(new Object());

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionCallback);

	verify(interactionCallback).doInInteraction(interaction,connectionFactory);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #6
Source File: ConnectionFactoryUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Actually obtain a CCI Connection from the given ConnectionFactory.
 * Same as {@link #getConnection}, but throwing the original ResourceException.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * <p>Directly accessed by {@link TransactionAwareConnectionFactoryProxy}.
 * @param cf the ConnectionFactory to obtain Connection from
 * @return a CCI Connection from the given ConnectionFactory
 * @throws ResourceException if thrown by CCI API methods
 * @see #doReleaseConnection
 */
public static Connection doGetConnection(ConnectionFactory cf) throws ResourceException {
	Assert.notNull(cf, "No ConnectionFactory specified");

	ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(cf);
	if (conHolder != null) {
		return conHolder.getConnection();
	}

	logger.debug("Opening CCI Connection");
	Connection con = cf.getConnection();

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		conHolder = new ConnectionHolder(con);
		conHolder.setSynchronizedWithTransaction(true);
		TransactionSynchronizationManager.registerSynchronization(new ConnectionSynchronization(conHolder, cf));
		TransactionSynchronizationManager.bindResource(cf, conHolder);
	}

	return con;
}
 
Example #7
Source File: CciTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testTemplateExecuteInputExtractorFalse()
		throws ResourceException, SQLException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordExtractor<Object> extractor = mock(RecordExtractor.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);
	given(extractor.extractData(outputRecord)).willReturn(new Object());

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionSpec, inputRecord, extractor);

	verify(extractor).extractData(outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #8
Source File: CciTemplate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <T> T execute(final InteractionCallback<T> action) throws DataAccessException {
	Assert.notNull(action, "Callback object must not be null");
	return execute(new ConnectionCallback<T>() {
		@Override
		public T doInConnection(Connection connection, ConnectionFactory connectionFactory)
				throws ResourceException, SQLException, DataAccessException {
			Interaction interaction = connection.createInteraction();
			try {
				return action.doInInteraction(interaction, connectionFactory);
			}
			finally {
				closeInteraction(interaction);
			}
		}
	});
}
 
Example #9
Source File: CciTemplateTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testTemplateExecuteConnectionCallback()
		throws ResourceException, SQLException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	ConnectionCallback<Object> connectionCallback = mock(ConnectionCallback.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connectionCallback.doInConnection(connection, connectionFactory)).willReturn(new Object());

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(connectionCallback);

	verify(connectionCallback).doInConnection(connection, connectionFactory);
	verify(connection).close();
}
 
Example #10
Source File: JCAContext.java    From hibersap with Apache License 2.0 6 votes vote down vote up
private ConnectionFactory getConnectionFactory(final String jndiName) {
    try {
        final InitialContext initialContext = new InitialContext();
        final Object object = initialContext.lookup(jndiName);

        if (object == null) {
            throw new HibersapException("Name not bound: " + jndiName);
        }

        if (!(object instanceof ConnectionFactory)) {
            throw new HibersapException("Object bound under " + jndiName + " is no ConnectionFactory");
        }

        return (ConnectionFactory) object;
    } catch (final NamingException e) {
        throw new HibersapException("JNDI lookup:", e);
    }
}
 
Example #11
Source File: EisOperationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSimpleRecordOperationWithExplicitOutputRecord() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	SimpleRecordOperation operation = new SimpleRecordOperation(connectionFactory, interactionSpec);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true);

	operation.execute(inputRecord, outputRecord);

	verify(interaction).execute(interactionSpec, inputRecord, outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #12
Source File: CciTemplateTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testTemplateExecuteInputOutputConnectionSpec() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	ConnectionSpec connectionSpec = mock(ConnectionSpec.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection(connectionSpec)).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true);

	ConnectionSpecConnectionFactoryAdapter adapter = new ConnectionSpecConnectionFactoryAdapter();
	adapter.setTargetConnectionFactory(connectionFactory);
	adapter.setConnectionSpec(connectionSpec);
	CciTemplate ct = new CciTemplate(adapter);
	ct.execute(interactionSpec, inputRecord, outputRecord);

	verify(interaction).execute(interactionSpec, inputRecord, outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #13
Source File: EisOperationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSimpleRecordOperation() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	SimpleRecordOperation query = new SimpleRecordOperation(connectionFactory, interactionSpec);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);

	query.execute(inputRecord);

	verify(interaction).execute(interactionSpec, inputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #14
Source File: ConnectionFactoryUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Actually obtain a CCI Connection from the given ConnectionFactory.
 * Same as {@link #getConnection}, but throwing the original ResourceException.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * <p>Directly accessed by {@link TransactionAwareConnectionFactoryProxy}.
 * @param cf the ConnectionFactory to obtain Connection from
 * @return a CCI Connection from the given ConnectionFactory
 * @throws ResourceException if thrown by CCI API methods
 * @see #doReleaseConnection
 */
public static Connection doGetConnection(ConnectionFactory cf) throws ResourceException {
	Assert.notNull(cf, "No ConnectionFactory specified");

	ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(cf);
	if (conHolder != null) {
		return conHolder.getConnection();
	}

	logger.debug("Opening CCI Connection");
	Connection con = cf.getConnection();

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering transaction synchronization for CCI Connection");
		conHolder = new ConnectionHolder(con);
		conHolder.setSynchronizedWithTransaction(true);
		TransactionSynchronizationManager.registerSynchronization(new ConnectionSynchronization(conHolder, cf));
		TransactionSynchronizationManager.bindResource(cf, conHolder);
	}

	return con;
}
 
Example #15
Source File: EisOperationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSimpleRecordOperationWithInputOutputRecord() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputOutputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	SimpleRecordOperation query = new SimpleRecordOperation(connectionFactory, interactionSpec);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputOutputRecord, inputOutputRecord)).willReturn(true);

	query.execute(inputOutputRecord, inputOutputRecord);

	verify(interaction).execute(interactionSpec, inputOutputRecord, inputOutputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #16
Source File: CciTemplateTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testTemplateExecuteInputFalse() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionSpec, inputRecord);

	verify(interaction).execute(interactionSpec, inputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #17
Source File: CciTemplateTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testTemplateExecuteInputTrueTrueWithCreator()
		throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordCreator creator = mock(RecordCreator.class);

	Record inputOutputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputOutputRecord, inputOutputRecord)).willReturn(true);

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.setOutputRecordCreator(creator);
	ct.execute(interactionSpec, inputOutputRecord, inputOutputRecord);

	verify(interaction).execute(interactionSpec, inputOutputRecord, inputOutputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #18
Source File: CciLocalTransactionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Test if a transaction ( begin / rollback ) is executed on the
 * LocalTransaction when CciLocalTransactionManager is specified as
 * transaction manager and a non-checked exception is thrown.
 */
@Test
public void testLocalTransactionRollback() throws ResourceException {
	final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	LocalTransaction localTransaction = mock(LocalTransaction.class);
	final Record record = mock(Record.class);
	final InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.getLocalTransaction()).willReturn(localTransaction);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, record, record)).willReturn(true);
	given(connection.getLocalTransaction()).willReturn(localTransaction);

	CciLocalTransactionManager tm = new CciLocalTransactionManager();
	tm.setConnectionFactory(connectionFactory);
	TransactionTemplate tt = new TransactionTemplate(tm);

	try {
		tt.execute(new TransactionCallback<Void>() {
			@Override
			public Void doInTransaction(TransactionStatus status) {
				assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory));
				CciTemplate ct = new CciTemplate(connectionFactory);
				ct.execute(interactionSpec, record, record);
				throw new DataRetrievalFailureException("error");
			}
		});
	}
	catch (Exception ex) {
	}

	verify(localTransaction).begin();
	verify(interaction).close();
	verify(localTransaction).rollback();
	verify(connection).close();
}
 
Example #19
Source File: CciTemplateTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testTemplateExecuteInputOutputResultsSetFalse()
		throws ResourceException, SQLException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	RecordFactory recordFactory = mock(RecordFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	Record record = mock(Record.class);
	ResultSet resultset = mock(ResultSet.class);
	RecordCreator generator = mock(RecordCreator.class);
	RecordExtractor<Object> extractor = mock(RecordExtractor.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(generator.createRecord(recordFactory)).willReturn(record);
	given(interaction.execute(interactionSpec, record)).willReturn(resultset);
	given(extractor.extractData(resultset)).willReturn(new Object());

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionSpec, generator, extractor);

	verify(extractor).extractData(resultset);
	verify(resultset).close();
	verify(interaction).close();
	verify(connection).close();
}
 
Example #20
Source File: CciTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testTemplateExecuteInputGeneratorFalse()
		throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	RecordFactory recordFactory = mock(RecordFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordCreator generator = mock(RecordCreator.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(generator.createRecord(recordFactory)).willReturn(inputRecord);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionSpec, generator);

	verify(interaction).execute(interactionSpec, inputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #21
Source File: CciTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute the specified interaction on an EIS with CCI.
 * All other interaction execution methods go through this.
 * @param spec the CCI InteractionSpec instance that defines
 * the interaction (connector-specific)
 * @param inputRecord the input record
 * @param outputRecord output record (can be {@code null})
 * @param outputExtractor object to convert the output record to a result object
 * @return the output data extracted with the RecordExtractor object
 * @throws DataAccessException if there is any problem
 */
protected <T> T doExecute(
		final InteractionSpec spec, final Record inputRecord, final Record outputRecord,
		final RecordExtractor<T> outputExtractor) throws DataAccessException {

	return execute(new InteractionCallback<T>() {
		@Override
		public T doInInteraction(Interaction interaction, ConnectionFactory connectionFactory)
				throws ResourceException, SQLException, DataAccessException {
			Record outputRecordToUse = outputRecord;
			try {
				if (outputRecord != null || getOutputRecordCreator() != null) {
					// Use the CCI execute method with output record as parameter.
					if (outputRecord == null) {
						RecordFactory recordFactory = getRecordFactory(connectionFactory);
						outputRecordToUse = getOutputRecordCreator().createRecord(recordFactory);
					}
					interaction.execute(spec, inputRecord, outputRecordToUse);
				}
				else {
					outputRecordToUse = interaction.execute(spec, inputRecord);
				}
				return (outputExtractor != null ? outputExtractor.extractData(outputRecordToUse) : null);
			}
			finally {
				if (outputRecordToUse instanceof ResultSet) {
					closeResultSet((ResultSet) outputRecordToUse);
				}
			}
		}
	});
}
 
Example #22
Source File: CciTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testTemplateExecuteInputExtractorTrueWithCreator()
		throws ResourceException, SQLException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	RecordFactory recordFactory = mock(RecordFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordExtractor<Object> extractor = mock(RecordExtractor.class);
	RecordCreator creator = mock(RecordCreator.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(creator.createRecord(recordFactory)).willReturn(outputRecord);
	given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true);
	given(extractor.extractData(outputRecord)).willReturn(new Object());

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.setOutputRecordCreator(creator);
	ct.execute(interactionSpec, inputRecord, extractor);

	verify(extractor).extractData(outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #23
Source File: CciTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testTemplateExecuteInputGeneratorTrueWithCreator()
		throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	RecordFactory recordFactory = mock(RecordFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordCreator generator = mock(RecordCreator.class);
	RecordCreator creator = mock(RecordCreator.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(generator.createRecord(recordFactory)).willReturn(inputRecord);
	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(creator.createRecord(recordFactory)).willReturn(outputRecord);
	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true);


	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.setOutputRecordCreator(creator);
	ct.execute(interactionSpec, generator);

	verify(interaction).execute(interactionSpec, inputRecord, outputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #24
Source File: TransactionAwareConnectionFactoryProxy.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Delegate to ConnectionFactoryUtils for automatically participating in Spring-managed
 * transactions. Throws the original ResourceException, if any.
 * @return a transactional Connection if any, a new one else
 * @see org.springframework.jca.cci.connection.ConnectionFactoryUtils#doGetConnection
 */
@Override
public Connection getConnection() throws ResourceException {
	ConnectionFactory targetConnectionFactory = obtainTargetConnectionFactory();
	Connection con = ConnectionFactoryUtils.doGetConnection(targetConnectionFactory);
	return getTransactionAwareConnectionProxy(con, targetConnectionFactory);
}
 
Example #25
Source File: CciTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testTemplateExecuteInputGeneratorFalse()
		throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	RecordFactory recordFactory = mock(RecordFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordCreator generator = mock(RecordCreator.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(generator.createRecord(recordFactory)).willReturn(inputRecord);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);

	CciTemplate ct = new CciTemplate(connectionFactory);
	ct.execute(interactionSpec, generator);

	verify(interaction).execute(interactionSpec, inputRecord);
	verify(interaction).close();
	verify(connection).close();
}
 
Example #26
Source File: EisOperationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testMappingRecordOperation() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordFactory recordFactory = mock(RecordFactory.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	QueryCallDetector callDetector = mock(QueryCallDetector.class);

	MappingRecordOperationImpl query = new MappingRecordOperationImpl(connectionFactory, interactionSpec);
	query.setCallDetector(callDetector);

	Object inObj = new Object();
	Object outObj = new Object();

	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(callDetector.callCreateInputRecord(recordFactory, inObj)).willReturn(inputRecord);
	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);
	given(callDetector.callExtractOutputData(outputRecord)).willReturn(outObj);

	assertSame(outObj, query.execute(inObj));
	verify(interaction).close();
	verify(connection).close();
}
 
Example #27
Source File: CciTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Construct a new CciTemplate, given a ConnectionFactory to obtain Connections from.
 * Note: This will trigger eager initialization of the exception translator.
 * @param connectionFactory the JCA ConnectionFactory to obtain Connections from
 * @param connectionSpec the CCI ConnectionSpec to obtain Connections for
 * (may be {@code null})
 */
public CciTemplate(ConnectionFactory connectionFactory, @Nullable ConnectionSpec connectionSpec) {
	setConnectionFactory(connectionFactory);
	if (connectionSpec != null) {
		setConnectionSpec(connectionSpec);
	}
	afterPropertiesSet();
}
 
Example #28
Source File: CciLocalTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Set the CCI ConnectionFactory that this instance should manage local
 * transactions for.
 */
public void setConnectionFactory(ConnectionFactory cf) {
	if (cf instanceof TransactionAwareConnectionFactoryProxy) {
		// If we got a TransactionAwareConnectionFactoryProxy, we need to perform transactions
		// for its underlying target ConnectionFactory, else JMS access code won't see
		// properly exposed transactions (i.e. transactions for the target ConnectionFactory).
		this.connectionFactory = ((TransactionAwareConnectionFactoryProxy) cf).getTargetConnectionFactory();
	}
	else {
		this.connectionFactory = cf;
	}
}
 
Example #29
Source File: EisOperationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testMappingRecordOperation() throws ResourceException {
	ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
	Connection connection = mock(Connection.class);
	Interaction interaction = mock(Interaction.class);
	RecordFactory recordFactory = mock(RecordFactory.class);

	Record inputRecord = mock(Record.class);
	Record outputRecord = mock(Record.class);

	InteractionSpec interactionSpec = mock(InteractionSpec.class);

	QueryCallDetector callDetector = mock(QueryCallDetector.class);

	MappingRecordOperationImpl query = new MappingRecordOperationImpl(connectionFactory, interactionSpec);
	query.setCallDetector(callDetector);

	Object inObj = new Object();
	Object outObj = new Object();

	given(connectionFactory.getRecordFactory()).willReturn(recordFactory);
	given(callDetector.callCreateInputRecord(recordFactory, inObj)).willReturn(inputRecord);
	given(connectionFactory.getConnection()).willReturn(connection);
	given(connection.createInteraction()).willReturn(interaction);
	given(interaction.execute(interactionSpec, inputRecord)).willReturn(outputRecord);
	given(callDetector.callExtractOutputData(outputRecord)).willReturn(outObj);

	assertSame(outObj, query.execute(inObj));
	verify(interaction).close();
	verify(connection).close();
}
 
Example #30
Source File: CciLocalTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
	CciLocalTransactionObject txObject = (CciLocalTransactionObject) transaction;
	ConnectionFactory connectionFactory = obtainConnectionFactory();

	// Remove the connection holder from the thread.
	TransactionSynchronizationManager.unbindResource(connectionFactory);
	txObject.getConnectionHolder().clear();

	Connection con = txObject.getConnectionHolder().getConnection();
	if (logger.isDebugEnabled()) {
		logger.debug("Releasing CCI Connection [" + con + "] after transaction");
	}
	ConnectionFactoryUtils.releaseConnection(con, connectionFactory);
}