/** * Close the given Connection. * @param con the Connection to close */ protected void closeConnection(Connection con) { try { con.close(); } catch (Throwable ex) { logger.warn("Could not close shared CCI Connection", ex); } }
@Override public RecordFactory getRecordFactory() throws ResourceException { return obtainTargetConnectionFactory().getRecordFactory(); }
@Override public Reference getReference() throws NamingException { return obtainTargetConnectionFactory().getReference(); }
@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(); }
@Override public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException { return obtainTargetConnectionFactory().getConnection(connectionSpec); }
@Test public void testCreateIndexedRecord() throws ResourceException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); RecordFactory recordFactory = mock(RecordFactory.class); IndexedRecord indexedRecord = mock(IndexedRecord.class); given(connectionFactory.getRecordFactory()).willReturn(recordFactory); given(recordFactory.createIndexedRecord("name")).willReturn(indexedRecord); CciTemplate ct = new CciTemplate(connectionFactory); ct.createIndexedRecord("name"); verify(recordFactory).createIndexedRecord("name"); }
@Test public void testCreateMappedRecord() throws ResourceException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); RecordFactory recordFactory = mock(RecordFactory.class); MappedRecord mappedRecord = mock(MappedRecord.class); given(connectionFactory.getRecordFactory()).willReturn(recordFactory); given(recordFactory.createMappedRecord("name")).willReturn(mappedRecord); CciTemplate ct = new CciTemplate(connectionFactory); ct.createMappedRecord("name"); verify(recordFactory).createMappedRecord("name"); }
@Override public ResourceAdapterMetaData getMetaData() throws ResourceException { return obtainTargetConnectionFactory().getMetaData(); }
@Override public void setReference(Reference reference) { obtainTargetConnectionFactory().setReference(reference); }
@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(); }
@Override public Connection getConnection() throws ResourceException { return obtainTargetConnectionFactory().getConnection(); }
/** * 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(); } }
@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(); }
/** * 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 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(); }
@Test public void testTemplateExecuteInputOutput() 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, outputRecord)).willReturn(true); CciTemplate ct = new CciTemplate(connectionFactory); ct.execute(interactionSpec, inputRecord, outputRecord); verify(interaction).execute(interactionSpec, inputRecord, outputRecord); verify(interaction).close(); verify(connection).close(); }
@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(); }
@Test public void testTemplateExecuteInputFalseTrue() 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)).willReturn(null); CciTemplate ct = new CciTemplate(connectionFactory); Record tmpOutputRecord = ct.execute(interactionSpec, inputOutputRecord); assertNull(tmpOutputRecord); verify(interaction).execute(interactionSpec, inputOutputRecord); verify(interaction).close(); verify(connection).close(); }
@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(); }
@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(); }