Skip to content

Commit

Permalink
Skip tests that aren't supported in Java 17+. Make HasPublicConstruct…
Browse files Browse the repository at this point in the history
…or constructor public. Rename/format/clean up. Eclipse encoding.

closes #89
  • Loading branch information
NathanSweet committed Nov 10, 2022
1 parent 783c59a commit cad4d75
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
68 changes: 37 additions & 31 deletions test/com/esotericsoftware/reflectasm/ConstructorAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@

package com.esotericsoftware.reflectasm;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import java.util.List;

import junit.framework.TestCase;

public class ConstructorAccessTest extends TestCase {
static private boolean java17;
static {
try {
Object version = Runtime.class.getDeclaredMethod("version").invoke(null);
java17 = ((List<Integer>)version.getClass().getDeclaredMethod("version").invoke(version)).get(0) >= 17;
} catch (Exception ignored) {
java17 = false;
}
}

public void testNewInstance () {
ConstructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class);
SomeClass someObject = new SomeClass();
Expand All @@ -28,6 +38,7 @@ public void testNewInstance () {
}

public void testPackagePrivateNewInstance () {
if (java17) return;
ConstructorAccess<PackagePrivateClass> access = ConstructorAccess.get(PackagePrivateClass.class);
PackagePrivateClass someObject = new PackagePrivateClass();
assertEquals(someObject, access.newInstance());
Expand All @@ -39,12 +50,10 @@ public void testHasArgumentConstructor () {
try {
ConstructorAccess.get(HasArgumentConstructor.class);
assertTrue(false);
}
catch (RuntimeException re) {
} catch (RuntimeException re) {
System.out.println("Expected exception happened: " + re);
}
catch (Throwable t) {
System.out.println("Unexpected exception happened: " + t);
} catch (Throwable t) {
t.printStackTrace();
assertTrue(false);
}
}
Expand All @@ -53,36 +62,34 @@ public void testHasPrivateConstructor () {
try {
ConstructorAccess.get(HasPrivateConstructor.class);
assertTrue(false);
}
catch (RuntimeException re) {
} catch (RuntimeException re) {
System.out.println("Expected exception happened: " + re);
}
catch (Throwable t) {
System.out.println("Unexpected exception happened: " + t);
} catch (Throwable t) {
t.printStackTrace();
assertTrue(false);
}
}

public void testHasProtectedConstructor () {
if (java17) return;
try {
ConstructorAccess<HasProtectedConstructor> access = ConstructorAccess.get(HasProtectedConstructor.class);
HasProtectedConstructor newInstance = access.newInstance();
assertEquals("cow", newInstance.getMoo());
}
catch (Throwable t) {
System.out.println("Unexpected exception happened: " + t);
} catch (Throwable t) {
t.printStackTrace();
assertTrue(false);
}
}

public void testHasPackageProtectedConstructor () {
public void testHasPackagePrivateConstructor () {
if (java17) return;
try {
ConstructorAccess<HasPackageProtectedConstructor> access = ConstructorAccess.get(HasPackageProtectedConstructor.class);
HasPackageProtectedConstructor newInstance = access.newInstance();
ConstructorAccess<HasPackagePrivateConstructor> access = ConstructorAccess.get(HasPackagePrivateConstructor.class);
HasPackagePrivateConstructor newInstance = access.newInstance();
assertEquals("cow", newInstance.getMoo());
}
catch (Throwable t) {
System.out.println("Unexpected exception happened: " + t);
} catch (Throwable t) {
t.printStackTrace();
assertTrue(false);
}
}
Expand All @@ -92,9 +99,8 @@ public void testHasPublicConstructor () {
ConstructorAccess<HasPublicConstructor> access = ConstructorAccess.get(HasPublicConstructor.class);
HasPublicConstructor newInstance = access.newInstance();
assertEquals("cow", newInstance.getMoo());
}
catch (Throwable t) {
System.out.println("Unexpected exception happened: " + t);
} catch (Throwable t) {
t.printStackTrace();
assertTrue(false);
}
}
Expand Down Expand Up @@ -152,7 +158,7 @@ public boolean equals (Object obj) {
return true;
}
}

static public class HasArgumentConstructor {
public String moo;

Expand All @@ -170,8 +176,8 @@ public boolean equals (Object obj) {
} else if (!moo.equals(other.moo)) return false;
return true;
}
public String getMoo() {

public String getMoo () {
return moo;
}
}
Expand All @@ -189,14 +195,14 @@ protected HasProtectedConstructor () {
}
}

static public class HasPackageProtectedConstructor extends HasProtectedConstructor {
HasPackageProtectedConstructor () {
static public class HasPackagePrivateConstructor extends HasProtectedConstructor {
HasPackagePrivateConstructor () {
super();
}
}

static public class HasPublicConstructor extends HasPackageProtectedConstructor {
HasPublicConstructor () {
static public class HasPublicConstructor extends HasPackagePrivateConstructor {
public HasPublicConstructor () {
super();
}
}
Expand Down

0 comments on commit cad4d75

Please sign in to comment.