Skip to content

Commit

Permalink
Test cleanup/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ostewart committed Nov 30, 2010
1 parent 7ae99e3 commit 35563ea
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 216 deletions.
Expand Up @@ -26,14 +26,17 @@
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly=true)
@SuppressWarnings("unchecked") // for query.list()
@Repository("imageGroupRepository")
public class HibernateImageGroupRepository implements ImageGroupRepository {
private static final String ALBUM_BY_OWNER_AND_NAME_QRY =
"albumByOwnerAndName";
Expand All @@ -59,15 +62,14 @@ public class HibernateImageGroupRepository implements ImageGroupRepository {
"allImageGroups";

private SessionFactory sessionFactory;
private HibernateTemplate m_hibernateTemplate;
private HibernateTemplate hibernateTemplate;

public void setSessionFactory(SessionFactory sf) {
sessionFactory = sf;
@Autowired
public HibernateImageGroupRepository(SessionFactory sessionFactory, HibernateTemplate hibernateTemplate) {
this.sessionFactory = sessionFactory;
this.hibernateTemplate = hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate template) {
m_hibernateTemplate = template;
}

public ImageGroup getAlbumByOwnerAndName(User owner, String albumName) {
try {
Expand Down Expand Up @@ -100,7 +102,7 @@ public ImageGroup getRollByOwnerAndName(User owner, String rollName) {
public ImageFrame getImageFrameByGroupNameTypeAndImageId(String groupName, Type groupType, long imageId)
throws NoSuchImageFrameException {
List results =
m_hibernateTemplate
hibernateTemplate
.findByNamedQueryAndNamedParam(IMGFRAME_BY_GROUP_NAME_TYPE_AND_IMAGE_ID_QRY,
new String[] {"groupName", "groupType", "imageId"},
new Object[] {groupName, groupType, imageId});
Expand Down Expand Up @@ -242,15 +244,15 @@ public ImageGroup getByIdWithFrames(long id) {

public ImageGroup loadById(long imageGroupId) throws NoSuchImageGroupException {
try {
return m_hibernateTemplate.load(ImageGroup.class, imageGroupId);
return hibernateTemplate.load(ImageGroup.class, imageGroupId);
} catch (ObjectRetrievalFailureException e) {
throw new NoSuchImageGroupException(imageGroupId, e);
}
}

public List<ImageGroup> getAll() {
return (List<ImageGroup>)
m_hibernateTemplate.execute(new HibernateCallback() {
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
Query qry = session.getNamedQuery(ALL_GROUPS_QUERY);
return qry.list();
Expand All @@ -260,17 +262,17 @@ public Object doInHibernate(Session session) {

@Transactional(readOnly=false)
public void saveNewGroup(ImageGroup newGroup) {
m_hibernateTemplate.save(newGroup);
hibernateTemplate.save(newGroup);
}

@Transactional(readOnly=false)
public ImageGroup saveGroup(ImageGroup imageGroup) {
return m_hibernateTemplate.merge(imageGroup);
return hibernateTemplate.merge(imageGroup);
}

public int getPublicFrameCount(ImageGroup group) {
return (Integer)
m_hibernateTemplate.findByNamedQuery("publicFrameCount", group.getId()).get(0);
hibernateTemplate.findByNamedQuery("publicFrameCount", group.getId()).get(0);

}

Expand Down
Expand Up @@ -18,12 +18,15 @@
import com.trailmagic.image.ImageManifestationRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true)
@Repository("imageManifestationRepository")
public class HibernateImageManifestationRepository
implements ImageManifestationRepository {

Expand All @@ -33,6 +36,7 @@ public class HibernateImageManifestationRepository
private HibernateTemplate hibernateTemplate;
private Log log = LogFactory.getLog(HibernateImageManifestationRepository.class);

@Autowired
public HibernateImageManifestationRepository(HibernateTemplate hibernateTemplate) {
super();
this.hibernateTemplate = hibernateTemplate;
Expand Down
Expand Up @@ -17,47 +17,57 @@
import com.trailmagic.image.ImageGroup;
import com.trailmagic.image.ImageRepository;
import com.trailmagic.image.NoSuchImageException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly=true)
@SuppressWarnings("unchecked") // for query.list()
public class HibernateImageRepository extends HibernateDaoSupport implements ImageRepository{
import java.util.List;

@SuppressWarnings({"unchecked"})// for query.list()
@Transactional(readOnly = true)
@Repository("imageRepository")
public class HibernateImageRepository implements ImageRepository {
private static final String ALL_IMAGES_QUERY_NAME = "allImages";
private static final String IMAGES_BY_NAME_GROUP_QUERY_NAME = "imagesByNameAndGroup";
private HibernateTemplate hibernateTemplate;

@Autowired
public HibernateImageRepository(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

public Image getById(long id) {
return getHibernateTemplate().get(Image.class, id);
return hibernateTemplate.get(Image.class, id);
}

public Image loadById(long imageId) {
try {
return getHibernateTemplate().load(Image.class, imageId);
return hibernateTemplate.load(Image.class, imageId);
} catch (ObjectRetrievalFailureException e) {
throw new NoSuchImageException(imageId, e);
}
}

public List<Image> getAll() {
return getHibernateTemplate().findByNamedQuery(ALL_IMAGES_QUERY_NAME);
return hibernateTemplate.findByNamedQuery(ALL_IMAGES_QUERY_NAME);
}

public List<Image> getByNameAndGroup(String name, ImageGroup group) {
return getHibernateTemplate()
.findByNamedQueryAndNamedParam(IMAGES_BY_NAME_GROUP_QUERY_NAME,
new String[] {"name", "group"},
new Object[] {name, group});
return hibernateTemplate.findByNamedQueryAndNamedParam(IMAGES_BY_NAME_GROUP_QUERY_NAME,
new String[]{"name", "group"},
new Object[]{name, group});
}

@Transactional(readOnly=false)
@Transactional(readOnly = false)
public void saveNew(Image image) {
getHibernateTemplate().save(image);
hibernateTemplate.save(image);
}

@Transactional(readOnly=false)
@Transactional(readOnly = false)
public Image save(Image image) {
return getHibernateTemplate().merge(image);
return hibernateTemplate.merge(image);
}

}

This file was deleted.

Expand Up @@ -2,21 +2,12 @@

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.4.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.4.xsd">

<security:global-method-security secured-annotations="enabled" access-decision-manager-ref="imageAccessDecisionManager"/>

Expand Down
31 changes: 1 addition & 30 deletions image/src/main/resources/applicationContext-imagestore.xml
@@ -1,18 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<!--
Expand All @@ -33,23 +27,11 @@
<import resource="classpath:applicationContext-resizer.xml"/>
<context:component-scan base-package="com.trailmagic.util"/>
<context:component-scan base-package="com.trailmagic.image.impl"/>
<context:component-scan base-package="com.trailmagic.image.hibernate"/>

<task:annotation-driven executor="imageResizeExecutor"/>
<task:executor id="imageResizeExecutor" pool-size="1-10" queue-capacity="500" rejection-policy="CALLER_RUNS"/>

<bean id="imageGroupRepository"
class="com.trailmagic.image.hibernate.HibernateImageGroupRepository">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>

<bean id="imageManifestationRepository"
class="com.trailmagic.image.hibernate.HibernateImageManifestationRepository">
<constructor-arg ref="hibernateTemplate"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="imagesParser"
class="com.trailmagic.image.util.ImagesParserImpl">
<constructor-arg ref="hibernateTemplate"/>
Expand All @@ -58,15 +40,4 @@
<constructor-arg ref="imageService"/>
<constructor-arg ref="imageInitializer"/>
</bean>

<bean id="testInterceptor"
class="com.trailmagic.image.security.TestMethodInterceptor"/>


<bean id="imageRepository"
class="com.trailmagic.image.hibernate.HibernateImageRepository">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>


</beans>
Expand Up @@ -3,21 +3,17 @@
import com.trailmagic.image.Image;
import com.trailmagic.image.ImageGroup;
import com.trailmagic.image.impl.ImageInitializer;
import com.trailmagic.image.security.test.DataCreator;
import com.trailmagic.user.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Arrays;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-global.xml",
Expand All @@ -27,31 +23,25 @@
"classpath:applicationContext-imagestore-authorization.xml"})
@Transactional
public class ImageSecurityServiceAccessIntegrationTest {
@Autowired
private ImageSecurityService securityService;
@Autowired
private ImageInitializer imageInitializer;
@Autowired private ImageSecurityService securityService;
@Autowired private ImageInitializer imageInitializer;
@Autowired private DataCreator dataCreator;

@Test(expected = AccessDeniedException.class)
public void testAddOwnerAclToImageFailsWithoutUser() throws IOException {
setupNoAuthenticatedUser();
dataCreator.setupNoAuthenticatedUser();
securityService.addOwnerAcl(new Image());
}

@Test(expected = AccessDeniedException.class)
public void testAddOwnerAclToImageGroupFailsWithoutUser() throws IOException {
setupNoAuthenticatedUser();
dataCreator.setupNoAuthenticatedUser();
securityService.addOwnerAcl(new ImageGroup("name", new User("owner"), ImageGroup.Type.ROLL));
}

@Test(expected = AccessDeniedException.class)
public void testMakePublicFailsWithoutUser() throws IOException {
setupNoAuthenticatedUser();
dataCreator.setupNoAuthenticatedUser();
securityService.makePublic(new Image());
}

private void setupNoAuthenticatedUser() {
SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("key", "anonymousUser", Arrays.<GrantedAuthority>asList(new GrantedAuthorityImpl("ROLE_ANONYMOUS"))));

}
}

0 comments on commit 35563ea

Please sign in to comment.