Skip to content

Commit da7349a

Browse files
committed
Merge pull request #8 from tporadowski/entityfactory-gson-config
EntityFactory allows fine-tuning of Gson
2 parents 0677946 + ba1eff5 commit da7349a

File tree

5 files changed

+154
-4
lines changed

5 files changed

+154
-4
lines changed

src/main/java/com/arangodb/entity/EntityFactory.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class EntityFactory {
4040
private static Gson gson;
4141
private static Gson gsonNull;
4242

43-
private static GsonBuilder getBuilder() {
43+
public static GsonBuilder getGsonBuilder() {
4444
return new GsonBuilder()
4545
.addSerializationExclusionStrategy(new ExcludeExclusionStrategy(true))
4646
.addDeserializationExclusionStrategy(new ExcludeExclusionStrategy(false))
@@ -101,10 +101,32 @@ private static GsonBuilder getBuilder() {
101101
}
102102

103103
static {
104-
gson = getBuilder().create();
105-
gsonNull = getBuilder().serializeNulls().create();
104+
configure(getGsonBuilder());
106105
}
107106

107+
/**
108+
* Configures instances of Gson used by this factory.
109+
*
110+
* @param builders one or two GsonBuilder instances. If only one is provided it will be used for
111+
* initializing both <code>gson</code> and <code>gsonNull</code> fields (latter with
112+
* <code>serializeNulls()</code> called prior to creating). If two are given - first initializes <code>gson</code>
113+
* field, second initializes <code>gsonNull</code> (used when serialization of nulls is requested).
114+
*/
115+
public static void configure(GsonBuilder ...builders) {
116+
if (builders.length < 1) {
117+
throw new IllegalArgumentException("builders");
118+
}
119+
120+
gson = builders[0].create();
121+
122+
if (builders.length > 1) {
123+
gsonNull = builders[1].create();
124+
} else {
125+
//use the first one again, but with nulls serialization turned on
126+
gsonNull = builders[0].serializeNulls().create();
127+
}
128+
}
129+
108130
public static <T> T createEntity(String jsonText, Type type) {
109131
return gson.fromJson(jsonText, type);
110132
}

src/test/java/com/arangodb/ArangoDriverDocumentTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import com.arangodb.entity.CollectionEntity;
3838
import com.arangodb.entity.DocumentEntity;
39+
import com.arangodb.entity.EntityFactory;
3940

4041
/**
4142
* @author tamtam180 - kirscheless at gmail.com
@@ -54,7 +55,8 @@ public ArangoDriverDocumentTest(ArangoConfigure configure, ArangoDriver driver)
5455

5556
CollectionEntity col1;
5657
CollectionEntity col2;
57-
58+
TestInterfaceInstanceCreator testInstanceCreator;
59+
5860
@Before
5961
public void before() throws ArangoException {
6062

@@ -72,12 +74,18 @@ public void before() throws ArangoException {
7274
col1 = driver.createCollection(collectionName);
7375
col2 = driver.createCollection(collectionName2);
7476

77+
//configure Gson to use our instance creator whenever documents of TestInterface are requested
78+
testInstanceCreator = new TestInterfaceInstanceCreator();
79+
EntityFactory.configure(EntityFactory.getGsonBuilder().registerTypeAdapter(TestInterface.class, testInstanceCreator));
80+
7581
logger.debug("--");
7682

7783
}
7884

7985
@After
8086
public void after() {
87+
//revert to default configuration
88+
EntityFactory.configure(EntityFactory.getGsonBuilder());
8189
logger.debug("----------");
8290
}
8391

@@ -320,7 +328,27 @@ public void test_get_document() throws ArangoException {
320328
assertThat(retVal.getEntity().getDesc(), is("説明:9999"));
321329
assertThat(retVal.getEntity().getAge(), is(9999));
322330
}
331+
332+
@Test
333+
public void test_get_document_with_instance_creator() throws ArangoException {
334+
//save an instance of TestInterfaceImpl with null as "name"
335+
DocumentEntity<TestInterfaceImpl> doc = driver.createDocument(collectionName, new TestInterfaceImpl(null), null, false);
336+
337+
assertThat(doc.getDocumentKey(), is(notNullValue()));
338+
assertThat(doc.getDocumentHandle(), is(collectionName + "/" + doc.getDocumentKey()));
339+
assertThat(doc.getDocumentRevision(), is(not(0L)));
323340

341+
//now we should get back an instance created with our configured InstanceCreator<TestInterface> (with "name" already set)
342+
DocumentEntity<TestInterface> retVal = driver.getDocument(doc.getDocumentHandle(), TestInterface.class);
343+
assertThat(retVal.getDocumentHandle(), is(doc.getDocumentHandle()));
344+
assertThat(retVal.getDocumentRevision(), is(doc.getDocumentRevision()));
345+
assertThat(retVal.getDocumentKey(), is(doc.getDocumentKey()));
346+
assertThat(retVal.getEntity(), instanceOf(TestInterface.class));
347+
assertThat(retVal.getEntity(), instanceOf(TestInterfaceImpl.class));
348+
assertThat(testInstanceCreator.getCounter(), is(1));
349+
assertThat(retVal.getEntity().getName(), is("name 0"));
350+
}
351+
324352
@Test
325353
public void test_get_document_collection_not_found() throws ArangoException {
326354
// Get
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2015 tporadowski
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb;
18+
19+
/**
20+
* @author tporadowski - tomasz at poradowski.com
21+
*/
22+
public interface TestInterface {
23+
String getName();
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2015 tporadowski
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb;
18+
19+
/**
20+
* @author tporadowski - tomasz at poradowski.com
21+
*/
22+
public class TestInterfaceImpl implements TestInterface {
23+
private String name;
24+
25+
public TestInterfaceImpl(String name) {
26+
this.name = name;
27+
}
28+
29+
@Override
30+
public String getName() {
31+
return name;
32+
}
33+
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2015 tporadowski
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb;
18+
19+
import java.lang.reflect.Type;
20+
21+
import com.google.gson.InstanceCreator;
22+
23+
/**
24+
* @author tporadowski - tomasz at poradowski.com
25+
*/
26+
public class TestInterfaceInstanceCreator implements
27+
InstanceCreator<TestInterface> {
28+
29+
private int counter;
30+
31+
@Override
32+
public TestInterface createInstance(Type type) {
33+
return new TestInterfaceImpl("name " + counter++);
34+
}
35+
36+
/**
37+
* @return the counter
38+
*/
39+
public int getCounter() {
40+
return counter;
41+
}
42+
}

0 commit comments

Comments
 (0)