gvsig-tools / org.gvsig.tools / library / tags / org.gvsig.tools-3.0.13 / org.gvsig.tools.lib / src / test / java / org / gvsig / tools / persistence / DynObjectFactory3Test.java
History | View | Annotate | Download (5.59 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 330 | fdiaz | package org.gvsig.tools.persistence; |
25 | |||
26 | import java.io.InputStream; |
||
27 | import java.util.ArrayList; |
||
28 | import java.util.HashMap; |
||
29 | import java.util.HashSet; |
||
30 | import java.util.List; |
||
31 | import java.util.Map; |
||
32 | import java.util.Set; |
||
33 | |||
34 | import org.gvsig.tools.ToolsLocator; |
||
35 | import org.gvsig.tools.dynobject.DynClass; |
||
36 | import org.gvsig.tools.dynobject.DynObject; |
||
37 | import org.gvsig.tools.dynobject.DynObjectManager; |
||
38 | import org.gvsig.tools.dynobject.DynStruct; |
||
39 | import org.gvsig.tools.exception.BaseException; |
||
40 | import org.gvsig.tools.persistence.case1.model.ModelManager; |
||
41 | import org.gvsig.tools.persistence.case1.model.Projection; |
||
42 | import org.gvsig.tools.persistence.case1.model.ViewPort; |
||
43 | import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered; |
||
44 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
45 | import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException; |
||
46 | import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions; |
||
47 | import org.slf4j.Logger; |
||
48 | import org.slf4j.LoggerFactory; |
||
49 | |||
50 | public class DynObjectFactory3Test extends AbstractBasicTest { |
||
51 | |||
52 | private static Logger logger = LoggerFactory |
||
53 | .getLogger(DynObjectFactory3Test.class); |
||
54 | |||
55 | 517 | cordinyana | // private static int counter = 0;
|
56 | 330 | fdiaz | |
57 | protected static final String OBJ1_NAME = "MyObjectDynTypes"; |
||
58 | protected DynObjectManager dynManager;
|
||
59 | ModelManager modelmgr; |
||
60 | |||
61 | public void doSetUp() throws Exception { |
||
62 | this.manager = ToolsLocator.getPersistenceManager();
|
||
63 | manager.setAutoValidation(PersistenceManager.MANDATORY); |
||
64 | this.dynManager = ToolsLocator.getDynObjectManager();
|
||
65 | modelmgr = new ModelManager();
|
||
66 | modelmgr.registerPersistence(); |
||
67 | |||
68 | } |
||
69 | |||
70 | protected void tearDown() throws Exception { |
||
71 | super.tearDown();
|
||
72 | this.manager.unregisterClass(ViewPort.class);
|
||
73 | modelmgr.unregisterPersistence(); |
||
74 | |||
75 | } |
||
76 | |||
77 | protected void register() throws Exception { |
||
78 | if(this.manager.getDynObjectDefinition(OBJ1_NAME)==null){ |
||
79 | InputStream resource = this |
||
80 | .getClass() |
||
81 | .getClassLoader() |
||
82 | .getResourceAsStream( |
||
83 | "org/gvsig/tools/persistence/DynTypesTest.xml");
|
||
84 | |||
85 | Map definitions = this.dynManager.importDynClassDefinitions(resource, null); |
||
86 | DynClass dynStruct = (DynClass)definitions.get(OBJ1_NAME); |
||
87 | this.manager.addDefinition(dynStruct, null, null); |
||
88 | |||
89 | // this.manager.addDefinition(DynObject.class, OBJ1_NAME, resource, null, null, null);
|
||
90 | } |
||
91 | } |
||
92 | |||
93 | private DynObject createDynObject1(){
|
||
94 | DynObject obj = this.dynManager.createDynObject(OBJ1_NAME, PersistenceManager.PERSISTENCE_DYNOBJECT_NAMESPACE);
|
||
95 | |||
96 | Map m = new HashMap(); |
||
97 | m.put("a", "A1"); |
||
98 | m.put("b", "B2"); |
||
99 | obj.setDynValue("mapa", m);
|
||
100 | |||
101 | Set s = new HashSet(); |
||
102 | s.add("elementoConjunto1");
|
||
103 | s.add("elementoConjunto2");
|
||
104 | obj.setDynValue("conjunto", s);
|
||
105 | |||
106 | List l = new ArrayList(); |
||
107 | l.add("elementoLista1");
|
||
108 | l.add("elementoLista2");
|
||
109 | obj.setDynValue("lista", l);
|
||
110 | |||
111 | ViewPort vp = new ViewPort(new Projection()); |
||
112 | obj.setDynValue("viewPort", vp);
|
||
113 | |||
114 | 517 | cordinyana | // counter++;
|
115 | 330 | fdiaz | |
116 | return obj;
|
||
117 | } |
||
118 | |||
119 | void check(DynObject obj1, DynObject obj2) {
|
||
120 | assertEquals("map value mistmach", obj1.getDynValue("mapa"), obj2.getDynValue("mapa")); |
||
121 | assertEquals("set value mistmach", obj1.getDynValue("conjunto"), obj2.getDynValue("conjunto")); |
||
122 | assertEquals("list value mistmach", obj1.getDynValue("lista"), obj2.getDynValue("lista")); |
||
123 | assertEquals("object value mistmach", ((ViewPort)obj1.getDynValue("viewPort")).getCode(), ((ViewPort)obj2.getDynValue("viewPort")).getCode()); |
||
124 | } |
||
125 | |||
126 | public void testRegisterFactory() throws Exception { |
||
127 | register(); |
||
128 | } |
||
129 | |||
130 | public void testGetState() throws Exception { |
||
131 | try {
|
||
132 | register(); |
||
133 | DynObject obj = createDynObject1(); |
||
134 | PersistentState state = manager.getState(obj); |
||
135 | assertNotNull("Can't retrieve state", state);
|
||
136 | DynStruct definition = state.getDefinition(); |
||
137 | assertNotNull("Can't register class", definition);
|
||
138 | assertEquals("Registration don't work, name incorrect", OBJ1_NAME,
|
||
139 | definition.getName()); |
||
140 | } catch (BaseException ex) {
|
||
141 | logger.warn(ex.getMessageStack(), ex); |
||
142 | throw ex;
|
||
143 | } |
||
144 | } |
||
145 | |||
146 | public void testSetState() throws PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions { |
||
147 | DynObject obj1 = createDynObject1(); |
||
148 | PersistentState state = manager.getState(obj1); |
||
149 | assertNotNull("Can't retrieve state", state);
|
||
150 | |||
151 | DynObject obj2 = (DynObject) manager.create(state); |
||
152 | check(obj1, obj2); |
||
153 | } |
||
154 | |||
155 | public void testSaveState() throws Exception { |
||
156 | DynObject obj = createDynObject1(); |
||
157 | saveState(obj); |
||
158 | } |
||
159 | |||
160 | public void testLoadState() throws Exception { |
||
161 | DynObject obj1 = createDynObject1(); |
||
162 | PersistentState state = this.saveAndRestoreState(obj1);
|
||
163 | DynObject obj2 = (DynObject) manager.create(state); |
||
164 | |||
165 | check(obj1, obj2); |
||
166 | } |
||
167 | |||
168 | protected String getTestId() { |
||
169 | return "dopf3"; |
||
170 | |||
171 | } |
||
172 | |||
173 | |||
174 | } |