@Test @SuppressWarnings("unchecked") public void testStatefulClose(){ SessionFactory sessionFactory = mock(SessionFactory.class); Session session = mock(Session.class); Query<Foo> scrollableResults = mock(Query.class); HibernateCursorItemReader<Foo> itemReader = new HibernateCursorItemReader<>(); itemReader.setSessionFactory(sessionFactory); itemReader.setQueryString("testQuery"); itemReader.setUseStatelessSession(false); when(sessionFactory.openSession()).thenReturn(session); when(session.createQuery("testQuery")).thenReturn(scrollableResults); when(scrollableResults.setFetchSize(0)).thenReturn(scrollableResults); itemReader.open(new ExecutionContext()); itemReader.close(); }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { HibernateCursorItemReader<Foo> reader = (HibernateCursorItemReader<Foo>) tested; reader.close(); reader.setQueryString("from Foo foo where foo.id = -1"); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
@Test public void testConfigurationNoSaveState() throws Exception { Map<String, Object> parameters = new HashMap<>(); parameters.put("value", 2); HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReaderBuilder<Foo>() .name("fooReader") .sessionFactory(this.sessionFactory) .queryString("from Foo foo where foo.id > :value") .parameterValues(parameters) .saveState(false) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); int i = 0; while(reader.read() != null) { i++; } reader.update(executionContext); reader.close(); assertEquals(3, i); assertEquals(0, executionContext.size()); }
@Test public void testConfigurationNativeQuery() throws Exception { HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReaderBuilder<Foo>() .name("fooReader") .sessionFactory(this.sessionFactory) .nativeQuery("select * from T_FOOS") .entityClass(Foo.class) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); int i = 0; while(reader.read() != null) { i++; } reader.update(executionContext); reader.close(); assertEquals(5, i); }
@Test public void testConfigurationQueryProvider() throws Exception { HibernateNativeQueryProvider<Foo> provider = new HibernateNativeQueryProvider<>(); provider.setEntityClass(Foo.class); provider.setSqlQuery("select * from T_FOOS"); provider.afterPropertiesSet(); HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReaderBuilder<Foo>() .name("fooReader") .sessionFactory(this.sessionFactory) .queryProvider(provider) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); int i = 0; while(reader.read() != null) { i++; } reader.update(executionContext); reader.close(); assertEquals(5, i); }
@Test public void testConfiguration() throws Exception { HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReaderBuilder<Foo>() .name("fooReader") .sessionFactory(this.sessionFactory) .fetchSize(2) .currentItemCount(2) .maxItemCount(4) .queryName("allFoos") .useStatelessSession(true) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item1 = reader.read(); Foo item2 = reader.read(); assertNull(reader.read()); reader.update(executionContext); reader.close(); assertEquals(3, item1.getId()); assertEquals("bar3", item1.getName()); assertEquals(3, item1.getValue()); assertEquals(4, item2.getId()); assertEquals("bar4", item2.getName()); assertEquals(4, item2.getValue()); assertEquals(2, executionContext.size()); }