Skip to content

Commit

Permalink
Merge pull request #1313 from newrelic/more_unit_tests_JGB_0912
Browse files Browse the repository at this point in the history
More unit tests jgb 0912
  • Loading branch information
jbedell-newrelic authored Jun 13, 2023
2 parents b2baa48 + 5d02c32 commit 5f7eead
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public void getURI_withValidURL_returnsProperStr() throws MalformedURLException
Assert.assertEquals("https://www.newrelic.com:80/pricing", URISupport.getURI(url));
}

@Test
public void getURI_withURLSyntaxException_returnsProperStr() throws MalformedURLException {
String urlStr = "http://newrelic.com:80/pracing";
Assert.assertEquals(urlStr, URISupport.getURI(new URL(urlStr+"#`")));
}

@Test
public void getURI_withNullURL_returnsEmptyStr() throws MalformedURLException {
URL url = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.newrelic.agent.bridge.reflect;

import org.junit.Assert;
import org.junit.Test;
import sun.misc.Launcher;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ClassReflectionTest {

public ClassReflectionTest() {

}

private Object testPrivateField;
public Object testPublicField;

@Test
public void test_allGetters() throws ClassNotFoundException, NoSuchMethodException,
NoSuchFieldException, IllegalAccessException {
ClassLoader classLoader = ClassReflection.getClassLoader(ClassReflection.class);
ClassReflectionTest theInstance = new ClassReflectionTest();

Class theClass = ClassReflection.loadClass(classLoader, ClassReflectionTest.class.getName());
Assert.assertEquals("com.newrelic.agent.bridge.reflect.ClassReflectionTest", theClass.getName());

Method[] methods = ClassReflection.getDeclaredMethods(theClass);
Assert.assertNotNull(methods);
Assert.assertTrue(methods.length > 0);

methods = ClassReflection.getMethods(theClass);
Assert.assertNotNull(methods);
Assert.assertTrue(methods.length > 0);

Method method = ClassReflection.getDeclaredMethod(theClass, "test_allGetters", null);
Assert.assertNotNull(method);

Constructor<?>[] constructors = ClassReflection.getDeclaredConstructors(theClass);
Assert.assertNotNull(constructors);
Assert.assertTrue(constructors.length > 0);

Field[] fields = ClassReflection.getDeclaredFields(theClass);
Assert.assertNotNull(fields);
Assert.assertTrue(fields.length > 0);

// if we stop using Jacoco, these obviously won't work any longer
Field field = ClassReflection.getDeclaredField(theClass, "testPublicField");
Assert.assertNotNull(field);
Object value = ClassReflection.get(field, theInstance);
Assert.assertNotNull(field);
}

@Test(expected = IllegalAccessException.class)
public void test_get_privateFieldThrowsIllegalAccessException() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
ClassLoader classLoader = ClassReflection.getClassLoader(ClassReflection.class);
ClassReflectionTest theInstance = new ClassReflectionTest();

Class theClass = ClassReflection.loadClass(classLoader, ClassReflectionTest.class.getName());
Field privateField = ClassReflection.getDeclaredField(theClass, "testPrivateField");
Assert.assertNotNull(privateField);
ClassReflection.setAccessible(privateField, false);
Object value = ClassReflection.get(privateField, theInstance);
}
}
6 changes: 5 additions & 1 deletion gradle/script/java.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ jacocoTestReport {
'**/*Exception.class',
'**/*Error.class',
'**/Dummy*.class',
'**/*Type.class',
'com/newrelic/agent/InternalLimitExceeded.class',
'com/newrelic/agent/model/ApdexPerfZone.class',
'com/newrelic/agent/model/CountedDuration.class',
Expand All @@ -239,7 +240,10 @@ jacocoTestReport {
'com/newrelic/agent/model/SyntheticsIds.class',
'com/newrelic/agent/model/TimeoutCause.class',
'com/newrelic/agent/model/TransactionTiming.class',
'com/newrelic/agent/bridge/external/*Parameters.class'])
'com/newrelic/agent/bridge/external/*Parameters.class',
'com/newrelic/weave/verification/*.class',
'com/newrelic/agent/introspec/*.class'
])
}))
}
reports {
Expand Down

0 comments on commit 5f7eead

Please sign in to comment.