测试Spring Boot中的javax.imageio无法找到阅读器

huangapple 未分类评论53阅读模式
标题翻译

Test javax.imageIo within spring boot doesn't find reader

问题

  1. package com.apparence.enlaps.services.pictures;
  2. import com.apparence.enlaps.services.pictures.editor.BasicThumbPictureAdapter;
  3. import com.apparence.enlaps.services.pictures.editor.exceptions.ResizeException;
  4. import com.twelvemonkeys.image.ImageUtil;
  5. import com.twelvemonkeys.imageio.metadata.CompoundDirectory;
  6. import com.twelvemonkeys.imageio.metadata.exif.EXIFReader;
  7. import com.twelvemonkeys.imageio.metadata.jpeg.JPEG;
  8. import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegment;
  9. import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegmentUtil;
  10. import com.twelvemonkeys.imageio.metadata.tiff.TIFF;
  11. import com.twelvemonkeys.imageio.metadata.tiff.TIFFReader;
  12. import com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReaderSpi;
  13. import com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriterSpi;
  14. import com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReaderSpi;
  15. import com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriterSpi;
  16. import org.junit.Before;
  17. import org.junit.Test;
  18. import org.junit.runner.RunWith;
  19. import org.springframework.boot.SpringApplication;
  20. import org.springframework.boot.test.context.SpringBootContextLoader;
  21. import org.springframework.boot.test.context.SpringBootTest;
  22. import org.springframework.core.io.ClassPathResource;
  23. import org.springframework.core.io.Resource;
  24. import org.springframework.test.annotation.DirtiesContext;
  25. import org.springframework.test.context.ActiveProfiles;
  26. import org.springframework.test.context.junit4.SpringRunner;
  27. import org.junit.Assert;
  28. import javax.imageio.ImageIO;
  29. import javax.imageio.ImageReadParam;
  30. import javax.imageio.ImageReader;
  31. import javax.imageio.spi.IIORegistry;
  32. import javax.imageio.stream.ImageInputStream;
  33. import javax.imageio.stream.MemoryCacheImageInputStream;
  34. import java.io.ByteArrayInputStream;
  35. import java.io.IOException;
  36. import java.io.InputStream;
  37. import java.util.*;
  38. @RunWith(SpringRunner.class)
  39. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  40. @ActiveProfiles("test")
  41. @DirtiesContext
  42. public class PictureEditorTest {
  43. BasicThumbPictureAdapter adapter = new BasicThumbPictureAdapter();
  44. @Before
  45. public void init() {
  46. ImageIO.scanForPlugins();
  47. IIORegistry registry = IIORegistry.getDefaultInstance();
  48. registry.registerApplicationClasspathSpis();
  49. }
  50. @Test
  51. public void resizeJpgTest() throws IOException, ResizeException {
  52. Resource resource = new ClassPathResource("img/test2.jpg");
  53. Assert.assertNotNull("image to check resize exists", resource);
  54. Assert.assertTrue("image to check resize exists", resource.exists());
  55. Assert.assertTrue("image to check resize exists", resource.getFile().exists());
  56. var is = resource.getInputStream();
  57. Assert.assertTrue("image to check resize exists", is.available() > 0);
  58. CompoundDirectory sourceExif = getExifData(is);
  59. Assert.assertNotNull("Source exif have been read", sourceExif);
  60. var result = adapter.resize(is, 800);
  61. try (var resultIs = new ByteArrayInputStream(result)) {
  62. ImageInputStream imageIs = ImageIO.createImageInputStream(resultIs);
  63. Iterator<ImageReader> readers = ImageIO.getImageReaders(imageIs);
  64. if (!readers.hasNext()) {
  65. throw new IllegalArgumentException("No reader found");
  66. }
  67. ImageReader reader = readers.next();
  68. reader.setInput(imageIs);
  69. Assert.assertEquals("JPEG", reader.getFormatName());
  70. CompoundDirectory exif = getExifData(resultIs);
  71. Assert.assertNotNull(exif);
  72. Assert.assertTrue("width should be <= 800 now", reader.getWidth(0) <= 800);
  73. Assert.assertTrue("height should be <= 800 now", reader.getHeight(0) <= 800);
  74. Assert.assertEquals("Orientation has not changed", 5, exif.getEntryByFieldName("orientation").getValue());
  75. } catch (Exception e) {
  76. throw new RuntimeException("error while reading result", e);
  77. }
  78. }
  79. private CompoundDirectory getExifData(InputStream resultIs) throws IOException {
  80. List<JPEGSegment> exifSegment = JPEGSegmentUtil.readSegments(ImageIO.createImageInputStream(resultIs), JPEG.APP1, "Exif");
  81. if (!exifSegment.isEmpty()) {
  82. InputStream exifData = exifSegment.get(0).data();
  83. exifData.read(); // Skip 0-pad for Exif in JFIF
  84. try (ImageInputStream exifStream = ImageIO.createImageInputStream(exifData)) {
  85. return (CompoundDirectory) new TIFFReader().read(exifStream);
  86. }
  87. }
  88. return null;
  89. }
  90. }
英文翻译

currently I would like this test to work within my spring boot. This is a simple unit test on a resizing image function using imageIo, it checks that the resize didn't touch any of the exif data and made a smaller image as requested.
This function work when running the server but fails in unit test on the same image.

saying this :

  1. No reader found
  2. java.lang.IllegalArgumentException: No reader found
  3. at com.apparence.enlaps.services.pictures.editor.BasicThumbPictureAdapter.resize(BasicThumbPictureAdapter.java:39)
  4. at com.apparence.enlaps.services.pictures.PictureEditorTest.resizeJpgTest(PictureEditorTest.java:70)
  5. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  6. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  7. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  8. at java.base/java.lang.reflect.Method.invoke(Method.java:567)
  9. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  10. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

The unit test code is this. How can I make it pass, I believe it's a matter of context no loading all reader methods properly on a unit test context. But my attempt to force loading them seems not working too. I'm out of solutions.

  1. package com.apparence.enlaps.services.pictures;
  2. import com.apparence.enlaps.services.pictures.editor.BasicThumbPictureAdapter;
  3. import com.apparence.enlaps.services.pictures.editor.exceptions.ResizeException;
  4. import com.twelvemonkeys.image.ImageUtil;
  5. import com.twelvemonkeys.imageio.metadata.CompoundDirectory;
  6. import com.twelvemonkeys.imageio.metadata.exif.EXIFReader;
  7. import com.twelvemonkeys.imageio.metadata.jpeg.JPEG;
  8. import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegment;
  9. import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegmentUtil;
  10. import com.twelvemonkeys.imageio.metadata.tiff.TIFF;
  11. import com.twelvemonkeys.imageio.metadata.tiff.TIFFReader;
  12. import com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReaderSpi;
  13. import com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriterSpi;
  14. import com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReaderSpi;
  15. import com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriterSpi;
  16. import org.junit.Before;
  17. import org.junit.Test;
  18. import org.junit.runner.RunWith;
  19. import org.springframework.boot.SpringApplication;
  20. import org.springframework.boot.test.context.SpringBootContextLoader;
  21. import org.springframework.boot.test.context.SpringBootTest;
  22. import org.springframework.core.io.ClassPathResource;
  23. import org.springframework.core.io.Resource;
  24. import org.springframework.test.annotation.DirtiesContext;
  25. import org.springframework.test.context.ActiveProfiles;
  26. import org.springframework.test.context.junit4.SpringRunner;
  27. import org.junit.Assert;
  28. import javax.imageio.ImageIO;
  29. import javax.imageio.ImageReadParam;
  30. import javax.imageio.ImageReader;
  31. import javax.imageio.spi.IIORegistry;
  32. import javax.imageio.stream.ImageInputStream;
  33. import javax.imageio.stream.MemoryCacheImageInputStream;
  34. import java.io.ByteArrayInputStream;
  35. import java.io.IOException;
  36. import java.io.InputStream;
  37. import java.util.*;
  38. @RunWith(SpringRunner.class)
  39. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  40. @ActiveProfiles(&quot;test&quot;)
  41. @DirtiesContext
  42. public class PictureEditorTest {
  43. BasicThumbPictureAdapter adapter = new BasicThumbPictureAdapter();
  44. @Before
  45. public void init() {
  46. ImageIO.scanForPlugins();
  47. IIORegistry registry = IIORegistry.getDefaultInstance();
  48. registry.registerApplicationClasspathSpis();
  49. }
  50. // BROKEN test cauz of https://stackoverflow.com/questions/49270343/imageio-unsupported-image-type-twelvemonkeys-plugin-with-fix-not-working
  51. // IMAGEIO drivers not loaded in test context
  52. @Test
  53. public void resizeJpgTest() throws IOException, ResizeException {
  54. Resource resource = new ClassPathResource(&quot;img/test2.jpg&quot;);
  55. Assert.assertNotNull(&quot;image to check resize exists&quot;, resource);
  56. Assert.assertTrue(&quot;image to check resize exists&quot;, resource.exists());
  57. Assert.assertTrue(&quot;image to check resize exists&quot;, resource.getFile().exists());
  58. var is = resource.getInputStream();
  59. Assert.assertTrue(&quot;image to check resize exists&quot;, is.available() &gt; 0);
  60. CompoundDirectory sourceExif = getExifData(is);
  61. Assert.assertNotNull(&quot;Source exif have been read&quot;, sourceExif);
  62. // call resize
  63. var result = adapter.resize(is, 800);
  64. // check results
  65. try (var resultIs = new ByteArrayInputStream(result)) {
  66. ImageInputStream imageIs = ImageIO.createImageInputStream(resultIs);
  67. Iterator&lt;ImageReader&gt; readers = ImageIO.getImageReaders(imageIs);
  68. if (!readers.hasNext()) {
  69. throw new IllegalArgumentException(&quot;No reader found&quot;);
  70. }
  71. ImageReader reader = readers.next();
  72. reader.setInput(imageIs);
  73. Assert.assertEquals(&quot;JPEG&quot;, reader.getFormatName());
  74. CompoundDirectory exif = getExifData(resultIs);
  75. Assert.assertNotNull(exif);
  76. Assert.assertTrue(&quot;width should be &lt;= 800 now&quot;, reader.getWidth(0) &lt;= 800);
  77. Assert.assertTrue(&quot;height should be &lt;= 800 now&quot;, reader.getHeight(0) &lt;= 800);
  78. Assert.assertEquals(&quot;Orientation has not changed&quot;, 5, exif.getEntryByFieldName(&quot;orientation&quot;).getValue());
  79. } catch (Exception e) {
  80. throw new RuntimeException(&quot;error while reading result&quot;, e);
  81. }
  82. }
  83. private CompoundDirectory getExifData(InputStream resultIs) throws IOException {
  84. List&lt;JPEGSegment&gt; exifSegment = JPEGSegmentUtil.readSegments(ImageIO.createImageInputStream(resultIs), JPEG.APP1, &quot;Exif&quot;);
  85. if (!exifSegment.isEmpty()) {
  86. InputStream exifData = exifSegment.get(0).data();
  87. exifData.read(); // Skip 0-pad for Exif in JFIF
  88. try (ImageInputStream exifStream = ImageIO.createImageInputStream(exifData)) {
  89. return (CompoundDirectory) new TIFFReader().read(exifStream);
  90. }
  91. }
  92. return null;
  93. }
  94. }

huangapple
  • 本文由 发表于 2020年5月30日 16:26:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/62099800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定