diff --git a/build.gradle b/build.gradle index c64bd08..f9f507e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,21 +1,21 @@ -apply plugin: 'java' -apply plugin: 'jacoco' -apply plugin: 'com.github.kt3k.coveralls' +plugins { + id 'java' + id 'jacoco' + id 'com.github.kt3k.coveralls' version '2.8.4' +} version = '0.1.7' repositories { mavenCentral() - jcenter() + // You can include other repositories if needed } -buildscript { - repositories { - mavenCentral() - } - dependencies { - classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.4.0' - } +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3' + testImplementation 'org.mockito:mockito-core:5.9.0' + // testImplementation 'org.mockito:mockito-inline:5.2.0' } allprojects { @@ -26,22 +26,28 @@ allprojects { jacocoTestReport { reports { - html.enabled = true - xml.enabled = true - csv.enabled = false + html.required.set(true) + xml.required.set(true) + csv.required.set(false) } } -dependencies { - testCompile 'junit:junit:4.12' - testCompile 'org.mockito:mockito-core:1.10.19' -} - -jar { +tasks.jar { manifest { attributes 'Main-Class': 'diff.rednaga.AXMLPrinter' } - processResources.inputs.property('version', version) - processResources.expand('version': version) -} \ No newline at end of file + processResources { + inputs.property('version', version) + expand('version': version) + } +} + +test { + useJUnitPlatform() + jvmArgs( + '--add-opens', 'java.base/java.lang=ALL-UNNAMED', + '--add-opens', 'java.base/java.io=ALL-UNNAMED', + '--add-opens', 'java.base/java.util=ALL-UNNAMED' + ) +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b0acbdc..2fa91c5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/android/content/res/IntReader.java b/src/main/java/android/content/res/IntReader.java index d7f7b0b..4a314c4 100644 --- a/src/main/java/android/content/res/IntReader.java +++ b/src/main/java/android/content/res/IntReader.java @@ -17,6 +17,8 @@ import java.io.EOFException; import java.io.IOException; import java.io.InputStream; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Simple helper class that allows reading of integers. @@ -27,10 +29,16 @@ */ public class IntReader { + private static final int BYTE_LENGTH = 1; + private static final int SHORT_LENGTH = 2; + private static final int INT_LENGTH = 4; + private InputStream stream; private boolean bigEndian; private int bytesRead; + private static final Logger LOGGER = Logger.getLogger(IntReader.class.getName()); + public IntReader(InputStream stream, boolean bigEndian) { reset(stream, bigEndian); } @@ -55,22 +63,22 @@ public void close() { try { stream.close(); } catch (IOException e) { - e.printStackTrace(); + LOGGER.log(Level.SEVERE, "Error closing stream", e); } reset(null, false); } } public int readByte() throws IOException { - return readInt(1); + return readInt(BYTE_LENGTH); } - + public int readShort() throws IOException { - return readInt(2); + return readInt(SHORT_LENGTH); } - + public int readInt() throws IOException { - return readInt(4); + return readInt(INT_LENGTH); } /** @@ -81,7 +89,7 @@ public int readInt() throws IOException { * @throws IOException */ public int readInt(int length) throws IOException { - if ((length < 0) || (length > 4)) { + if ((length < 0) || (length > INT_LENGTH)) { throw new IllegalArgumentException(); } int result = 0; @@ -126,7 +134,7 @@ public void skip(int bytes) throws IOException { } public void skipInt() throws IOException { - skip(4); + skip(INT_LENGTH); } public int getBytesRead() { diff --git a/src/main/java/android/content/res/chunk/sections/StringSection.java b/src/main/java/android/content/res/chunk/sections/StringSection.java index 61aea6f..0f567cc 100644 --- a/src/main/java/android/content/res/chunk/sections/StringSection.java +++ b/src/main/java/android/content/res/chunk/sections/StringSection.java @@ -25,6 +25,7 @@ import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; public class StringSection extends GenericChunkSection implements Chunk, ChunkSection { @@ -82,37 +83,61 @@ public void readSection(IntReader inputReader) throws IOException { } } - // TODO : Ensure we goto the proper offset in the case it isn't in proper order - private void readPool(ArrayList pool, int flags, IntReader inputReader) throws IOException { - int offset = 0; - for (PoolItem item : pool) { - // TODO: This assumes that the pool is ordered... - inputReader.skip(item.getOffset() - offset); - offset = item.getOffset(); +private void readPool(ArrayList pool, int flags, IntReader inputReader) throws IOException { + // Create a list of indices to maintain the original order + List indices = new ArrayList<>(); + for (int i = 0; i < pool.size(); i++) { + indices.add(i); + } + + // Sort indices based on the offsets of the pool items + indices.sort((i1, i2) -> Integer.compare(pool.get(i1).getOffset(), pool.get(i2).getOffset())); + + int currentStreamPosition = 0; + + // Temporary storage for strings + String[] results = new String[pool.size()]; - int length = 0; + // Read data in the order determined by sorted indices + for (int index : indices) { + PoolItem item = pool.get(index); + int targetOffset = item.getOffset(); + + // Move to the target offset, skip forward if needed + if (targetOffset > currentStreamPosition) { + inputReader.skip(targetOffset - currentStreamPosition); + currentStreamPosition = targetOffset; + } + + int length = 0; + if ((flags & UTF8_FLAG) != 0) { + length = inputReader.readByte(); + currentStreamPosition += 1; + } else { + length = inputReader.readShort(); + currentStreamPosition += 2; + } + + StringBuilder result = new StringBuilder(length); + for (; length != 0; length -= 1) { if ((flags & UTF8_FLAG) != 0) { - length = inputReader.readByte(); - offset += 1; + result.append((char) inputReader.readByte()); + currentStreamPosition += 1; } else { - length = inputReader.readShort(); - offset += 2; + result.append((char) inputReader.readShort()); + currentStreamPosition += 2; } + } - StringBuilder result = new StringBuilder(length); - for (; length != 0; length -= 1) { - if ((flags & UTF8_FLAG) != 0) { - result.append((char) inputReader.readByte()); - offset += 1; - } else { - result.append((char) inputReader.readShort()); - offset += 2; - } - } + // Store the result temporarily + results[index] = result.toString(); + } - item.setString(result.toString()); - } + // Assign results back to the pool items in the original order + for (int i = 0; i < pool.size(); i++) { + pool.get(i).setString(results[i]); } +} public int getStringIndex(String string) { if (string != null) { diff --git a/src/test/java/android/content/res/TestAXMLResource.java b/src/test/java/android/content/res/TestAXMLResource.java index 28fc963..bc04509 100644 --- a/src/test/java/android/content/res/TestAXMLResource.java +++ b/src/test/java/android/content/res/TestAXMLResource.java @@ -3,10 +3,9 @@ import android.content.res.chunk.AttributeType; import android.content.res.chunk.types.Attribute; import android.content.res.chunk.types.StartTag; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; @@ -16,15 +15,16 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author tstrazzere */ -@RunWith(Enclosed.class) public class TestAXMLResource { - public static class FunctionalTest { + + @Nested + class FunctionalTest { // Legacy files from original repo String[] oldTestFiles = {"test.xml", "test1.xml", "test2.xml", "test3.xml"}; @@ -34,7 +34,7 @@ public static class FunctionalTest { AXMLResource underTest; - @Before + @BeforeEach public void setUp() { underTest = new AXMLResource(); } @@ -128,4 +128,4 @@ public void testWriteInsertedApplicationAttribute() throws IOException { "test"); } } -} +} \ No newline at end of file diff --git a/src/test/java/android/content/res/TestChunkUtil.java b/src/test/java/android/content/res/TestChunkUtil.java index 741b2e2..c472eff 100644 --- a/src/test/java/android/content/res/TestChunkUtil.java +++ b/src/test/java/android/content/res/TestChunkUtil.java @@ -1,14 +1,13 @@ package android.content.res; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; -import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; import android.content.res.chunk.ChunkType; import android.content.res.chunk.ChunkUtil; @@ -25,9 +24,10 @@ /** * @author tstrazzere */ -@RunWith(Enclosed.class) public class TestChunkUtil { - public static class UnitTest { + + @Nested + class UnitTest { @Test public void testReadChunkType() throws IOException { @@ -66,7 +66,7 @@ public void testCreateResourceSection() throws IOException { when(mockReader.readInt()).thenReturn(ChunkType.RESOURCE_SECTION.getIntType(), 8, 0); if (!(ChunkUtil.createChunk(mockReader) instanceof ResourceSection)) { - throw new AssertionError("Expected AXMLHeader chunk!"); + throw new AssertionError("Expected ResourceSection chunk!"); } } @@ -76,7 +76,7 @@ public void testCreateStringSection() throws IOException { when(mockReader.readInt()).thenReturn(ChunkType.STRING_SECTION.getIntType(), 0); if (!(ChunkUtil.createChunk(mockReader) instanceof StringSection)) { - throw new AssertionError("Expected AXMLHeader chunk!"); + throw new AssertionError("Expected StringSection chunk!"); } } @@ -86,7 +86,7 @@ public void testCreateBuffer() throws IOException { when(mockReader.readInt()).thenReturn(ChunkType.BUFFER.getIntType(), 0); if (!(ChunkUtil.createChunk(mockReader) instanceof Buffer)) { - throw new AssertionError("Expected AXMLHeader chunk!"); + throw new AssertionError("Expected Buffer chunk!"); } } @@ -109,7 +109,6 @@ public void testCreateNameSpace() throws IOException { } assertEquals(false, ((NameSpace) chunk).isStart()); - } @Test @@ -145,4 +144,4 @@ public void testCreateTextTag() throws IOException { } } } -} +} \ No newline at end of file diff --git a/src/test/java/android/content/res/TestIntReader.java b/src/test/java/android/content/res/TestIntReader.java index cf76545..a13ac06 100644 --- a/src/test/java/android/content/res/TestIntReader.java +++ b/src/test/java/android/content/res/TestIntReader.java @@ -1,6 +1,7 @@ package android.content.res; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -8,55 +9,62 @@ import java.io.IOException; import java.io.InputStream; +import java.io.EOFException; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; /** * @author tstrazzere */ -@RunWith(Enclosed.class) public class TestIntReader { - public static class UnitTest { + @Nested + class UnitTest { InputStream mockStream; - IntReader underTest; - @Before + @BeforeEach public void setUp() { mockStream = mock(InputStream.class); - underTest = new IntReader(mockStream, true); + underTest = new IntReader(mockStream, true); // Start with big-endian mode } @Test public void testClose() throws IOException { underTest.close(); - verify(mockStream, times(1)).close(); } @Test public void testReadShort() throws IOException { - underTest.readShort(); + // Ensure the mock returns exactly two bytes for the short read + when(mockStream.read()).thenReturn(0x01, 0x02, -1); + // Expect 0x0102 in big-endian order + assertEquals((0x01 << 8) | 0x02, underTest.readShort()); verify(mockStream, times(2)).read(); } @Test public void testReadByte() throws IOException { - underTest.readByte(); + // Ensure the mock returns exactly one byte + when(mockStream.read()).thenReturn(0x01, -1); + // Expect single byte value 0x01 + assertEquals(0x01, underTest.readByte()); verify(mockStream, times(1)).read(); } @Test public void testReadInt() throws IOException { - underTest.readInt(); + // Ensure the mock returns exactly four bytes for the integer read + when(mockStream.read()).thenReturn(0x01, 0x02, 0x03, 0x04, -1); + // Expect 0x01020304 in big-endian order + assertEquals((0x01 << 24) | (0x02 << 16) | (0x03 << 8) | 0x04, underTest.readInt()); verify(mockStream, times(4)).read(); } @@ -68,14 +76,15 @@ public void testCloseDoesntDieWithNull() { @Test public void testSkips() throws IOException { + // Ensure the mock simulates skipping 8 and 4 bytes when(mockStream.skip(8)).thenReturn(8L); when(mockStream.skip(4)).thenReturn(4L); - // Nothing happens + // Skip no bytes underTest.skip(0); - // Skip 8 + // Skip 8 bytes underTest.skip(8); - // Skip 4 + // Skip 4 bytes (for int) underTest.skipInt(); verify(mockStream, times(1)).skip(8); @@ -84,65 +93,50 @@ public void testSkips() throws IOException { @Test public void testSkipFails() throws IOException { - when(mockStream.skip(4)).thenReturn(-1L); - - try { - underTest.skipInt(); - throw new AssertionError("Excepted exception!"); - } catch (IOException exception) { - // Good case - } + // Simulate a failed skip + when(mockStream.skip(4)).thenReturn(0L); + + assertThrows(EOFException.class, () -> underTest.skipInt()); } @Test public void testReadIntFailsBadParams() { - try { - underTest.readInt(-1); - throw new AssertionError("Excepted exception!"); - } catch (IllegalArgumentException exception) { - // Good case - } catch (IOException exception) { - throw new AssertionError("Unexcepted exception!"); - } - - try { - underTest.readInt(5); - throw new IllegalArgumentException("Excepted exception!"); - } catch (IllegalArgumentException exception) { - // Good case - } catch (IOException exception) { - throw new AssertionError("Unexcepted exception!"); - } + assertThrows(IllegalArgumentException.class, () -> underTest.readInt(-1)); + assertThrows(IllegalArgumentException.class, () -> underTest.readInt(5)); } @Test public void testReadIntFailsEOF() throws IOException { + // Simulate end-of-stream (EOF) when(mockStream.read()).thenReturn(-1); - try { - underTest.readInt(2); - throw new AssertionError("Excepted exception!"); - } catch (IOException exception) { - // Good case - } + + assertThrows(EOFException.class, () -> underTest.readInt(2)); } @Test public void testReadIntBigEndian() throws IOException { - when(mockStream.read()).thenReturn(10).thenReturn(20).thenReturn(30).thenReturn(40); + // Ensure the mock returns four bytes + when(mockStream.read()).thenReturn(0x0A, 0x14, 0x1E, 0x28, -1); - assertEquals(10, underTest.readInt(1)); + // First read: expect 0x0A + assertEquals(0x0A, underTest.readInt(1)); - assertEquals(((20 << 16) | (30 << 8) | (40 << 0)), underTest.readInt(3)); + // Next three bytes read: expect 0x141E28 in big-endian order + assertEquals((0x14 << 16) | (0x1E << 8) | 0x28, underTest.readInt(3)); } @Test public void testReadIntLittleEndian() throws IOException { - underTest = new IntReader(mockStream, false); - when(mockStream.read()).thenReturn(10).thenReturn(20).thenReturn(30).thenReturn(40); + underTest = new IntReader(mockStream, false); // Little-endian mode + + // Ensure the mock returns four bytes + when(mockStream.read()).thenReturn(0x0A, 0x14, 0x1E, 0x28, -1); - assertEquals(10, underTest.readInt(1)); + // First read: expect 0x0A + assertEquals(0x0A, underTest.readInt(1)); - assertEquals(((20 << 0) | (30 << 8) | (40 << 16)), underTest.readInt(3)); + // Next three bytes read: expect 0x281E14 in little-endian order + assertEquals(0x14 | (0x1E << 8) | (0x28 << 16), underTest.readInt(3)); } } -} +} \ No newline at end of file diff --git a/src/test/java/android/content/res/TestIssue8.java b/src/test/java/android/content/res/TestIssue8.java index 8562d1e..8b157a5 100644 --- a/src/test/java/android/content/res/TestIssue8.java +++ b/src/test/java/android/content/res/TestIssue8.java @@ -3,10 +3,9 @@ import android.content.res.chunk.AttributeType; import android.content.res.chunk.types.Attribute; import android.content.res.chunk.types.StartTag; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; @@ -16,22 +15,23 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author tstrazzere */ -@RunWith(Enclosed.class) public class TestIssue8 { - public static class FunctionalTest { + + @Nested + class FunctionalTest { // Large file with weird tricks that broke tools in the past String issue8 = "qihoo_jiagu_issue8.xml"; AXMLResource underTest; - @Before + @BeforeEach public void setUp() { underTest = new AXMLResource(); } @@ -52,7 +52,7 @@ public void testToXml() throws IOException, ParserConfigurationException { underTest = new AXMLResource(testStream); String xml = underTest.toXML(); - try { + // try { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes())); Node manifestNode = document.getFirstChild(); NamedNodeMap manifestNodeAttributes = manifestNode.getAttributes(); @@ -60,10 +60,10 @@ public void testToXml() throws IOException, ParserConfigurationException { assertEquals("3133", manifestNodeAttributes.getNamedItem("android:versionCode").getNodeValue()); assertEquals("1.9.3", manifestNodeAttributes.getNamedItem("android:versionName").getNodeValue()); assertEquals("com.faithcomesbyhearing.android.pt.bibleis", manifestNodeAttributes.getNamedItem("package").getNodeValue()); - } catch (SAXException e) { - // Is not xml - assertTrue(false); - } + // } catch (SAXException e) { + // // Is not xml + // assertTrue(false); + // } } @Test @@ -115,4 +115,4 @@ public void testWriteInsertedApplicationAttribute() throws IOException { "test"); } } -} +} \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/sections/ResourceSectionTest.java b/src/test/java/android/content/res/chunk/sections/ResourceSectionTest.java index 978b81c..86d67f3 100644 --- a/src/test/java/android/content/res/chunk/sections/ResourceSectionTest.java +++ b/src/test/java/android/content/res/chunk/sections/ResourceSectionTest.java @@ -18,18 +18,19 @@ import android.content.res.IntReader; import android.content.res.chunk.ChunkType; import android.content.res.chunk.types.GenericChunk; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author tstrazzere */ -public class ResourceSectionTest extends TestCase { +public class ResourceSectionTest { // Implement a new type of Generic chunk just to test specific functionality protected class TestResourceSection extends ResourceSection { public TestResourceSection(ChunkType chunkType, IntReader inputReader) { @@ -51,21 +52,20 @@ public void readSection(IntReader inputReader) throws IOException { private IntReader mockReader; private ChunkType mockChunkType; - - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the size read -- however this should /not/ be the output! // We expect that the section truncates the to proper length since it's assumed to have been // modified since the size differs when(mockReader.readInt()).thenReturn(0xBB60); - mockChunkType = mock(ChunkType.class); + mockChunkType = ChunkType.RESOURCE_SECTION; underTest = new TestResourceSection(mockChunkType, mockReader); } + @Test public void testToBytes() throws Exception { // (chunk type) RESOURCE_SECTION + (size) 0xBB60 + (int[]) resourceIDs byte[] expected = { @@ -85,10 +85,8 @@ public void testToBytes() throws Exception { (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; - when(mockChunkType.getIntType()).thenReturn(ChunkType.RESOURCE_SECTION.getIntType()); - byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/sections/StringSectionTest.java b/src/test/java/android/content/res/chunk/sections/StringSectionTest.java index b8c5595..479ee4c 100644 --- a/src/test/java/android/content/res/chunk/sections/StringSectionTest.java +++ b/src/test/java/android/content/res/chunk/sections/StringSectionTest.java @@ -2,25 +2,25 @@ import android.content.res.IntReader; import android.content.res.chunk.ChunkType; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * Created by diff on 9/29/15. */ -public class StringSectionTest extends TestCase { +public class StringSectionTest { private StringSection underTest; private IntReader mockReader; private ChunkType mockChunkType; - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the string section data when(mockReader.readInt()).thenReturn( @@ -47,12 +47,12 @@ public void setUp() throws Exception { 0x69, // i 0x6D); // m - mockChunkType = mock(ChunkType.class); - when(mockChunkType.getIntType()).thenReturn(ChunkType.STRING_SECTION.getIntType()); + mockChunkType = ChunkType.STRING_SECTION; underTest = new StringSection(mockChunkType, mockReader); } + @Test public void testToBytes() throws Exception { byte[] expected = { // STRING_SECTION @@ -85,6 +85,6 @@ public void testToBytes() throws Exception { }; byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/types/AttributeTest.java b/src/test/java/android/content/res/chunk/types/AttributeTest.java index ea529d2..d3caede 100644 --- a/src/test/java/android/content/res/chunk/types/AttributeTest.java +++ b/src/test/java/android/content/res/chunk/types/AttributeTest.java @@ -16,25 +16,24 @@ package android.content.res.chunk.types; import android.content.res.IntReader; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author tstrazzere */ -public class AttributeTest extends TestCase { +public class AttributeTest { private Attribute underTest; private IntReader mockReader; - - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the attribute data when(mockReader.readInt()).thenReturn(0x35, 0x60, 0x10, 0x3000008, 0x2C); @@ -42,6 +41,7 @@ public void setUp() throws Exception { underTest = new Attribute(mockReader); } + @Test public void testToBytes() throws Exception { byte[] expected = { // uri @@ -57,6 +57,6 @@ public void testToBytes() throws Exception { }; byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/types/EndTagTest.java b/src/test/java/android/content/res/chunk/types/EndTagTest.java index 26c3190..772e0b8 100644 --- a/src/test/java/android/content/res/chunk/types/EndTagTest.java +++ b/src/test/java/android/content/res/chunk/types/EndTagTest.java @@ -17,35 +17,35 @@ import android.content.res.IntReader; import android.content.res.chunk.ChunkType; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author tstrazzere */ -public class EndTagTest extends TestCase { +public class EndTagTest { private EndTag underTest; private IntReader mockReader; private ChunkType mockChunkType; - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the end tag data when(mockReader.readInt()).thenReturn(0x18, 0x19, 0xFFFFFFFF, 0xFFFFFFFF, 0x46); - mockChunkType = mock(ChunkType.class); - when(mockChunkType.getIntType()).thenReturn(ChunkType.END_TAG.getIntType()); + mockChunkType = ChunkType.END_TAG; underTest = new EndTag(mockChunkType, mockReader); } + @Test public void testToBytes() throws Exception { byte[] expected = { // END_TAG @@ -63,6 +63,6 @@ public void testToBytes() throws Exception { }; byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/types/GenericChunkTest.java b/src/test/java/android/content/res/chunk/types/GenericChunkTest.java index 1338730..c790604 100644 --- a/src/test/java/android/content/res/chunk/types/GenericChunkTest.java +++ b/src/test/java/android/content/res/chunk/types/GenericChunkTest.java @@ -19,19 +19,20 @@ import android.content.res.chunk.ChunkType; import android.content.res.chunk.sections.ResourceSection; import android.content.res.chunk.sections.StringSection; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.nio.ByteBuffer; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author tstrazzere */ -public class GenericChunkTest extends TestCase { +public class GenericChunkTest { // Implement a new type of Generic chunk just to test specific functionality protected class TestChunk extends GenericChunk { @@ -62,20 +63,18 @@ public byte[] toBytes() { private IntReader mockReader; private ChunkType mockChunkType; - - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the size read when(mockReader.readInt()).thenReturn(0xBB60); - mockChunkType = mock(ChunkType.class); - when(mockChunkType.getIntType()).thenReturn(ChunkType.AXML_HEADER.getIntType()); + mockChunkType = ChunkType.AXML_HEADER; underTest = new TestChunk(mockChunkType, mockReader); } + @Test public void testToBytes() throws Exception { // (chunk type) AXML_HEADER + (size) 0xBB60 + (TestChunk.toBytes() addition) 0xFF byte[] expected = { @@ -89,6 +88,6 @@ public void testToBytes() throws Exception { byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/types/NameSpaceTest.java b/src/test/java/android/content/res/chunk/types/NameSpaceTest.java index 43856be..bf020c5 100644 --- a/src/test/java/android/content/res/chunk/types/NameSpaceTest.java +++ b/src/test/java/android/content/res/chunk/types/NameSpaceTest.java @@ -17,35 +17,35 @@ import android.content.res.IntReader; import android.content.res.chunk.ChunkType; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author tstrazzere */ -public class NameSpaceTest extends TestCase { +public class NameSpaceTest { private NameSpace underTest; private IntReader mockReader; private ChunkType mockChunkType; - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the namespace data when(mockReader.readInt()).thenReturn(0x18, 0x19, 0xFFFFFFFF, 0x5C, 0x12); - mockChunkType = mock(ChunkType.class); - when(mockChunkType.getIntType()).thenReturn(ChunkType.START_NAMESPACE.getIntType()); + mockChunkType = ChunkType.START_NAMESPACE; underTest = new NameSpace(mockChunkType, mockReader); } + @Test public void testToBytes() throws Exception { byte[] expected = { // END_TAG @@ -63,6 +63,6 @@ public void testToBytes() throws Exception { }; byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/types/StartTagTest.java b/src/test/java/android/content/res/chunk/types/StartTagTest.java index 68871ce..afc37d8 100644 --- a/src/test/java/android/content/res/chunk/types/StartTagTest.java +++ b/src/test/java/android/content/res/chunk/types/StartTagTest.java @@ -17,35 +17,35 @@ import android.content.res.IntReader; import android.content.res.chunk.ChunkType; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author tstrazzere */ -public class StartTagTest extends TestCase { +public class StartTagTest { private StartTag underTest; private IntReader mockReader; private ChunkType mockChunkType; - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the namespace data when(mockReader.readInt()).thenReturn(9 * 4, 0x17, 0xFFFFFFFF, 0xFFFFFFFF, 0x41, 0x140014, 0, 0); - mockChunkType = mock(ChunkType.class); - when(mockChunkType.getIntType()).thenReturn(ChunkType.START_TAG.getIntType()); + mockChunkType = ChunkType.START_TAG; underTest = new StartTag(mockChunkType, mockReader); } + @Test public void testToBytes() throws Exception { byte[] expected = { // START_TAG @@ -69,6 +69,6 @@ public void testToBytes() throws Exception { }; byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/android/content/res/chunk/types/TextTagTest.java b/src/test/java/android/content/res/chunk/types/TextTagTest.java index ee6f3e5..5da59ab 100644 --- a/src/test/java/android/content/res/chunk/types/TextTagTest.java +++ b/src/test/java/android/content/res/chunk/types/TextTagTest.java @@ -17,35 +17,35 @@ import android.content.res.IntReader; import android.content.res.chunk.ChunkType; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author tstrazzere */ -public class TextTagTest extends TestCase { +public class TextTagTest { private TextTag underTest; private IntReader mockReader; private ChunkType mockChunkType; - @Override + @BeforeEach public void setUp() throws Exception { - super.setUp(); mockReader = mock(IntReader.class); // Mock the text tag data when(mockReader.readInt()).thenReturn(7 * 4, 0x17, 0xFFFFFFFF, 0x1C, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); - mockChunkType = mock(ChunkType.class); - when(mockChunkType.getIntType()).thenReturn(ChunkType.TEXT_TAG.getIntType()); + mockChunkType = ChunkType.TEXT_TAG; underTest = new TextTag(mockChunkType, mockReader); } + @Test public void testToBytes() throws Exception { byte[] expected = { // TEXT_TAG @@ -65,6 +65,6 @@ public void testToBytes() throws Exception { }; byte[] actual = underTest.toBytes(); - Assert.assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } \ No newline at end of file