View Javadoc

1   package com.atlassian.core.util.thumbnail;
2   
3   import com.google.common.base.Optional;
4   import org.junit.Before;
5   import org.junit.Test;
6   import org.mockito.Mock;
7   import org.mockito.MockitoAnnotations;
8   
9   import javax.imageio.ImageReader;
10  import javax.imageio.stream.ImageInputStream;
11  import java.io.IOException;
12  import java.io.InputStream;
13  
14  import static org.hamcrest.CoreMatchers.equalTo;
15  import static org.junit.Assert.assertThat;
16  import static org.mockito.Mockito.verify;
17  import static org.mockito.Mockito.when;
18  
19  public class DimensionsHelperTest {
20  
21      public static final int IMAGE_HEIGHT = 123456;
22      public static final int IMAGE_WIDTH = 98765;
23      @Mock
24      private ImageReader imageReader;
25      @Mock
26      private InputStream inputStream;
27  
28      @Before
29      public void setUp() throws Exception {
30          MockitoAnnotations.initMocks(this);
31      }
32  
33      @Test
34      public void testGettingRightDimensionsInHappyPath() throws Exception {
35          final DimensionsHelper dimensionsHelper = create(imageReader);
36  
37          when(imageReader.getHeight(0)).thenReturn(IMAGE_HEIGHT);
38          when(imageReader.getWidth(0)).thenReturn(IMAGE_WIDTH);
39  
40          final Dimensions dimensions = dimensionsHelper.dimensionsForImage(inputStream);
41  
42          assertThat(dimensions.getHeight(), equalTo(IMAGE_HEIGHT));
43          assertThat(dimensions.getWidth(), equalTo(IMAGE_WIDTH));
44  
45          verify(imageReader).dispose();
46          verify(inputStream).close();
47      }
48  
49      @Test(expected = IOException.class)
50      public void testThrowsExceptionWhenCannotReadImage() throws Exception {
51          final DimensionsHelper dimensionsHelper = create(null);
52          
53          dimensionsHelper.dimensionsForImage(inputStream);
54      }
55  
56      /**
57       * Testing extremely wide and tall images clamp minimum width and height to 1,
58       * as per http://jira.atlassian.com/browse/JRA-20369
59       */
60      @Test
61      public void testDetermineScaleSize() throws Exception {
62          final DimensionsHelper dimensionsHelper = create(null);
63          Dimensions dimensions = dimensionsHelper.determineScaledDimensions(20, 20, 1, 600);
64          assertThat(dimensions.getWidth(), equalTo(1));
65          assertThat(dimensions.getHeight(), equalTo(20));
66  
67          dimensions = dimensionsHelper.determineScaledDimensions(200, 200, 600, 1);
68          assertThat(dimensions.getWidth(), equalTo(200));
69          assertThat(dimensions.getHeight(), equalTo(1));
70      }
71  
72      private DimensionsHelper create(final ImageReader imageReader) {
73          return new DimensionsHelper() {
74  
75              @Override
76              ImageInputStream getImageInputStream(InputStream inputStream) throws IOException {
77                  return null;
78              }
79  
80              @Override
81              Optional<ImageReader> getImageReader(ImageInputStream inputStream) throws IOException {
82                  return Optional.fromNullable(imageReader);
83              }
84  
85          };
86      }
87  }