@SuppressWarnings("unchecked") @Test public void performActionWithTallImage() throws Exception { final int imageHeight = 1000; final int imageWidth = 500; context.checking(new Expectations() { { oneOf(request).getFileItem(); will(returnValue(null)); allowing(person).getId(); ignoring(user); allowing(request).getImageId(); will(returnValue("avatarId")); allowing(request).getEntityId(); will(returnValue(1L)); oneOf(finder).findEntity(principal, 1L); will(returnValue(person)); oneOf(person).getAvatarId(); will(returnValue(null)); oneOf(person).setAvatarId("avatarId"); oneOf(personMapper).flush(); oneOf(imageWriter).getImageFromFile(null); will(returnValue(imageMock)); allowing(imageMock).getWidth(null); will(returnValue(imageWidth)); allowing(imageMock).getHeight(null); will(returnValue(imageHeight)); allowing(imageMock).getWidth(); will(returnValue(imageWidth)); allowing(imageMock).getHeight(); will(returnValue(imageHeight)); allowing(imageMock).getColorModel(); allowing(imageMock).getType(); allowing(imageMock).getRaster(); allowing(imageMock).getAccelerationPriority(); allowing(imageMock).getSampleModel(); oneOf(imageWriter).write(with(any(RenderedImage.class)), with(any(String.class))); oneOf(action).execute(with(any(TaskHandlerActionContext.class))); will(returnValue(personResult)); } }); sut.execute(TestContextCreator.createTaskHandlerContextWithPrincipal(request, principal)); context.assertIsSatisfied(); }
@SuppressWarnings("unchecked") @Override public Serializable execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext) { try { SaveImageRequest request = (SaveImageRequest) inActionContext.getActionContext().getParams(); String avatarId = request.getImageId(); Long entityId = request.getEntityId(); T avatarEntity = finder.findEntity(inActionContext.getActionContext().getPrincipal(), entityId); String oldAvatarId = avatarEntity.getAvatarId(); avatarEntity.setAvatarId(avatarId); mapper.flush(); BufferedImage originalImage = imageWriter.getImageFromFile(request.getFileItem()); Integer scaleX = SCALE_X; Integer scaleY = SCALE_Y; if (originalImage.getWidth(null) > originalImage.getHeight(null)) { scaleY = (scaleX * originalImage.getHeight(null)) / originalImage.getWidth(null); } else { scaleX = (scaleY * originalImage.getWidth(null)) / originalImage.getHeight(null); } ResampleOp resampleOp = new ResampleOp(scaleX, scaleY); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage scaledImage = resampleOp.filter(originalImage, null); imageWriter.write(scaledImage, "o" + avatarId); Integer x = 0; Integer y = 0; Integer cropSize = 0; if (scaledImage.getWidth(null) > scaledImage.getHeight(null)) { cropSize = scaledImage.getHeight(null); x = (scaledImage.getWidth(null) - scaledImage.getHeight(null)) / 2; } else { cropSize = scaledImage.getWidth(null); y = (scaledImage.getHeight(null) - scaledImage.getWidth(null)) / 2; } ResizeAvatarRequest resizeRequest = new ResizeAvatarRequest(x, y, cropSize, Boolean.FALSE, entityId); T newEntity = (T) new InlineExecutionStrategyExecutor().execute(avatarResizer, resizeRequest, inActionContext); if (oldAvatarId != null) { imageWriter.delete("o" + oldAvatarId); imageWriter.delete("n" + oldAvatarId); imageWriter.delete("s" + oldAvatarId); } if (cacheUpdaterStategy != null) { inActionContext.getUserActionRequests().addAll( cacheUpdaterStategy.getUpdateCacheRequests(inActionContext.getActionContext().getPrincipal(), entityId)); } return newEntity; } catch (Exception ex) { throw new ExecutionException(ex); } }
SaveAvatarExecution implements
        TaskHandlerExecutionStrategy<PrincipalActionContext> { @SuppressWarnings("unchecked") @Override public Serializable execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext) { try { SaveImageRequest request = (SaveImageRequest) inActionContext.getActionContext().getParams(); String avatarId = request.getImageId(); Long entityId = request.getEntityId(); T avatarEntity = finder.findEntity(inActionContext.getActionContext().getPrincipal(), entityId); String oldAvatarId = avatarEntity.getAvatarId(); avatarEntity.setAvatarId(avatarId); mapper.flush(); BufferedImage originalImage = imageWriter.getImageFromFile(request.getFileItem()); Integer scaleX = SCALE_X; Integer scaleY = SCALE_Y; if (originalImage.getWidth(null) > originalImage.getHeight(null)) { scaleY = (scaleX * originalImage.getHeight(null)) / originalImage.getWidth(null); } else { scaleX = (scaleY * originalImage.getWidth(null)) / originalImage.getHeight(null); } ResampleOp resampleOp = new ResampleOp(scaleX, scaleY); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage scaledImage = resampleOp.filter(originalImage, null); imageWriter.write(scaledImage, "o" + avatarId); Integer x = 0; Integer y = 0; Integer cropSize = 0; if (scaledImage.getWidth(null) > scaledImage.getHeight(null)) { cropSize = scaledImage.getHeight(null); x = (scaledImage.getWidth(null) - scaledImage.getHeight(null)) / 2; } else { cropSize = scaledImage.getWidth(null); y = (scaledImage.getHeight(null) - scaledImage.getWidth(null)) / 2; } ResizeAvatarRequest resizeRequest = new ResizeAvatarRequest(x, y, cropSize, Boolean.FALSE, entityId); T newEntity = (T) new InlineExecutionStrategyExecutor().execute(avatarResizer, resizeRequest, inActionContext); if (oldAvatarId != null) { imageWriter.delete("o" + oldAvatarId); imageWriter.delete("n" + oldAvatarId); imageWriter.delete("s" + oldAvatarId); } if (cacheUpdaterStategy != null) { inActionContext.getUserActionRequests().addAll( cacheUpdaterStategy.getUpdateCacheRequests(inActionContext.getActionContext().getPrincipal(), entityId)); } return newEntity; } catch (Exception ex) { throw new ExecutionException(ex); } } }
SaveAvatarExecution implements
        TaskHandlerExecutionStrategy<PrincipalActionContext> { @SuppressWarnings("unchecked") @Override public Serializable execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext) { try { SaveImageRequest request = (SaveImageRequest) inActionContext.getActionContext().getParams(); String avatarId = request.getImageId(); Long entityId = request.getEntityId(); T avatarEntity = finder.findEntity(inActionContext.getActionContext().getPrincipal(), entityId); String oldAvatarId = avatarEntity.getAvatarId(); avatarEntity.setAvatarId(avatarId); mapper.flush(); BufferedImage originalImage = imageWriter.getImageFromFile(request.getFileItem()); Integer scaleX = SCALE_X; Integer scaleY = SCALE_Y; if (originalImage.getWidth(null) > originalImage.getHeight(null)) { scaleY = (scaleX * originalImage.getHeight(null)) / originalImage.getWidth(null); } else { scaleX = (scaleY * originalImage.getWidth(null)) / originalImage.getHeight(null); } ResampleOp resampleOp = new ResampleOp(scaleX, scaleY); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage scaledImage = resampleOp.filter(originalImage, null); imageWriter.write(scaledImage, "o" + avatarId); Integer x = 0; Integer y = 0; Integer cropSize = 0; if (scaledImage.getWidth(null) > scaledImage.getHeight(null)) { cropSize = scaledImage.getHeight(null); x = (scaledImage.getWidth(null) - scaledImage.getHeight(null)) / 2; } else { cropSize = scaledImage.getWidth(null); y = (scaledImage.getHeight(null) - scaledImage.getWidth(null)) / 2; } ResizeAvatarRequest resizeRequest = new ResizeAvatarRequest(x, y, cropSize, Boolean.FALSE, entityId); T newEntity = (T) new InlineExecutionStrategyExecutor().execute(avatarResizer, resizeRequest, inActionContext); if (oldAvatarId != null) { imageWriter.delete("o" + oldAvatarId); imageWriter.delete("n" + oldAvatarId); imageWriter.delete("s" + oldAvatarId); } if (cacheUpdaterStategy != null) { inActionContext.getUserActionRequests().addAll( cacheUpdaterStategy.getUpdateCacheRequests(inActionContext.getActionContext().getPrincipal(), entityId)); } return newEntity; } catch (Exception ex) { throw new ExecutionException(ex); } } @SuppressWarnings("unchecked")  SaveAvatarExecution(final DomainEntityMapper<T> inMapper,
            final TaskHandlerExecutionStrategy<PrincipalActionContext> inAction, final ImageWriter inImageWriter,
            final EntityFinder<T> inFinder); }
SaveAvatarExecution implements
        TaskHandlerExecutionStrategy<PrincipalActionContext> { @SuppressWarnings("unchecked") @Override public Serializable execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext) { try { SaveImageRequest request = (SaveImageRequest) inActionContext.getActionContext().getParams(); String avatarId = request.getImageId(); Long entityId = request.getEntityId(); T avatarEntity = finder.findEntity(inActionContext.getActionContext().getPrincipal(), entityId); String oldAvatarId = avatarEntity.getAvatarId(); avatarEntity.setAvatarId(avatarId); mapper.flush(); BufferedImage originalImage = imageWriter.getImageFromFile(request.getFileItem()); Integer scaleX = SCALE_X; Integer scaleY = SCALE_Y; if (originalImage.getWidth(null) > originalImage.getHeight(null)) { scaleY = (scaleX * originalImage.getHeight(null)) / originalImage.getWidth(null); } else { scaleX = (scaleY * originalImage.getWidth(null)) / originalImage.getHeight(null); } ResampleOp resampleOp = new ResampleOp(scaleX, scaleY); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage scaledImage = resampleOp.filter(originalImage, null); imageWriter.write(scaledImage, "o" + avatarId); Integer x = 0; Integer y = 0; Integer cropSize = 0; if (scaledImage.getWidth(null) > scaledImage.getHeight(null)) { cropSize = scaledImage.getHeight(null); x = (scaledImage.getWidth(null) - scaledImage.getHeight(null)) / 2; } else { cropSize = scaledImage.getWidth(null); y = (scaledImage.getHeight(null) - scaledImage.getWidth(null)) / 2; } ResizeAvatarRequest resizeRequest = new ResizeAvatarRequest(x, y, cropSize, Boolean.FALSE, entityId); T newEntity = (T) new InlineExecutionStrategyExecutor().execute(avatarResizer, resizeRequest, inActionContext); if (oldAvatarId != null) { imageWriter.delete("o" + oldAvatarId); imageWriter.delete("n" + oldAvatarId); imageWriter.delete("s" + oldAvatarId); } if (cacheUpdaterStategy != null) { inActionContext.getUserActionRequests().addAll( cacheUpdaterStategy.getUpdateCacheRequests(inActionContext.getActionContext().getPrincipal(), entityId)); } return newEntity; } catch (Exception ex) { throw new ExecutionException(ex); } } @SuppressWarnings("unchecked")  SaveAvatarExecution(final DomainEntityMapper<T> inMapper,
            final TaskHandlerExecutionStrategy<PrincipalActionContext> inAction, final ImageWriter inImageWriter,
            final EntityFinder<T> inFinder); void setCacheUpdaterStategy(final CacheUpdater inCacheUpdaterStrategy); @SuppressWarnings("unchecked") @Override Serializable execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext); }
SaveAvatarExecution implements
        TaskHandlerExecutionStrategy<PrincipalActionContext> { @SuppressWarnings("unchecked") @Override public Serializable execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext) { try { SaveImageRequest request = (SaveImageRequest) inActionContext.getActionContext().getParams(); String avatarId = request.getImageId(); Long entityId = request.getEntityId(); T avatarEntity = finder.findEntity(inActionContext.getActionContext().getPrincipal(), entityId); String oldAvatarId = avatarEntity.getAvatarId(); avatarEntity.setAvatarId(avatarId); mapper.flush(); BufferedImage originalImage = imageWriter.getImageFromFile(request.getFileItem()); Integer scaleX = SCALE_X; Integer scaleY = SCALE_Y; if (originalImage.getWidth(null) > originalImage.getHeight(null)) { scaleY = (scaleX * originalImage.getHeight(null)) / originalImage.getWidth(null); } else { scaleX = (scaleY * originalImage.getWidth(null)) / originalImage.getHeight(null); } ResampleOp resampleOp = new ResampleOp(scaleX, scaleY); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage scaledImage = resampleOp.filter(originalImage, null); imageWriter.write(scaledImage, "o" + avatarId); Integer x = 0; Integer y = 0; Integer cropSize = 0; if (scaledImage.getWidth(null) > scaledImage.getHeight(null)) { cropSize = scaledImage.getHeight(null); x = (scaledImage.getWidth(null) - scaledImage.getHeight(null)) / 2; } else { cropSize = scaledImage.getWidth(null); y = (scaledImage.getHeight(null) - scaledImage.getWidth(null)) / 2; } ResizeAvatarRequest resizeRequest = new ResizeAvatarRequest(x, y, cropSize, Boolean.FALSE, entityId); T newEntity = (T) new InlineExecutionStrategyExecutor().execute(avatarResizer, resizeRequest, inActionContext); if (oldAvatarId != null) { imageWriter.delete("o" + oldAvatarId); imageWriter.delete("n" + oldAvatarId); imageWriter.delete("s" + oldAvatarId); } if (cacheUpdaterStategy != null) { inActionContext.getUserActionRequests().addAll( cacheUpdaterStategy.getUpdateCacheRequests(inActionContext.getActionContext().getPrincipal(), entityId)); } return newEntity; } catch (Exception ex) { throw new ExecutionException(ex); } } @SuppressWarnings("unchecked")  SaveAvatarExecution(final DomainEntityMapper<T> inMapper,
            final TaskHandlerExecutionStrategy<PrincipalActionContext> inAction, final ImageWriter inImageWriter,
            final EntityFinder<T> inFinder); void setCacheUpdaterStategy(final CacheUpdater inCacheUpdaterStrategy); @SuppressWarnings("unchecked") @Override Serializable execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext);  }