Upgrade to Junit5 (#1050)

* chore: Update to junit5 for servlet package

* chore: Update to junit5 for contrib package

* chore: Update to junit5 for common-image package

* chore: Update to junit5 for common-lang package

* chore: Update to junit5 for entire project files

* fix: test case for JPEGImageReaderTest failed for java 8 and 11

assertEquals was using old signature of junit4.

* Update common/common-io/src/test/java/com/twelvemonkeys/io/InputStreamAbstractTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update common/common-io/src/test/java/com/twelvemonkeys/io/InputStreamAbstractTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-bmp/src/test/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-jpeg/src/test/java/com/twelvemonkeys/imageio/plugins/jpeg/JPEGImageReaderTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageMetadataTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageReaderTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageWriterTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedChannelImageInputStreamTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedChannelImageInputStreamTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* refactor: few indentation changes and missed test case

- review change related to missing test annotation
- unwanted new lines inside test case
- duplicate assertions

* refactor: moved the lambda expression to method reference

* review: testNotNullWithParameterNull catch block was never executed.

Added the suggested change

* Apply suggestions from code review

chore: adjust suggested indentation

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageReaderAbstractTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageReaderAbstractTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* Update imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageWriterAbstractTest.java

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>

* refactor: using assertTimeout doesnot kill the execution, even if the timeout happens.

It is better to use assertTimeoutPreemptively in cases, where we really want to kill the execution after timeout.
https://stackoverflow.com/questions/57116801/how-to-fail-a-test-after-a-timeout-is-exceeded-in-junit-5/57116959#57116959

---------

Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>
This commit is contained in:
Vyshak Puthusseri
2024-11-12 14:43:15 +05:30
committed by GitHub
parent a67fdd4b80
commit 543acce8a3
194 changed files with 2554 additions and 3086 deletions

View File

@@ -30,9 +30,8 @@
package com.twelvemonkeys.imageio.metadata;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* AbstractEntryTest
@@ -51,9 +50,9 @@ public class AbstractEntryTest extends EntryAbstractTest {
return new AbstractEntry(identifier, value) {};
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCreateEntryNullId() {
createEntry(null, new Object());
assertThrows(IllegalArgumentException.class, () -> createEntry(null, new Object()));
}
@Test

View File

@@ -30,14 +30,12 @@
package com.twelvemonkeys.imageio.metadata;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* CompoundDirectoryTest
@@ -60,9 +58,9 @@ public abstract class CompoundDirectoryAbstractTest extends DirectoryAbstractTes
return createCompoundDirectory(Collections.<Directory>singleton(createSingleDirectory(entries)));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCreateNullDirectories() {
createCompoundDirectory(Collections.<Directory>singleton(null));
assertThrows(IllegalArgumentException.class, () -> createCompoundDirectory(Collections.<Directory>singleton(null)));
}
@Test
@@ -103,12 +101,12 @@ public abstract class CompoundDirectoryAbstractTest extends DirectoryAbstractTes
assertSame(three, directory.getDirectory(2));
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testOutOfBounds() {
Directory only = createSingleDirectory(null);
CompoundDirectory directory = createCompoundDirectory(Collections.<Directory>singleton(only));
directory.getDirectory(directory.directoryCount());
assertThrows(IndexOutOfBoundsException.class, () -> directory.getDirectory(directory.directoryCount()));
}
protected static final class TestDirectory extends AbstractDirectory {

View File

@@ -31,11 +31,11 @@
package com.twelvemonkeys.imageio.metadata;
import com.twelvemonkeys.lang.ObjectAbstractTest;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* DirectoryTest
@@ -65,9 +65,9 @@ public abstract class DirectoryAbstractTest extends ObjectAbstractTest {
assertEquals(0, directory.size());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCreateNullEntries() {
createDirectory(Collections.<Entry>singleton(null));
assertThrows(IllegalArgumentException.class, () -> createDirectory(Collections.<Entry>singleton(null)));
}
@Test

View File

@@ -31,11 +31,11 @@
package com.twelvemonkeys.imageio.metadata;
import com.twelvemonkeys.lang.ObjectAbstractTest;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* EntryTest

View File

@@ -35,7 +35,6 @@ import com.twelvemonkeys.imageio.stream.URLImageInputStreamSpi;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.spi.IIORegistry;
@@ -44,7 +43,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* ReaderAbstractTest
@@ -71,9 +71,9 @@ public abstract class MetadataReaderAbstractTest {
protected abstract MetadataReader createReader();
@Test(expected = IllegalArgumentException.class)
@Test
public void testReadNull() throws IOException {
createReader().read(null);
assertThrows(IllegalArgumentException.class, () -> createReader().read(null));
}
@Test

View File

@@ -31,8 +31,8 @@
package com.twelvemonkeys.imageio.metadata;
import com.twelvemonkeys.imageio.stream.URLImageInputStreamSpi;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import javax.imageio.ImageIO;
import javax.imageio.spi.IIORegistry;
import javax.imageio.stream.ImageInputStream;
@@ -43,6 +43,8 @@ import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* ReaderAbstractTest
*
@@ -68,14 +70,17 @@ public abstract class MetadataWriterAbstractTest {
protected abstract MetadataWriter createWriter();
@Test(expected = IllegalArgumentException.class)
@Test
public void testWriteNullDirectory() throws IOException {
createWriter().write(null, new MemoryCacheImageOutputStream(new ByteArrayOutputStream()));
assertThrows(IllegalArgumentException.class, () -> createWriter().write(null, new MemoryCacheImageOutputStream(new ByteArrayOutputStream())));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testWriteNullStream() throws IOException {
createWriter().write(new AbstractDirectory(new ArrayList<Entry>()) {
}, null);
assertThrows(IllegalArgumentException.class, () ->
createWriter().write(new AbstractDirectory(new ArrayList<Entry>()) {
}, null)
);
}
}

View File

@@ -36,17 +36,16 @@ import com.twelvemonkeys.imageio.metadata.MetadataReaderAbstractTest;
import com.twelvemonkeys.imageio.metadata.tiff.TIFF;
import com.twelvemonkeys.imageio.stream.SubImageInputStream;
import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
/**
* EXIFReaderTest
@@ -83,14 +82,16 @@ public class EXIFReaderTest extends MetadataReaderAbstractTest {
assertEquals(exif.size(), exif.getDirectory(0).size() + exif.getDirectory(1).size());
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testDirectoryOutOfBounds() throws IOException {
InputStream data = getData();
CompoundDirectory exif = (CompoundDirectory) createReader().read(ImageIO.createImageInputStream(data));
assertEquals(2, exif.directoryCount());
assertNotNull(exif.getDirectory(exif.directoryCount()));
assertThrows(IndexOutOfBoundsException.class, () -> {
exif.getDirectory(exif.directoryCount());
});
}
@Test

View File

@@ -37,7 +37,6 @@ import com.twelvemonkeys.imageio.metadata.tiff.TIFFReader;
import com.twelvemonkeys.imageio.metadata.tiff.TIFFWriter;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import com.twelvemonkeys.io.FastByteArrayOutputStream;
import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
@@ -47,8 +46,8 @@ import java.io.InputStream;
import java.nio.ByteOrder;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* TIFFWriterTest

View File

@@ -30,9 +30,8 @@
package com.twelvemonkeys.imageio.metadata.exif;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* RationalTest
@@ -43,19 +42,19 @@ import static org.junit.Assert.assertEquals;
*/
@SuppressWarnings("deprecation")
public class RationalTest {
@Test(expected = IllegalArgumentException.class)
@Test
public void testZeroDenominator() {
new Rational(1, 0);
assertThrows(IllegalArgumentException.class, () -> new Rational(1, 0));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testLongMinValueNumerator() {
new Rational(Long.MIN_VALUE, 1);
assertThrows(IllegalArgumentException.class, () -> new Rational(Long.MIN_VALUE, 1));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testLongMinValueDenominator() {
new Rational(1, Long.MIN_VALUE);
assertThrows(IllegalArgumentException.class, () -> new Rational(1, Long.MIN_VALUE));
}
@Test

View File

@@ -33,14 +33,13 @@ package com.twelvemonkeys.imageio.metadata.iptc;
import com.twelvemonkeys.imageio.metadata.Directory;
import com.twelvemonkeys.imageio.metadata.MetadataReaderAbstractTest;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.io.InputStream;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
/**
* IPTCReaderTest

View File

@@ -34,7 +34,6 @@ import com.twelvemonkeys.imageio.metadata.Directory;
import com.twelvemonkeys.imageio.metadata.MetadataWriter;
import com.twelvemonkeys.imageio.metadata.MetadataWriterAbstractTest;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import org.junit.Test;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.io.ByteArrayOutputStream;
@@ -43,8 +42,9 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeNotNull;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
* IPTCWriterTest.
@@ -72,8 +72,7 @@ public class IPTCWriterTest extends MetadataWriterAbstractTest {
public void testRewriteExisting() throws IOException {
IPTCReader reader = createReader();
Directory iptc = reader.read(getDataAsIIS());
assumeNotNull(iptc);
assumeTrue(iptc != null, "IPTC object should not be null");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(bytes);
createWriter().write(iptc, stream);

View File

@@ -31,8 +31,6 @@
package com.twelvemonkeys.imageio.metadata.jpeg;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import org.junit.Ignore;
import org.junit.Test;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
@@ -47,8 +45,9 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* JPEGQualityTest
@@ -97,7 +96,7 @@ public class JPEGQualityTest {
}
}
@Ignore("Need a JPEG test image with bad DQT data...")
@Disabled("Need a JPEG test image with bad DQT data...")
@Test
public void testGetQualityBadData() throws IOException {
ImageInputStream stream = ImageIO.createImageInputStream(getClass().getResourceAsStream("/bad-data"));
@@ -257,9 +256,9 @@ public class JPEGQualityTest {
}
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testGetQTablesNull() throws IOException {
JPEGQuality.getQTables(null);
assertThrows(IllegalArgumentException.class, () -> JPEGQuality.getQTables(null));
}
@Test

View File

@@ -31,11 +31,11 @@
package com.twelvemonkeys.imageio.metadata.jpeg;
import com.twelvemonkeys.lang.ObjectAbstractTest;
import org.junit.Test;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.Assert.assertEquals;
/**
* JPEGSegmentTest

View File

@@ -31,7 +31,6 @@
package com.twelvemonkeys.imageio.metadata.jpeg;
import com.twelvemonkeys.imageio.stream.URLImageInputStreamSpi;
import org.junit.Test;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
@@ -46,8 +45,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* JPEGSegmentUtilTest
@@ -148,7 +147,7 @@ public class JPEGSegmentUtilTest {
}
ICC_Profile profile = ICC_Profile.getInstance(new SequenceInputStream(Collections.enumeration(Arrays.asList(streams))));
assertNotNull("Profile could not be read, probably bad data", profile);
assertNotNull(profile, "Profile could not be read, probably bad data");
}
@Test
@@ -156,9 +155,9 @@ public class JPEGSegmentUtilTest {
List<JPEGSegment> segments = JPEGSegmentUtil.readSegments(getData("/jpeg/9788245605525.jpg"), JPEGSegmentUtil.ALL_SEGMENTS);
assertEquals(7, segments.size());
assertEquals(segments.toString(), JPEG.SOF0, segments.get(3).marker());
assertEquals(segments.toString(), null, segments.get(3).identifier());
assertEquals(segments.toString(), JPEG.SOS, segments.get(segments.size() - 1).marker());
assertEquals(JPEG.SOF0, segments.get(3).marker(), segments.toString());
assertEquals(null, segments.get(3).identifier(), segments.toString());
assertEquals(JPEG.SOS, segments.get(segments.size() - 1).marker(), segments.toString());
}
@Test
@@ -166,9 +165,9 @@ public class JPEGSegmentUtilTest {
List<JPEGSegment> segments = JPEGSegmentUtil.readSegments(getData("/jpeg/ts_open_300dpi.jpg"), JPEGSegmentUtil.ALL_SEGMENTS);
assertEquals(27, segments.size());
assertEquals(segments.toString(), JPEG.SOF0, segments.get(23).marker());
assertEquals(segments.toString(), null, segments.get(23).identifier());
assertEquals(segments.toString(), JPEG.SOS, segments.get(segments.size() - 1).marker());
assertEquals(JPEG.SOF0, segments.get(23).marker(), segments.toString());
assertEquals(null, segments.get(23).identifier(), segments.toString());
assertEquals(JPEG.SOS, segments.get(segments.size() - 1).marker(), segments.toString());
}
@Test

View File

@@ -33,15 +33,14 @@ package com.twelvemonkeys.imageio.metadata.psd;
import com.twelvemonkeys.imageio.metadata.Directory;
import com.twelvemonkeys.imageio.metadata.MetadataReaderAbstractTest;
import com.twelvemonkeys.imageio.stream.SubImageInputStream;
import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* PhotoshopReaderTest

View File

@@ -34,8 +34,6 @@ import com.twelvemonkeys.imageio.metadata.*;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import com.twelvemonkeys.io.FastByteArrayOutputStream;
import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.ImageOutputStreamImpl;
@@ -47,8 +45,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* TIFFWriterTest

View File

@@ -2,15 +2,13 @@ package com.twelvemonkeys.imageio.metadata.tiff;
import com.twelvemonkeys.io.FastByteArrayOutputStream;
import org.junit.Test;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* HalfTest.
@@ -39,7 +37,7 @@ public class HalfTest {
@Test
public void testExactEncoding() {
for (short half = -2048; half < 2048; half++) {
assertEquals(String.valueOf(half), half, Half.shortBitsToFloat(Half.floatToShortBits(half)), 0);
assertEquals(half, Half.shortBitsToFloat(Half.floatToShortBits(half)), 0, String.valueOf(half));
}
}
@@ -103,14 +101,14 @@ public class HalfTest {
// TODO: More... But we just delegate to Float.toString, so no worries... :-)
}
@Test(expected = NullPointerException.class)
@Test
public void testParseHalfNull() {
Half.parseHalf(null);
assertThrows(NullPointerException.class, () -> Half.parseHalf(null));
}
@Test(expected = NumberFormatException.class)
@Test
public void testParseHalfBad() {
Half.parseHalf("foo");
assertThrows(NumberFormatException.class, () -> Half.parseHalf("foo"));
}
@Test
@@ -120,14 +118,14 @@ public class HalfTest {
// TODO: More... But we just delegate to Float.valueOf, so no worries... :-)
}
@Test(expected = NullPointerException.class)
@Test
public void testValueOfNull() {
Half.valueOf(null);
assertThrows(NullPointerException.class, () -> Half.valueOf(null));
}
@Test(expected = NumberFormatException.class)
@Test
public void testValueOfBad() {
Half.valueOf("foo");
assertThrows(NumberFormatException.class, () -> Half.valueOf("foo"));
}
@Test

View File

@@ -30,9 +30,8 @@
package com.twelvemonkeys.imageio.metadata.tiff;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* RationalTest
@@ -42,20 +41,20 @@ import static org.junit.Assert.assertEquals;
* @version $Id: RationalTest.java,v 1.0 Nov 18, 2009 3:23:17 PM haraldk Exp$
*/
public class RationalTest {
@Test(expected = IllegalArgumentException.class)
@Test
public void testZeroDenominator() {
new Rational(1, 0);
assertThrows(IllegalArgumentException.class, () -> new Rational(1, 0));
}
// TODO: Find a solution to this problem, as we should be able to work with it...
@Test(expected = IllegalArgumentException.class)
@Test
public void testLongMinValueNumerator() {
new Rational(Long.MIN_VALUE, 1);
assertThrows(IllegalArgumentException.class, () -> new Rational(Long.MIN_VALUE, 1));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testLongMinValueDenominator() {
new Rational(1, Long.MIN_VALUE);
assertThrows(IllegalArgumentException.class, () -> new Rational(1, Long.MIN_VALUE));
}
@Test
@@ -162,8 +161,8 @@ public class RationalTest {
assertEquals(Rational.ZERO, new Rational(0, 386).divides(x));
}
@Test(expected = ArithmeticException.class)
@Test
public void testDivideZero() {
new Rational(3037141, 3247033).divides(new Rational(0, 1));
assertThrows(ArithmeticException.class, () -> new Rational(3037141, 3247033).divides(new Rational(0, 1)));
}
}

View File

@@ -32,7 +32,10 @@ package com.twelvemonkeys.imageio.metadata.tiff;
import com.twelvemonkeys.imageio.metadata.Entry;
import com.twelvemonkeys.imageio.metadata.EntryAbstractTest;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* TIFFEntryTest
@@ -51,9 +54,9 @@ public class TIFFEntryTest extends EntryAbstractTest {
return new TIFFEntry(identifier, (short) type, value);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCreateEXIFEntryIllegalType() {
createEXIFEntry(0, null, -1);
assertThrows(IllegalArgumentException.class, () -> createEXIFEntry(0, null, -1));
}
// TODO: TIFF/EXIF specific tests

View File

@@ -32,17 +32,17 @@ package com.twelvemonkeys.imageio.metadata.tiff;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import com.twelvemonkeys.imageio.metadata.CompoundDirectory;
import com.twelvemonkeys.imageio.metadata.Directory;
@@ -86,14 +86,16 @@ public class TIFFReaderTest extends MetadataReaderAbstractTest {
assertEquals(exif.size(), exif.getDirectory(0).size() + exif.getDirectory(1).size());
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testDirectoryOutOfBounds() throws IOException {
InputStream data = getData();
CompoundDirectory exif = (CompoundDirectory) createReader().read(ImageIO.createImageInputStream(data));
assertEquals(2, exif.directoryCount());
assertNotNull(exif.getDirectory(exif.directoryCount()));
assertThrows(IndexOutOfBoundsException.class, () -> {
exif.getDirectory(exif.directoryCount());
});
}
@Test
@@ -365,21 +367,23 @@ public class TIFFReaderTest extends MetadataReaderAbstractTest {
}
}
@Test(timeout = 200)
@Test
public void testReadCyclicExifWithoutLoopOrOOME() throws IOException {
// This EXIF segment has an interesting bug...
// The bits per sample value (0x 0008 0008 0008) overwrites half the IFD1 link offset (should be 0x00000000),
// effectively making it a loop back to the IFD0 at offset 0x0000008...
try (ImageInputStream stream = ImageIO.createImageInputStream(getResource("/exif/exif-loop.bin"))) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
assertEquals(1, directory.directoryCount());
assertEquals(12, directory.getDirectory(0).size());
assertEquals("Polarr Photo Editor", directory.getDirectory(0).getEntryById(TIFF.TAG_SOFTWARE).getValue());
assertEquals("2019:02:27 09:22:59", directory.getDirectory(0).getEntryById(TIFF.TAG_DATE_TIME).getValueAsString());
}
assertTimeoutPreemptively(Duration.ofMillis(200), () -> {
try (ImageInputStream stream = ImageIO.createImageInputStream(getResource("/exif/exif-loop.bin"))) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
assertEquals(1, directory.directoryCount());
assertEquals(12, directory.getDirectory(0).size());
assertEquals("Polarr Photo Editor", directory.getDirectory(0).getEntryById(TIFF.TAG_SOFTWARE).getValue());
assertEquals("2019:02:27 09:22:59", directory.getDirectory(0).getEntryById(TIFF.TAG_DATE_TIME).getValueAsString());
}
});
}
@Test(timeout = 100)
@Test
public void testIFDLoop() throws IOException {
byte[] looping = new byte[] {
'M', 'M', 0, 42,
@@ -391,16 +395,17 @@ public class TIFFReaderTest extends MetadataReaderAbstractTest {
0, 0, 0, 0, //
0, 0, 0, 8, // IFD1 pointer
};
assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
});
}
@Test(timeout = 100)
@Test
public void testIFDLoopNested() throws IOException {
byte[] looping = new byte[] {
'M', 'M', 0, 42,
@@ -412,16 +417,17 @@ public class TIFFReaderTest extends MetadataReaderAbstractTest {
0, 0, 0, 8, // sub IFD pointer -> IFD0
0, 0, 0, 0, // End of IFD chain
};
assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
});
}
@Test(timeout = 100)
@Test
public void testSubIFDLoop() throws IOException {
byte[] looping = new byte[] {
'M', 'M', 0, 42,
@@ -439,16 +445,17 @@ public class TIFFReaderTest extends MetadataReaderAbstractTest {
0, 0, 0, 1, // count
0, 0, 0, 26, // sub IFD pointer -> sub IFD
};
assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
});
}
@Test(timeout = 100)
@Test
public void testSubIFDLoopNested() throws IOException {
byte[] looping = new byte[] {
'M', 'M', 0, 42,
@@ -466,12 +473,13 @@ public class TIFFReaderTest extends MetadataReaderAbstractTest {
0, 0, 0, 1, // count
0, 0, 0, 8, // sub IFD pointer -> IFD0
};
assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
try (ImageInputStream stream = new ByteArrayImageInputStream(looping)) {
CompoundDirectory directory = (CompoundDirectory) createReader().read(stream);
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
assertEquals(1, directory.directoryCount());
assertEquals(1, directory.size());
}
});
}
}

View File

@@ -34,7 +34,6 @@ import com.twelvemonkeys.imageio.metadata.*;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import com.twelvemonkeys.io.FastByteArrayOutputStream;
import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
@@ -47,7 +46,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* TIFFWriterTest
@@ -272,7 +272,7 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(data));
assertNotNull(read.getEntryById(TIFF.TAG_SOFTWARE));
assertTrue("value not an string array", read.getEntryById(TIFF.TAG_SOFTWARE).getValue() instanceof String[]);
assertTrue(read.getEntryById(TIFF.TAG_SOFTWARE).getValue() instanceof String[], "value not an string array");
assertArrayEquals(strings, (String[]) read.getEntryById(TIFF.TAG_SOFTWARE).getValue());
}

View File

@@ -32,11 +32,10 @@ package com.twelvemonkeys.imageio.metadata.xmp;
import com.twelvemonkeys.imageio.metadata.Entry;
import com.twelvemonkeys.imageio.metadata.EntryAbstractTest;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* XMPEntryTest

View File

@@ -32,9 +32,9 @@ package com.twelvemonkeys.imageio.metadata.xmp;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -45,6 +45,7 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -54,7 +55,6 @@ import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import com.twelvemonkeys.imageio.stream.DirectImageInputStream;
import org.junit.Test;
import com.twelvemonkeys.imageio.metadata.CompoundDirectory;
import com.twelvemonkeys.imageio.metadata.Directory;
@@ -494,24 +494,26 @@ public class XMPReaderTest extends MetadataReaderAbstractTest {
assertThat(exif.getEntryById("http://ns.adobe.com/exif/1.0/NativeDigest"), hasValue("36864,40960,40961,37121,37122,40962,40963,37510,40964,36867,36868,33434,33437,34850,34852,34855,34856,37377,37378,37379,37380,37381,37382,37383,37384,37385,37386,37396,41483,41484,41486,41487,41488,41492,41493,41495,41728,41729,41730,41985,41986,41987,41988,41989,41990,41991,41992,41993,41994,41995,41996,42016,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,22,23,24,25,26,27,28,30;A7F21D25E2C562F152B2C4ECC9E534DA"));
}
@Test(timeout = 2500L)
@Test
public void testNoExternalRequest() throws Exception {
String maliciousXML = resourceAsString("/xmp/xmp-jpeg-xxe.xml");
assertTimeoutPreemptively(Duration.ofMillis(2500L), () -> {
String maliciousXML = resourceAsString("/xmp/xmp-jpeg-xxe.xml");
try (HTTPServer server = new HTTPServer()) {
String dynamicXML = maliciousXML.replace("http://localhost:7777/", "http://localhost:" + server.port() + "/");
try (HTTPServer server = new HTTPServer()) {
String dynamicXML = maliciousXML.replace("http://localhost:7777/", "http://localhost:" + server.port() + "/");
try (DirectImageInputStream input = new DirectImageInputStream(new ByteArrayInputStream(dynamicXML.getBytes(StandardCharsets.UTF_8)));) {
createReader().read(input);
} catch (IOException ioe) {
if (ioe.getMessage().contains("501")) {
throw new AssertionError("Reading should not cause external requests", ioe);
try (DirectImageInputStream input = new DirectImageInputStream(new ByteArrayInputStream(dynamicXML.getBytes(StandardCharsets.UTF_8)));) {
createReader().read(input);
} catch (IOException ioe) {
if (ioe.getMessage().contains("501")) {
throw new AssertionError("Reading should not cause external requests", ioe);
}
// Any other exception is a bug (but might happen if the parser does not support certain features)
throw ioe;
}
// Any other exception is a bug (but might happen if the parser does not support certain features)
throw ioe;
}
}
});
}
private String resourceAsString(String name) throws IOException {

View File

@@ -30,15 +30,14 @@
package com.twelvemonkeys.imageio.metadata.xmp;
import org.junit.Test;
import java.io.*;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* XMPScannerTestCase