Skip to content

Commit 720f52a

Browse files
committed
added LazyLoadingProxy.isResolved() (DE-805, #271)
1 parent 10361b6 commit 720f52a

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/main/java/com/arangodb/springframework/core/convert/resolver/AbstractResolver.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ public abstract class AbstractResolver<A extends Annotation> {
4545

4646
private static final Method GET_ENTITY_METHOD;
4747
private static final Method GET_REF_ID_METHOD;
48+
private static final Method IS_RESOLVED;
4849

4950
static {
5051
try {
5152
GET_ENTITY_METHOD = LazyLoadingProxy.class.getMethod("getEntity");
5253
GET_REF_ID_METHOD = LazyLoadingProxy.class.getMethod("getRefId");
54+
IS_RESOLVED = LazyLoadingProxy.class.getMethod("isResolved");
5355
} catch (final Exception e) {
5456
throw new RuntimeException(e);
5557
}
@@ -140,6 +142,11 @@ public Object intercept(final Object obj, final Method method, final Object[] ar
140142
if (GET_REF_ID_METHOD.equals(method)) {
141143
return id;
142144
}
145+
146+
if (IS_RESOLVED.equals(method)) {
147+
return resolved;
148+
}
149+
143150
if (method.getName().equals("canEqual")) {
144151
return proxyCanEqual(ensureResolved(), args[0]);
145152
}

src/main/java/com/arangodb/springframework/core/convert/resolver/LazyLoadingProxy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ public interface LazyLoadingProxy {
4141
*/
4242
String getRefId();
4343

44+
/**
45+
* @return whether the proxy is resolved
46+
*/
47+
boolean isResolved();
48+
4449
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.springframework.core.mapping;
22+
23+
import com.arangodb.springframework.AbstractArangoTest;
24+
import com.arangodb.springframework.annotation.Ref;
25+
import com.arangodb.springframework.core.convert.resolver.LazyLoadingProxy;
26+
import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity;
27+
import org.junit.jupiter.api.Test;
28+
29+
import static org.hamcrest.MatcherAssert.assertThat;
30+
import static org.hamcrest.Matchers.*;
31+
32+
public class LazyLoadingProxyTest extends AbstractArangoTest {
33+
34+
public static class SingleReferenceLazyTestEntity extends BasicTestEntity {
35+
@Ref(lazy = true)
36+
private BasicTestEntity entity;
37+
}
38+
39+
@Test
40+
public void isResolved() {
41+
final BasicTestEntity e1 = new BasicTestEntity();
42+
template.insert(e1);
43+
final SingleReferenceLazyTestEntity e0 = new SingleReferenceLazyTestEntity();
44+
e0.entity = e1;
45+
template.insert(e0);
46+
final SingleReferenceLazyTestEntity document = template.find(e0.id, SingleReferenceLazyTestEntity.class).get();
47+
assertThat(document, is(notNullValue()));
48+
assertThat(document.entity, is(notNullValue()));
49+
assertThat(document.entity, instanceOf(LazyLoadingProxy.class));
50+
assertThat(document.entity, instanceOf(BasicTestEntity.class));
51+
assertThat(((LazyLoadingProxy) document.entity).isResolved(), is(false));
52+
assertThat(document.entity.getId(), is(e1.getId()));
53+
assertThat(((LazyLoadingProxy) document.entity).isResolved(), is(true));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)