View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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"); // extra binding to make sure it's skipped
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 }