/** * This implementation delegates to the {@code getConnection(ConnectionSpec)} * method of the target ConnectionFactory, passing in the specified user credentials. * If the specified username is empty, it will simply delegate to the standard * {@code getConnection()} method of the target ConnectionFactory. * @param spec the ConnectionSpec to apply * @return the Connection * @see javax.resource.cci.ConnectionFactory#getConnection(javax.resource.cci.ConnectionSpec) * @see javax.resource.cci.ConnectionFactory#getConnection() */ protected Connection doGetConnection(@Nullable ConnectionSpec spec) throws ResourceException { ConnectionFactory connectionFactory = getTargetConnectionFactory(); Assert.state(connectionFactory != null, "No 'targetConnectionFactory' set"); return (spec != null ? connectionFactory.getConnection(spec) : connectionFactory.getConnection()); }
@Override public RecordFactory getRecordFactory() throws ResourceException { return obtainTargetConnectionFactory().getRecordFactory(); }
@Override public ResourceAdapterMetaData getMetaData() throws ResourceException { return obtainTargetConnectionFactory().getMetaData(); }
@Test public void testMappingRecordOperationWithOutputRecordCreator() 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); RecordCreator outputCreator = mock(RecordCreator.class); InteractionSpec interactionSpec = mock(InteractionSpec.class); QueryCallDetector callDetector = mock(QueryCallDetector.class); MappingRecordOperationImpl query = new MappingRecordOperationImpl(connectionFactory, interactionSpec); query.setOutputRecordCreator(outputCreator); 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(connectionFactory.getRecordFactory()).willReturn(recordFactory); given(outputCreator.createRecord(recordFactory)).willReturn(outputRecord); given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true); given(callDetector.callExtractOutputData(outputRecord)).willReturn(outObj); assertSame(outObj, query.execute(inObj)); verify(interaction).close(); verify(connection).close(); }
@Override public Reference getReference() throws NamingException { return obtainTargetConnectionFactory().getReference(); }
@Override public void setReference(Reference reference) { obtainTargetConnectionFactory().setReference(reference); }
@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(); }
@Override public Reference getReference() throws NamingException { return obtainTargetConnectionFactory().getReference(); }
@Override public void setReference(Reference reference) { obtainTargetConnectionFactory().setReference(reference); }
/** * Create a CCI Connection via this template's ConnectionFactory. * @return the new CCI Connection * @throws javax.resource.ResourceException if thrown by CCI API methods */ protected Connection doCreateConnection() throws ResourceException { ConnectionFactory connectionFactory = getTargetConnectionFactory(); Assert.state(connectionFactory != null, "No 'targetConnectionFactory' set"); return connectionFactory.getConnection(); }
@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(); }
/** * Return a RecordFactory for the given ConnectionFactory. * <p>Default implementation returns the connector's RecordFactory if * available, falling back to a NotSupportedRecordFactory placeholder. * This allows to invoke a RecordCreator callback with a non-null * RecordFactory reference in any case. * @param connectionFactory the CCI ConnectionFactory * @return the CCI RecordFactory for the ConnectionFactory * @throws ResourceException if thrown by CCI methods * @see org.springframework.jca.cci.connection.NotSupportedRecordFactory */ protected RecordFactory getRecordFactory(ConnectionFactory connectionFactory) throws ResourceException { try { return connectionFactory.getRecordFactory(); } catch (NotSupportedException ex) { return new NotSupportedRecordFactory(); } }
@Override public Reference getReference() throws NamingException { return obtainTargetConnectionFactory().getReference(); }
@Override public void setReference(Reference reference) { obtainTargetConnectionFactory().setReference(reference); }
@Override public ResourceAdapterMetaData getMetaData() throws ResourceException { return obtainTargetConnectionFactory().getMetaData(); }
@Override public Connection getConnection() throws ResourceException { return obtainTargetConnectionFactory().getConnection(); }
@SuppressWarnings("unchecked") @Test public void testTemplateExecuteInputGeneratorExtractorTrueWithCreator() throws ResourceException, SQLException { 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); RecordExtractor<Object> extractor = mock(RecordExtractor.class); RecordCreator creator = mock(RecordCreator.class); Record inputRecord = mock(Record.class); Record outputRecord = mock(Record.class); Object obj = new Object(); InteractionSpec interactionSpec = mock(InteractionSpec.class); given(connectionFactory.getRecordFactory()).willReturn(recordFactory); given(connectionFactory.getConnection()).willReturn(connection); given(connection.createInteraction()).willReturn(interaction); given(creator.createRecord(recordFactory)).willReturn(outputRecord); given(connectionFactory.getRecordFactory()).willReturn(recordFactory); given(generator.createRecord(recordFactory)).willReturn(inputRecord); given(interaction.execute(interactionSpec, inputRecord, outputRecord)).willReturn(true); given(extractor.extractData(outputRecord)).willReturn(obj); CciTemplate ct = new CciTemplate(connectionFactory); ct.setOutputRecordCreator(creator); assertEquals(obj, ct.execute(interactionSpec, generator, extractor)); verify(interaction).close(); verify(connection).close(); }
@Override public RecordFactory getRecordFactory() throws ResourceException { return obtainTargetConnectionFactory().getRecordFactory(); }
@Override public Reference getReference() throws NamingException { return obtainTargetConnectionFactory().getReference(); }
@Override public void setReference(Reference reference) { obtainTargetConnectionFactory().setReference(reference); }