1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.graph;
21
22 import org.grouplens.grapht.BindingFunctionBuilder;
23 import org.grouplens.grapht.BindingFunctionBuilder.RuleSet;
24 import org.grouplens.grapht.Dependency;
25 import org.grouplens.grapht.annotation.AnnotationBuilder;
26 import org.grouplens.grapht.CachePolicy;
27 import org.grouplens.grapht.Component;
28 import org.grouplens.grapht.reflect.Desires;
29 import org.grouplens.grapht.reflect.Satisfactions;
30 import org.grouplens.grapht.reflect.internal.InstanceSatisfaction;
31 import org.grouplens.grapht.reflect.internal.types.NamedType;
32 import org.grouplens.grapht.solver.DefaultDesireBindingFunction;
33 import org.grouplens.grapht.solver.DependencySolver;
34 import org.grouplens.grapht.solver.DesireChain;
35 import org.junit.After;
36 import org.junit.Assert;
37 import org.junit.Test;
38
39 import javax.inject.Named;
40 import java.io.*;
41
42 import static org.hamcrest.Matchers.contains;
43 import static org.hamcrest.Matchers.hasSize;
44 import static org.junit.Assert.assertThat;
45
46 public class SerializationTest {
47 private static File GRAPH_FILE = new File("graph.dump");
48
49 @Test
50 public void testEmptyGraph() throws Exception {
51 DAGNode<Component, DesireChain> g =
52 DAGNode.singleton(DependencySolver.ROOT_SATISFACTION);
53 write(g);
54 DAGNode<Component, DesireChain> read = read();
55
56 assertThat(read.getReachableNodes(),
57 contains(read));
58 }
59
60 @Test
61 public void testSharedNodesGraph() throws Exception {
62 Component s1 = Component.create(Satisfactions.type(Object.class), CachePolicy.NEW_INSTANCE);
63 Component s2 = Component.create(Satisfactions.type(Object.class), CachePolicy.MEMOIZE);
64
65 DAGNode<Component, String> n2 = DAGNode.singleton(s2);
66 DAGNodeBuilder<Component, String> bld = DAGNode.newBuilder(s1);
67 bld.addEdge(n2, "wombat");
68 bld.addEdge(n2, "foobar");
69 write(bld.build());
70 DAGNode<Object, Object> read = read();
71
72 Assert.assertEquals(2, read.getReachableNodes().size());
73 assertThat(read.getOutgoingEdges(),
74 hasSize(2));
75 }
76
77 @Test
78 public void testDependencySolverSerialization() throws Exception {
79 BindingFunctionBuilder b = new BindingFunctionBuilder();
80 b.getRootContext().bind(String.class).withQualifier(new AnnotationBuilder<Named>(Named.class).set("value", "unused").build()).to("shouldn't see this");
81 b.getRootContext().bind(String.class).withQualifier(new AnnotationBuilder<Named>(Named.class).set("value", "test1").build()).to("hello world");
82
83 DependencySolver solver = DependencySolver.newBuilder()
84 .addBindingFunction(b.build(RuleSet.EXPLICIT))
85 .addBindingFunction(b.build(RuleSet.INTERMEDIATE_TYPES))
86 .addBindingFunction(b.build(RuleSet.SUPER_TYPES))
87 .addBindingFunction(DefaultDesireBindingFunction.create())
88 .build();
89 solver.resolve(Desires.create(null, NamedType.class, false));
90
91 DAGNode<Component,Dependency> g = solver.getGraph();
92 write(g);
93 DAGNode<Component, Dependency> root = read();
94
95 Assert.assertEquals(1, root.getOutgoingEdges().size());
96 DAGEdge<Component, Dependency> rootEdge = root.getOutgoingEdges().iterator().next();
97 DAGNode<Component, Dependency> namedType = rootEdge.getTail();
98
99 Assert.assertEquals(NamedType.class, namedType.getLabel().getSatisfaction().getErasedType());
100 Assert.assertEquals(NamedType.class, rootEdge.getLabel().getInitialDesire().getDesiredType());
101 Assert.assertEquals(rootEdge.getLabel().getInitialDesire().getSatisfaction(), namedType.getLabel().getSatisfaction());
102 Assert.assertNull(rootEdge.getLabel().getInitialDesire().getInjectionPoint().getQualifier());
103 Assert.assertTrue(rootEdge.getLabel().getInitialDesire().getInjectionPoint().getAttributes().isEmpty());
104
105 Assert.assertEquals(1, namedType.getOutgoingEdges().size());
106 DAGEdge<Component, Dependency> nameEdge = namedType.getOutgoingEdges().iterator().next();
107 DAGNode<Component, Dependency> string = nameEdge.getTail();
108
109 Assert.assertEquals(String.class, string.getLabel().getSatisfaction().getErasedType());
110 Assert.assertEquals(String.class, nameEdge.getLabel().getInitialDesire().getDesiredType());
111 Assert.assertEquals(AnnotationBuilder.of(Named.class).setValue("test1").build(), nameEdge.getLabel().getInitialDesire().getInjectionPoint().getQualifier());
112 Assert.assertTrue(nameEdge.getLabel().getInitialDesire().getInjectionPoint().getAttributes().isEmpty());
113
114 Assert.assertTrue(string.getLabel().getSatisfaction() instanceof InstanceSatisfaction);
115 Assert.assertEquals("hello world", ((InstanceSatisfaction) string.getLabel().getSatisfaction()).getInstance());
116 }
117
118 @After
119 public void cleanup() throws Exception {
120 GRAPH_FILE.delete();
121 }
122
123 private <V, E> void write(DAGNode<V, E> g) throws IOException {
124 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(GRAPH_FILE));
125 out.writeObject(g);
126 out.flush();
127 out.close();
128 }
129
130 private <V,E> DAGNode<V,E> read() throws IOException, ClassNotFoundException {
131 ObjectInputStream in = new ObjectInputStream(new FileInputStream(GRAPH_FILE));
132 try {
133 DAGNode<V,E> g = (DAGNode<V,E>) in.readObject();
134 return g;
135 } finally {
136 in.close();
137 }
138 }
139 }