Skip to content

Commit 1806c82

Browse files
committed
test IndyProxyHelper
1 parent 63a501e commit 1806c82

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/IndyProxy.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
package io.opentelemetry.javaagent.bootstrap;
77

8-
/**
9-
* Interface added to indy proxies to allow unwrapping the proxy object
10-
*/
8+
/** Interface added to indy proxies to allow unwrapping the proxy object */
119
public interface IndyProxy {
1210

1311
/**

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/IndyProxyHelper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ private IndyProxyHelper() {}
1515
* @param <T> type of object to return
1616
* @param o object to unwrap
1717
* @param type expected object type
18-
* @return unwrapped proxy instance or the original object (if not a proxy) cast to the expected type
19-
* @throws IllegalArgumentException if the provided object the proxied object can't be cast to the expected type
20-
*
18+
* @return unwrapped proxy instance or the original object (if not a proxy) cast to the expected
19+
* type
20+
* @throws IllegalArgumentException if the provided object the proxied object can't be cast to the
21+
* expected type
2122
*/
2223
public static <T> T unwrapIfNeeded(Object o, Class<T> type) {
23-
if (type.isAssignableFrom(o.getClass())) {
24-
return type.cast(o);
25-
}
26-
2724
if (o instanceof IndyProxy) {
2825
Object delegate = ((IndyProxy) o).__getIndyProxyDelegate();
2926
if (type.isAssignableFrom(delegate.getClass())) {
3027
return type.cast(delegate);
3128
}
3229
}
30+
if (type.isAssignableFrom(o.getClass())) {
31+
return type.cast(o);
32+
}
3333

3434
throw new IllegalArgumentException("unexpected object type");
3535
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.bootstrap;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
11+
import org.junit.jupiter.api.Test;
12+
13+
class IndyProxyHelperTest {
14+
15+
@Test
16+
void wrongType() {
17+
assertThrows(
18+
IllegalArgumentException.class,
19+
() -> IndyProxyHelper.unwrapIfNeeded("", Integer.class));
20+
assertThrows(
21+
IllegalArgumentException.class,
22+
() -> IndyProxyHelper.unwrapIfNeeded(proxy(""), Integer.class));
23+
}
24+
25+
@Test
26+
void unwrap() {
27+
28+
// no wrapping
29+
Number number = IndyProxyHelper.unwrapIfNeeded(42, Number.class);
30+
assertThat(number).isEqualTo(42);
31+
32+
// unwrap needed
33+
String string = IndyProxyHelper.unwrapIfNeeded(proxy("hello"), String.class);
34+
assertThat(string).isEqualTo("hello");
35+
}
36+
37+
private static IndyProxy proxy(Object delegate) {
38+
return () -> delegate;
39+
}
40+
}

0 commit comments

Comments
 (0)