Skip to content

Commit

Permalink
added signature tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed May 12, 2023
1 parent 0891365 commit d0a88fe
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ private Type parseExceptionSignature(SignatureReader reader) {
* SimpleClassTypeSignature: Identifier TypeArguments?
* ClassTypeSignatureSuffix: '.' SimpleClassTypeSignature
*/
private ObjectType parseClassTypeSignature(SignatureReader reader, int dimension) {
ObjectType parseClassTypeSignature(SignatureReader reader, int dimension) {
if (reader.nextEqualsTo('L')) {
// Skip 'L'. Parse 'PackageSpecifier* SimpleClassTypeSignature'
int index = ++reader.index;
Expand Down Expand Up @@ -725,7 +725,7 @@ private TypeArgument parseTypeArgument(SignatureReader reader) {
}
}

private static boolean isAReferenceTypeSignature(SignatureReader reader) {
static boolean isAReferenceTypeSignature(SignatureReader reader) {
if (reader.available()) {
// Skip dimension
char c = reader.read();
Expand Down Expand Up @@ -753,7 +753,7 @@ private static boolean isAReferenceTypeSignature(SignatureReader reader) {
return false;
}

private static boolean isAClassTypeSignature(SignatureReader reader) {
static boolean isAClassTypeSignature(SignatureReader reader) {
if (reader.nextEqualsTo('L')) {
// Skip 'L'
reader.index++;
Expand Down Expand Up @@ -1796,11 +1796,11 @@ private static void skipAttributes(DataInput reader) throws IOException {
}
}

private static class SignatureReader {
static class SignatureReader {
private final String signature;
private final char[] array;
private final int length;
private int index;
int index;

public SignatureReader(String signature) {
this(signature, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.jd.core.v1.service.converter.classfiletojavasyntax.util;

import org.jd.core.v1.service.converter.classfiletojavasyntax.util.TypeMaker.SignatureReader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SignatureReaderTest {
private SignatureReader signatureReader;

@Before
public void setUp() {
signatureReader = new SignatureReader("Ljava/lang/String;", 0);
}

@Test
public void testRead() {
Assert.assertEquals('L', signatureReader.read());
Assert.assertEquals('j', signatureReader.read());
}

@Test
public void testNextEqualsTo() {
Assert.assertTrue(signatureReader.nextEqualsTo('L'));
Assert.assertFalse(signatureReader.nextEqualsTo('j'));
}

@Test
public void testSearch() {
Assert.assertTrue(signatureReader.search(';'));
Assert.assertEquals("Ljava/lang/String;".indexOf(';'), signatureReader.index);
Assert.assertFalse(signatureReader.search('Z'));
}

@Test
public void testAvailable() {
Assert.assertTrue(signatureReader.available());
signatureReader = new SignatureReader("", 0);
Assert.assertFalse(signatureReader.available());
}

@Test
public void testSubstring() {
signatureReader.read(); // Move index
Assert.assertEquals("Ljava", signatureReader.substring(1));
signatureReader.read(); // Move index
Assert.assertEquals("va", signatureReader.substring(2));
}

@Test
public void testToString() {
Assert.assertEquals("SignatureReader{index=0, nextChars=Ljava/lang/String;}", signatureReader.toString());
signatureReader.read(); // Move index
Assert.assertEquals("SignatureReader{index=1, nextChars=java/lang/String;}", signatureReader.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
* copy and modify the code freely for non-commercial purposes.
*/

package org.jd.core.v1;
package org.jd.core.v1.service.converter.classfiletojavasyntax.util;

import org.apache.commons.collections4.iterators.AbstractUntypedIteratorDecorator;
import org.jd.core.v1.loader.ClassPathLoader;
import org.jd.core.v1.model.javasyntax.type.ObjectType;
import org.jd.core.v1.model.javasyntax.type.TypeArguments;
import org.jd.core.v1.model.javasyntax.type.WildcardExtendsTypeArgument;
import org.jd.core.v1.model.javasyntax.type.WildcardSuperTypeArgument;
import org.jd.core.v1.service.converter.classfiletojavasyntax.util.TypeMaker;
import org.jd.core.v1.service.converter.classfiletojavasyntax.util.TypeMaker.SignatureReader;
import org.jd.core.v1.util.StringConstants;
import org.jd.core.v1.util.ZipLoader;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.PrimitiveIterator;

import static org.junit.Assert.assertThrows;

import junit.framework.TestCase;

@SuppressWarnings("all")
Expand Down Expand Up @@ -337,4 +338,125 @@ public void testIteratorNumberAndAbstractUntypedIteratorDecoratorNumberAssignmen
// Valid: iterator1 = iterator2;
assertTrue(typeMaker.isAssignable(ot1, ot2));
}

@Test
public void testIsAClassTypeSignature() {
// Case: Signature doesn't start with 'L'
assertFalse(typeMaker.isAClassTypeSignature(new SignatureReader("X")));

// Case: Signature starts with 'L' but no class name
assertTrue(typeMaker.isAClassTypeSignature(new SignatureReader("L;")));

// Case: Signature with a class name without TypeArguments
assertTrue(typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/lang/String;")));

// Case: Signature with a class name with TypeArguments
assertTrue(typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Map<Ljava/lang/String;Ljava/lang/Integer;>;")));

// Case: Signature with multiple class names separated by '.' without TypeArguments
assertTrue(typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Map.Entry;")));

// Case: Signature with multiple class names separated by '.' with TypeArguments
assertTrue(typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Map.Entry<Ljava/lang/String;Ljava/lang/Integer;>;")));

// Case: Class name starting with a digit
assertTrue(typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Map.1Entry;")));

// Case: Signature with class name but missing valid end marker
assertThrows(SignatureFormatException.class, () ->
typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/lang/String")));

// Case: Signature with class name and TypeArguments but missing valid end marker
assertThrows(SignatureFormatException.class, () ->
typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Map<Ljava/lang/String;Ljava/lang/Integer")));

// Case: Signature with multiple class names separated by '.' and TypeArguments but missing valid end marker
assertThrows(SignatureFormatException.class, () ->
typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Entry.Map<Ljava/lang/String;Ljava/lang/Integer")));

// Case: Class name starting with a digit but missing valid end marker
assertThrows(SignatureFormatException.class, () ->
typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Entry.1Map")));

// Case: Signature with TypeArguments but missing closing '>'
assertThrows(SignatureFormatException.class, () ->
typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Map<Ljava/lang/String;Ljava/lang/Integer;;")));

// Case: Signature with multiple class names separated by '.' with TypeArguments but missing closing '>'
assertThrows(SignatureFormatException.class, () ->
typeMaker.isAClassTypeSignature(new SignatureReader("Ljava/util/Map.Entry<Ljava/lang/String;Ljava/lang/Integer;;")));
}

@Test
public void testParseClassTypeSignature() {
// Case: Signature doesn't start with 'L'
assertNull(typeMaker.parseClassTypeSignature(new SignatureReader("X"), 0));

// Case: Signature starts with 'L' but no class name
assertNotNull(typeMaker.parseClassTypeSignature(new SignatureReader("L;"), 0));

// Case: Signature with a class name without TypeArguments
ObjectType objectType = typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/lang/String;"), 0);
assertNotNull(objectType);
assertEquals("java.lang.String", objectType.getQualifiedName());

// Case: Signature with a class name with TypeArguments
objectType = typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/util/Map<Ljava/lang/String;Ljava/lang/Integer;>;"), 0);
assertNotNull(objectType);

// Case: Signature with multiple class names separated by '.' without TypeArguments
objectType = typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/util/Entry.Map;"), 0);
assertNotNull(objectType);
assertEquals("java.util.Entry.Map", objectType.getQualifiedName());

// Case: Signature with multiple class names separated by '.' with TypeArguments
objectType = typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/util/Entry.Map<Ljava/lang/String;Ljava/lang/Integer;>;"), 0);
assertNotNull(objectType);

// Case: Class name starting with a digit
objectType = typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/util/Entry.1Map;"), 0);
assertNotNull(objectType);
assertNull(objectType.getQualifiedName());

// Case: Dimension greater than 0
objectType = typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/lang/String;"), 2);
assertNotNull(objectType);

// Case: Signature ends unexpectedly after 'L'
assertThrows(SignatureFormatException.class, () ->
typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/lang/String"), 0));

// Case: Signature has TypeArgument but does not close it with '>'
assertThrows(SignatureFormatException.class, () ->
typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/util/Map<Ljava/lang/String;Ljava/lang/Integer;;"), 0));

// Case: Signature ends unexpectedly after '.'
assertThrows(SignatureFormatException.class, () ->
typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/util/Map.Entry"), 0));

// Case: Signature has TypeArgument in ClassTypeSignatureSuffix but does not close it with '>'
assertThrows(SignatureFormatException.class, () ->
typeMaker.parseClassTypeSignature(new SignatureReader("Ljava/util/Map.Entry<Ljava/lang/String;Ljava/lang/Integer;;"), 0));
}


@Test
public void testIsAReferenceTypeSignature() {
// Test when reader is empty
assertFalse(typeMaker.isAReferenceTypeSignature(new SignatureReader("")));

// Test when reader's first character is '[' and subsequent character is each of 'B', 'C', 'D', 'F', 'I', 'J', 'S', 'V', 'Z'
assertTrue(typeMaker.isAReferenceTypeSignature(new SignatureReader("[B")));
// Repeat for each character

// Test when reader's first character is 'L' and rest of signature is a valid class type signature
assertTrue(typeMaker.isAReferenceTypeSignature(new SignatureReader("Ljava/lang/String;")));

// Test when reader's first character is 'T'
assertTrue(typeMaker.isAReferenceTypeSignature(new SignatureReader("T")));

// Test when reader's first character is not 'L', 'T', or any of the primitive type characters
assertFalse(typeMaker.isAReferenceTypeSignature(new SignatureReader("X")));
}

}

0 comments on commit d0a88fe

Please sign in to comment.