public void testInterceptorWithThreadBoundAndFilters() { MockControl sfControl = MockControl.createControl(SessionFactory.class); SessionFactory sf = (SessionFactory) sfControl.getMock(); MockControl sessionControl = MockControl.createControl(Session.class); Session session = (Session) sessionControl.getMock(); session.getSessionFactory(); sessionControl.setReturnValue(sf, 1); session.isOpen(); sessionControl.setReturnValue(true, 1); session.enableFilter("myFilter"); sessionControl.setReturnValue(null, 1); session.enableFilter("yourFilter"); sessionControl.setReturnValue(null, 1); session.disableFilter("myFilter"); sessionControl.setVoidCallable(1); session.disableFilter("yourFilter"); sessionControl.setVoidCallable(1); sfControl.replay(); sessionControl.replay(); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setSessionFactory(sf); interceptor.setFilterNames(new String[] {"myFilter", "yourFilter"}); try { interceptor.invoke(new TestInvocation(sf)); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } finally { TransactionSynchronizationManager.unbindResource(sf); } sfControl.verify(); sessionControl.verify(); }
public void testInterceptorWithNewSessionAndFilter() throws HibernateException { MockControl sfControl = MockControl.createControl(SessionFactory.class); SessionFactory sf = (SessionFactory) sfControl.getMock(); MockControl sessionControl = MockControl.createControl(Session.class); Session session = (Session) sessionControl.getMock(); sf.openSession(); sfControl.setReturnValue(session, 1); session.getSessionFactory(); sessionControl.setReturnValue(sf); session.enableFilter("myFilter"); sessionControl.setReturnValue(null, 1); session.flush(); sessionControl.setVoidCallable(1); session.close(); sessionControl.setReturnValue(null, 1); sfControl.replay(); sessionControl.replay(); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setSessionFactory(sf); interceptor.setFilterName("myFilter"); try { interceptor.invoke(new TestInvocation(sf)); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } sfControl.verify(); sessionControl.verify(); }
public void testFiltersWithJoinedSubclass() { Session s = openSession(); s.enableFilter("region").setParameter("userRegion", "US"); Transaction t = s.beginTransaction(); prepareTestData(s); s.clear(); List results; Iterator itr; results = s.createQuery("from Person").list(); assertEquals("Incorrect qry result count", 4, results.size()); s.clear(); results = s.createQuery("from Employee").list(); assertEquals("Incorrect qry result count", 2, results.size()); s.clear(); results = new ArrayList( new HashSet(s.createQuery("from Person as p left join fetch p.minions").list())); assertEquals("Incorrect qry result count", 4, results.size()); itr = results.iterator(); while (itr.hasNext()) { // find john final Person p = (Person) itr.next(); if (p.getName().equals("John Doe")) { Employee john = (Employee) p; assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size()); break; } } s.clear(); results = new ArrayList( new HashSet(s.createQuery("from Employee as p left join fetch p.minions").list())); assertEquals("Incorrect qry result count", 2, results.size()); itr = results.iterator(); while (itr.hasNext()) { // find john final Person p = (Person) itr.next(); if (p.getName().equals("John Doe")) { Employee john = (Employee) p; assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size()); break; } } t.commit(); s.close(); }