Revision 2005

View differences:

org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/dynobject/DynObjectPagingHelperTest.java
1
/**
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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2010 {}  {{Task}}
27
 */
28
package org.gvsig.tools.dynobject;
29

  
30
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
31

  
32
/**
33
 * Tests for the {@link DynObjectPagingHelper} interface.
34
 * 
35
 * @author 2010- C?sar Ordi?ana - gvSIG team
36
 */
37
public abstract class DynObjectPagingHelperTest extends
38
		AbstractLibraryAutoInitTestCase {
39

  
40
	private DynObjectPagingHelper helper;
41

  
42
	protected void doSetUp() throws Exception {
43
		helper = createDynObjectPagingHelper();
44
	}
45

  
46
	/**
47
	 * Test method for the max page size setting.
48
	 */
49
	public void testMaxPageSize() throws Exception {
50

  
51
		helper.setMaxPageSize(2);
52
		helper.setCurrentPage(0);
53

  
54
		long numPages = helper.getNumPages();
55
		if (numPages == 0) {
56
			fail("The provided helper can't have 0 pages, "
57
					+ "please provide one with some data for the test");
58
		}
59
		for (long i = numPages; i < numPages - 1; i++) {
60
			assertEquals("Number of elements of page " + i
61
					+ " is not the same as the maximum page size", numPages,
62
					helper.getCurrentPageDynObjects().length);
63
			helper.setCurrentPage(i + 1);
64
		}
65
		assertTrue(
66
				"Last page is not less than or equal to the maximum page size",
67
				numPages >= helper.getCurrentPageDynObjects().length);
68
	}
69

  
70
	/**
71
	 * Test method for
72
	 * {@link org.gvsig.tools.dynobject.DynObjectPagingHelper#getNumPages()}.
73
	 */
74
	public void testGetNumPages() throws Exception {
75
		double numDOs = helper.getTotalSize();
76
		double pageSize = helper.getMaxPageSize();
77

  
78
		long numPages = (long) Math.ceil(numDOs / pageSize);
79
		assertEquals(numPages, helper.getNumPages());
80

  
81
		pageSize = 2;
82
		helper.setMaxPageSize((int) pageSize);
83

  
84
		numPages = (long) Math.ceil(numDOs / pageSize);
85
		assertEquals(numPages, helper.getNumPages());
86
	}
87

  
88
	/**
89
	 * Test method for
90
	 * {@link org.gvsig.tools.dynobject.DynObjectPagingHelper#setCurrentPage(long)}
91
	 * .
92
	 */
93
	public void testSetCurrentPage() throws Exception {
94
		try {
95
			helper.setCurrentPage(helper.getNumPages() + 1);
96
			fail("Helper allows to set a page which does not exist");
97
		} catch (Exception e) {
98
			// Works as excepted
99
		}
100

  
101
		try {
102
			helper.setCurrentPage(-1);
103
			fail("Helper allows to set a negative page");
104
		} catch (Exception e) {
105
			// Works as excepted
106
		}
107
	}
108

  
109
	/**
110
	 * Test method for
111
	 * {@link org.gvsig.tools.dynobject.DynObjectPagingHelper#getCurrentPageDynObjects()}
112
	 * .
113
	 */
114
	public void testGetCurrentPageDynObjects() throws Exception {
115
		helper.setMaxPageSize(2);
116
		long numPages = helper.getNumPages();
117

  
118
		if (numPages < 2) {
119
			fail("The provided helper can't have 0 or 1 page, "
120
					+ "please provide one with some more data for the test");
121
		}
122

  
123
		helper.setCurrentPage(0);
124
		DynObject[] values = helper.getCurrentPageDynObjects();
125
		assertTrue(values != null && values.length == 2);
126
	}
127

  
128
	protected DynObjectPagingHelper getHelper() {
129
		return helper;
130
	}
131

  
132
	protected abstract DynObjectPagingHelper createDynObjectPagingHelper()
133
			throws Exception;
134
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/dynobject/ImportDynClassTest.java
1
/**
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
package org.gvsig.tools.dynobject;
25

  
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.util.Map;
29

  
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dataTypes.DataTypes;
32
import org.gvsig.tools.evaluator.Evaluator;
33
import org.gvsig.tools.exception.BaseRuntimeException;
34
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
35
import org.gvsig.tools.visitor.Visitor;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38
import org.xmlpull.v1.XmlPullParserException;
39

  
40
public class ImportDynClassTest extends AbstractLibraryAutoInitTestCase {
41

  
42
	private final static Logger LOG = LoggerFactory
43
			.getLogger(ImportDynClassTest.class);
44
	private DynObjectManager manager;
45

  
46
	protected void doSetUp() throws Exception {
47
		this.manager = ToolsLocator.getDynObjectManager();
48
	}
49

  
50
	public void testTagBasedBasicTypes() throws InstantiationException,
51
			XmlPullParserException, IOException {
52
		try {
53
			InputStream stream = this.getClass().getResourceAsStream(
54
					"tagBasedImportDynClassTest.xml");
55

  
56
			Map dynClasses = this.manager.importDynClassDefinitions(stream,
57
					null);
58

  
59
			checkDynClassValues(dynClasses);
60
		} catch (BaseRuntimeException ex) {
61
			LOG.error(ex.getMessageStack());
62
			throw ex;
63
		}
64
	}
65

  
66
	public void testAttrBasedBasicTypes() throws InstantiationException,
67
			XmlPullParserException, IOException {
68
		try {
69
			InputStream stream = this.getClass().getResourceAsStream(
70
					"attrBasedImportDynClassTest.xml");
71

  
72
			Map dynClasses = this.manager.importDynClassDefinitions(stream,
73
					null);
74

  
75
			checkDynClassValues(dynClasses);
76
		} catch (BaseRuntimeException ex) {
77
			LOG.error(ex.getMessageStack());
78
			throw ex;
79
		}
80
	}
81

  
82
	private void checkDynClassValues(Map dynClasses) {
83
		DynField field;
84

  
85
		if (dynClasses.isEmpty()) {
86
			fail("No ha cargado ninguna clase.");
87
		}
88
		if (dynClasses.size() != 4) {
89
			fail("Ha cargado ms clases de las esperadas");
90
		}
91
		DynClass dynClass = (DynClass) dynClasses.get("MyObjectBasicTypes");
92
		assertNotNull("No se ha recuperado la clase 'MyObjectBasicTypes'",
93
				dynClass);
94
		assertEquals("La descripcion no es la esperada",
95
				"MyObjectBasicTypes test definition", dynClass.getDescription());
96

  
97
		field = dynClass.getDynField("a");
98
		assertNotNull("No se ha encontrado el campo 'a'", field);
99
		assertEquals("type para el campo 'a' no tiene el valor esperado.",
100
				DataTypes.INT, field.getType());
101
		assertEquals(
102
				"isMandatory para el campo 'a' no tiene el valor esperado.",
103
				true, field.isMandatory());
104
		assertEquals(
105
				"isPersistent para el campo 'a' no tiene el valor esperado.",
106
				true, field.isPersistent());
107
		assertEquals("minValue para el campo 'a' no tiene el valor esperado.",
108
				0, ((Integer) field.getMinValue()).intValue());
109
		assertEquals("maxValue para el campo 'a' no tiene el valor esperado.",
110
				100, ((Integer) field.getMaxValue()).intValue());
111
		assertEquals(
112
				"defaultValue para el campo 'a' no tiene el valor esperado.",
113
				1, ((Integer) field.getDefaultValue()).intValue());
114
		assertEquals("group para el campo 'a' no tiene el valor esperado.",
115
				"Numbers", field.getGroup());
116
		assertEquals("order para el campo 'a' no tiene el valor esperado.", 12,
117
				field.getOder());
118

  
119
		field = dynClass.getDynField("b");
120
		assertNotNull("No se ha encontrado el campo 'b'", field);
121
		assertEquals("type para el campo 'b' no tiene el valor esperado.",
122
				DataTypes.LONG, field.getType());
123
		assertEquals(
124
				"isMandatory para el campo 'b' no tiene el valor esperado.",
125
				true, field.isMandatory());
126
		assertEquals(
127
				"isPersistent para el campo 'b' no tiene el valor esperado.",
128
				true, field.isPersistent());
129
		assertEquals(
130
				"defaultValue para el campo 'b' no tiene el valor esperado.",
131
				2, ((Long) field.getDefaultValue()).longValue());
132
		assertEquals("group para el campo 'b' no tiene el valor esperado.",
133
				"Numbers", field.getGroup());
134

  
135
		field = dynClass.getDynField("c");
136
		assertNotNull("No se ha encontrado el campo 'c'", field);
137
		assertEquals("type para el campo 'c' no tiene el valor esperado.",
138
				DataTypes.DOUBLE, field.getType());
139
		assertEquals(
140
				"isMandatory para el campo 'c' no tiene el valor esperado.",
141
				false, field.isMandatory());
142
		assertEquals(
143
				"isPersistent para el campo 'c' no tiene el valor esperado.",
144
				true, field.isPersistent());
145
		assertEquals("minValue para el campo 'c' no tiene el valor esperado.",
146
				0.0, ((Double) field.getMinValue()).doubleValue(), 0.001);
147
		assertEquals("maxValue para el campo 'c' no tiene el valor esperado.",
148
				100.0, ((Double) field.getMaxValue()).doubleValue(), 0.001);
149
		assertEquals(
150
				"defaultValue para el campo 'c' no tiene el valor esperado.",
151
				1.1, ((Double) field.getDefaultValue()).doubleValue(), 0.001);
152
		assertEquals("group para el campo 'c' no tiene el valor esperado.",
153
				"Numbers", field.getGroup());
154

  
155
		field = dynClass.getDynField("d");
156
		assertNotNull("No se ha encontrado el campo 'd'", field);
157
		assertEquals("type para el campo 'd' no tiene el valor esperado.",
158
				DataTypes.FLOAT, field.getType());
159
		assertEquals(
160
				"isMandatory para el campo 'd' no tiene el valor esperado.",
161
				false, field.isMandatory());
162
		assertEquals(
163
				"isPersistent para el campo 'd' no tiene el valor esperado.",
164
				true, field.isPersistent());
165
		assertEquals(
166
				"defaultValue para el campo 'd' no tiene el valor esperado.",
167
				1.2, ((Float) field.getDefaultValue()).floatValue(), 0.001);
168
		assertEquals("group para el campo 'd' no tiene el valor esperado.",
169
				"Numbers", field.getGroup());
170

  
171
		field = dynClass.getDynField("e");
172
		assertNotNull("No se ha encontrado el campo 'e'", field);
173
		assertEquals("type para el campo 'e' no tiene el valor esperado.",
174
				DataTypes.BOOLEAN, field.getType());
175
		assertEquals(
176
				"isMandatory para el campo 'e' no tiene el valor esperado.",
177
				true, field.isMandatory());
178
		assertEquals(
179
				"isPersistent para el campo 'e' no tiene el valor esperado.",
180
				false, field.isPersistent());
181
		assertEquals(
182
				"defaultValue para el campo 'e' no tiene el valor esperado.",
183
				true, ((Boolean) field.getDefaultValue()).booleanValue());
184
		assertEquals(
185
				"availableValues[0].value para el campo 'e' no tiene el valor esperado.",
186
				true, ((Boolean) (field.getAvailableValues()[0].getValue()))
187
						.booleanValue());
188
		assertEquals(
189
				"availableValues[0].label para el campo 'e' no tiene el valor esperado.",
190
				"True", field.getAvailableValues()[0].getLabel());
191
		assertEquals(
192
				"availableValues[1].value para el campo 'e' no tiene el valor esperado.",
193
				false, ((Boolean) (field.getAvailableValues()[1].getValue()))
194
						.booleanValue());
195
		assertEquals(
196
				"availableValues[1].label para el campo 'e' no tiene el valor esperado.",
197
				"False", field.getAvailableValues()[1].getLabel());
198

  
199
		field = dynClass.getDynField("f");
200
		assertNotNull("No se ha encontrado el campo 'f'", field);
201
		assertEquals("type para el campo 'f' no tiene el valor esperado.",
202
				DataTypes.STRING, field.getType());
203
		assertEquals(
204
				"isMandatory para el campo 'f' no tiene el valor esperado.",
205
				true, field.isMandatory());
206
		assertEquals(
207
				"isPersistent para el campo 'f' no tiene el valor esperado.",
208
				true, field.isPersistent());
209
		assertEquals(
210
				"defaultValue para el campo 'f' no tiene el valor esperado.",
211
				"red", field.getDefaultValue());
212
		assertEquals(
213
				"availableValues[0].value para el campo 'f' no tiene el valor esperado.",
214
				"red", field.getAvailableValues()[0].getValue());
215
		assertEquals(
216
				"availableValues[0].label para el campo 'f' no tiene el valor esperado.",
217
				"Red", field.getAvailableValues()[0].getLabel());
218
		assertEquals(
219
				"availableValues[1].value para el campo 'f' no tiene el valor esperado.",
220
				"green", field.getAvailableValues()[1].getValue());
221
		assertEquals(
222
				"availableValues[1].label para el campo 'f' no tiene el valor esperado.",
223
				"Green", field.getAvailableValues()[1].getLabel());
224
		assertEquals(
225
				"availableValues[2].value para el campo 'f' no tiene el valor esperado.",
226
				"blue", field.getAvailableValues()[2].getValue());
227
		assertEquals(
228
				"availableValues[2].label para el campo 'f' no tiene el valor esperado.",
229
				"Blue", field.getAvailableValues()[2].getLabel());
230

  
231
		field = dynClass.getDynField("g");
232
		assertNotNull("No se ha encontrado el campo 'g'", field);
233
		assertEquals("type para el campo 'g' no tiene el valor esperado.",
234
				DataTypes.DATE, field.getType());
235
		assertEquals(
236
				"isMandatory para el campo 'g' no tiene el valor esperado.",
237
				true, field.isMandatory());
238
		assertEquals(
239
				"isPersistent para el campo 'g' no tiene el valor esperado.",
240
				true, field.isPersistent());
241

  
242
		dynClass = (DynClass) dynClasses.get("MyObjectBasicTypes4");
243
		assertNotNull("No se ha recuperado la clase 'MyObjectBasicTypes4'",
244
				dynClass);
245
	
246
		field = dynClass.getDynField("obj");
247
		assertNotNull("No se ha encontrado el campo 'obj'", field);
248
		assertEquals("type para el campo 'obj' no tiene el valor esperado.",
249
				DataTypes.OBJECT, field.getType());
250
		assertTrue("El valor de classOfValue del campo obj no es el esperado.", Evaluator.class.isAssignableFrom(field.getClassOfValue()) ) ;
251
		
252
		field = dynClass.getDynField("objs");
253
		assertNotNull("No se ha encontrado el campo 'objs'", field);
254
		assertEquals("type para el campo 'objs' no tiene el valor esperado.",
255
				DataTypes.LIST, field.getType());
256
		assertTrue("El valor de classOfItems del campo objs no es el esperado.", Visitor.class.isAssignableFrom(field.getClassOfValue()) ) ;
257
	}
258

  
259
}
0 260

  
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/dynobject/DynObjectSetTest.java
1
/**
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
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

  
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2010 {}  {{Task}}
49
*/
50
package org.gvsig.tools.dynobject;
51

  
52
import org.gvsig.tools.dispose.DisposableIterator;
53
import org.gvsig.tools.exception.BaseException;
54
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
55
import org.gvsig.tools.visitor.VisitCanceledException;
56
import org.gvsig.tools.visitor.Visitor;
57

  
58
/**
59
 * Tests for the {@link DynObjectSet} interface.
60
 * 
61
 * @author 2010- C?sar Ordi?ana - gvSIG team
62
 */
63
public abstract class DynObjectSetTest extends AbstractLibraryAutoInitTestCase {
64

  
65
	protected void doSetUp() throws Exception {
66
		// Nothing to do
67
	}
68

  
69
	public void testIterator() throws Exception {
70
		DynObjectSet dynObjectSet = createDynObjectSet();
71
		testIterator(dynObjectSet);
72
		dynObjectSet.dispose();
73
	}
74

  
75
	private void testIterator(DynObjectSet dynObjectSet) throws Exception {
76
		try {
77
			DisposableIterator iter = dynObjectSet.iterator(-5);
78
			iter.dispose();
79
			fail("Iterator index accepted with a value < 0");
80
		} catch (Exception ex) {
81
			// Good
82
		}
83

  
84
		try {
85
			DisposableIterator iter = dynObjectSet.iterator(dynObjectSet
86
					.getSize() + 2);
87
			iter.dispose();
88
			fail("Iterator index accepted with a value > collection size");
89
		} catch (Exception ex) {
90
			// Good
91
		}
92

  
93
		long index = dynObjectSet.getSize() - 3l;
94
		DisposableIterator iter = dynObjectSet.iterator(index);
95
		int iterations = 0;
96
		while (iter.hasNext()) {
97
			iter.next();
98
			iterations++;
99
		}
100
		assertEquals("The number of iterations remaining is not correct", 3,
101
				iterations);
102
		iter.dispose();
103

  
104
		iter = dynObjectSet.iterator(0);
105
		iterations = 0;
106
		while (iter.hasNext()) {
107
			iter.next();
108
			iterations++;
109
		}
110
		assertEquals("The number of iterations is not the "
111
				+ "total size of the collection", dynObjectSet.getSize(),
112
				iterations);
113
		iter.dispose();
114
	}
115

  
116
	public void testVisitable() throws Exception {
117
		DynObjectSet dynObjectSet = createDynObjectSet();
118
		testVisitable(dynObjectSet);
119
		dynObjectSet.dispose();
120
	}
121

  
122
	private void testVisitable(DynObjectSet dynObjectSet) throws Exception {
123
		long size = dynObjectSet.getSize();
124

  
125
		TestCountVisitor visitor = new TestCountVisitor();
126
		dynObjectSet.accept(visitor);
127
		
128
		assertEquals("The number of iterations performed is not correct", size,
129
				visitor.getCount());
130

  
131
		visitor = new TestCountVisitor();
132
		dynObjectSet.accept(visitor, 2);
133
		
134
		assertEquals("The number of iterations performed is not correct",
135
				size - 3, visitor.getCount());
136
	}
137

  
138
	public void testSize() throws Exception {
139
		DynObjectSet dynObjectSet = createDynObjectSet();
140
		testSize(dynObjectSet);
141
		dynObjectSet.dispose();
142
	}
143

  
144
	private void testSize(DynObjectSet dynObjectSet) throws Exception {
145
		long size = dynObjectSet.getSize();
146
		
147
		assertTrue("Set size cannot be less than zero", (size >= 0l));
148

  
149
		if (size == 0) {
150
			assertTrue(
151
					"An empty set does not return true in the isEmpty method",
152
					dynObjectSet.isEmpty());
153
		} else {
154
			assertFalse(
155
					"An non empty set does return true in the isEmpty method",
156
					dynObjectSet.isEmpty());
157
		}
158
	}
159

  
160
	private class TestCountVisitor implements Visitor {
161
		private long count = 0;
162

  
163
		public void visit(Object obj) throws VisitCanceledException,
164
				BaseException {
165
			count++;
166
		}
167
		
168
		/**
169
		 * @return the count
170
		 */
171
		public long getCount() {
172
			return count;
173
		}
174
	}
175

  
176
	/**
177
	 * Creates a {@link DynObjectSet} with at least 4 elements.
178
	 * 
179
	 * @return the {@link DynObjectSet}
180
	 * @throws Exception
181
	 *             if there is an error creating the {@link DynObjectSet}
182
	 */
183
	protected abstract DynObjectSet createDynObjectSet() throws Exception;
184
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/dynobject/impl/DefaultDynFieldTest.java
1
/**
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
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

  
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2008 {DiSiD Technologies}  {{Task}}
49
*/
50
package org.gvsig.tools.dynobject.impl;
51

  
52
import junit.framework.TestCase;
53
import org.gvsig.tools.dataTypes.DataTypes;
54

  
55
/**
56
 * Unit tests for the class {@link DefaultDynField}.
57
 * 
58
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
59
 */
60
public class DefaultDynFieldTest extends TestCase {
61

  
62
    protected void setUp() throws Exception {
63
        super.setUp();
64
    }
65
    
66
    public void testEquals() {
67
        DefaultDynField field1 = new DefaultDynField("field1", DataTypes.STRING);
68
        DefaultDynField field2 = new DefaultDynField("field2", DataTypes.STRING);
69
        DefaultDynField field1b = new DefaultDynField("field1", DataTypes.STRING);
70

  
71
        assertTrue(field1.equals(field1b));
72
        assertFalse(field1.equals(field2));
73
        assertFalse(field1.equals(null));
74
    }
75

  
76
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/dynobject/impl/DefaultDynClassTest.java
1
/**
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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.tools.dynobject.impl;
29

  
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.List;
33

  
34
import org.easymock.MockControl;
35

  
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dataTypes.DataTypes;
38
import org.gvsig.tools.dynobject.DynClass;
39
import org.gvsig.tools.dynobject.DynField;
40
import org.gvsig.tools.dynobject.DynField_v2;
41
import org.gvsig.tools.dynobject.DynObject;
42
import org.gvsig.tools.dynobject.DynObjectManager;
43
import org.gvsig.tools.dynobject.DynObjectValueItem;
44
import org.gvsig.tools.dynobject.DynStruct;
45
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
46

  
47
/**
48
 * Unit tests for the class {@link DefaultDynClass}.
49
 * 
50
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
51
 */
52
public class DefaultDynClassTest extends AbstractLibraryAutoInitTestCase {
53

  
54
	private MockControl managerControl;
55
	private DynObjectManager manager;
56
	private DefaultDynClass dynClass;
57

  
58
	protected void doSetUp() throws Exception {
59
		managerControl = MockControl.createNiceControl(DynObjectManager.class);
60
		manager = (DynObjectManager) managerControl.getMock();
61
		dynClass = new DefaultDynClass(manager, new DefaultDynClassName(
62
				"dynclass"), "description");
63
	}
64

  
65
	/**
66
	 * Test method for
67
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#newInstance()}.
68
	 */
69
	// public void testNewInstance() {
70
	// TODO: solve
71
	// MockControl dynObjControl = MockControl
72
	// .createNiceControl(DynObject.class);
73
	// DynObject dynObj = (DynObject) dynObjControl.getMock();
74
	// dynObjControl.expectAndReturn(dynObj.getDynClass(), dynClass);
75
	// dynObjControl.replay();
76
	//
77
	// managerControl.expectAndReturn(manager.createDynObject(dynClass),
78
	// dynObj);
79
	// managerControl.replay();
80
	//
81
	// DynObject newDynObj = dynClass.newInstance();
82
	// assertTrue(dynClass.isInstance(newDynObj));
83
	// }
84

  
85
	/**
86
	 * Test method for
87
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#extend(org.gvsig.tools.dynobject.DynClass)}
88
	 * .
89
	 */
90
	public void testExtendDynClass() {
91
		final String parent = "parent";
92

  
93
		DefaultDynClassName name = new DefaultDynClassName(parent);
94

  
95
		DefaultDynClass parentDynClass = new DefaultDynClass(manager, name,
96
				"parent description");
97

  
98
		managerControl.expectAndReturn(manager.has(parent), true);
99
		managerControl.expectAndReturn(manager.get(parent), parentDynClass);
100
		managerControl.replay();
101

  
102
		dynClass.extend(parentDynClass);
103

  
104
		DynClass[] parents = dynClass.getSuperDynClasses();
105
		assertEquals(parentDynClass, parents[0]);
106
	}
107

  
108
	/**
109
	 * Test method for
110
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#extend(java.lang.String)}
111
	 * .
112
	 */
113
	public void testExtendString() {
114
		final String parent = "parent";
115

  
116
		DefaultDynClassName name = new DefaultDynClassName(parent);
117

  
118
		DefaultDynClass parentDynClass = new DefaultDynClass(manager, name,
119
				"parent description");
120

  
121
		managerControl.expectAndReturn(manager.has(parent), true);
122
		managerControl.expectAndReturn(manager.get(parent), parentDynClass);
123
		managerControl.replay();
124

  
125
		dynClass.extend(parent);
126

  
127
		DynClass[] parents = dynClass.getSuperDynClasses();
128
		assertEquals(parentDynClass, parents[0]);
129
	}
130

  
131
	/**
132
	 * Test method for
133
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#getDynField(java.lang.String)}
134
	 * ,
135
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#addDynField(java.lang.String)}
136
	 * ,
137
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#removeDynField(java.lang.String)}
138
	 * ,
139
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#getDeclaredDynFields()}
140
	 * .
141
	 */
142
	public void testDynFields() {
143
		final String field1 = "field1";
144
		final String field2 = "field2";
145

  
146
		DynField dynField1 = dynClass.addDynField(field1);
147
		assertEquals(dynField1, dynClass.getDynField(field1));
148

  
149
		DynField dynField2 = dynClass.addDynField(field2);
150
		assertEquals(dynField2, dynClass.getDynField(field2));
151

  
152
		DynField[] fields = dynClass.getDeclaredDynFields();
153
		assertEquals(2, fields.length);
154

  
155
		dynClass.removeDynField(field1);
156
		assertNull(dynClass.getDynField(field1));
157
	}
158

  
159
	/**
160
	 * Test method for
161
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynClass#equals(Object)}.
162
	 */
163
	public void testEquals() {
164
		DefaultDynClass dynClass2 = new DefaultDynClass(manager,
165
				new DefaultDynClassName("dynclass2"), "description2");
166
		DefaultDynClass dynClassB = new DefaultDynClass(manager,
167
				new DefaultDynClassName("dynclass"), "description");
168

  
169
		assertTrue(dynClass.equals(dynClassB));
170
		assertFalse(dynClass.equals(dynClass2));
171
		assertFalse(dynClass.equals(null));
172
	}
173

  
174
	/**
175
	 * Validation test: DynObjects as dynfields within a dynObject.
176
	 * 
177
	 * Metadata A will have metadata B as porperty. for each dynField, a
178
	 * validate function will be triggered with the current
179
	 * 
180
	 * @throws Exception
181
	 * 
182
	 */
183
	public void testValidate() throws Exception {
184

  
185
		/**
186
		 * Creating and registering both dynClasses
187
		 */
188
		DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
189

  
190
		DynClass dynClassA = new DefaultDynClass(manager,
191
				new DefaultDynClassName("metadata", "DynclassA"),
192
				"descriptionA");
193
		dynManager.add(dynClassA);
194

  
195
		DynClass dynClassB = new DefaultDynClass(manager,
196
				new DefaultDynClassName("metadata", "DynclassB"),
197
				"descriptionB");
198
		dynManager.add(dynClassB);
199

  
200
		/**
201
		 * Filling dynClass B with "Name" and "Street"
202
		 */
203
		dynClassB.addDynFieldString("name");
204
		dynClassB.addDynFieldString("street");
205

  
206
		/**
207
		 * Filling dynClass A with "Name" and the dynClass B named "dynObjB"
208
		 */
209
		dynClassA.addDynFieldString("name");
210
		dynClassA.addDynField("dynObjB").setType(DataTypes.DYNOBJECT)
211
				.setSubtype(dynClassB.getFullName());
212

  
213
		/**
214
		 * Creating dynObjects for current dynClasses and applying dynObject B
215
		 * as a property of A
216
		 */
217
		DynObject dynObjA = new DefaultDynObject(dynClassA);
218
		DynObject dynObjB = new DefaultDynObject(dynClassB);
219

  
220
		// TODO: Note that even specified a subType, if we set a different
221
		// dynObject, it will not check if the subType is correct.
222
		dynObjA.setDynValue("dynObjB", dynObjB);
223

  
224
		/**
225
		 * Once the structure has been created, we will validate each field of
226
		 * dynObject A, which also contains B as a property.
227
		 */
228
		DynField[] fields = dynObjA.getDynClass().getDynFields();
229
		DynField field;
230
		Object value;
231
		for (int i = 0; i < fields.length; ++i) {
232
			field = fields[i];
233
			value = dynObjA.getDynValue(field.getName());
234

  
235
			// Checking that it actually contains the DynObject B as a
236
			// property
237
			if (field.getType() == DataTypes.DYNOBJECT) {
238
				assertTrue(dynClassB.equals(((DynObject) value).getDynClass()));
239
			}
240

  
241
			// Performing validation
242
			field.validate(value);
243
		}
244
	}
245

  
246
	/**
247
	 * 
248
	 * Duplicate test: checking if, given two dynClasses A and B, dynFields from
249
	 * class A can be duplicated into dynClass B by using setters in B with the
250
	 * returned values of getters of A as input.
251
	 * 
252
	 */
253
	public void testSetters() {
254
		/**
255
		 * Creating and registering both dynClasses
256
		 */
257
		DynObjectManager dynManager = ToolsLocator.getDynObjectManager();
258

  
259
		DynClass dynClassA = new DefaultDynClass(manager,
260
				new DefaultDynClassName("metadata", "DynclassA"),
261
				"descriptionA");
262

  
263
		DynClass dynClassB = new DefaultDynClass(manager,
264
				new DefaultDynClassName("metadata", "DynclassB"),
265
				"descriptionB");
266

  
267
		/**
268
		 * Filling dynClass B with "Name" and "Street"
269
		 */
270
		dynClassB.addDynFieldString("name");
271
		dynClassB.addDynFieldString("street");
272

  
273
		fillInitialDynfields(dynClassA, dynClassB.getFullName());
274

  
275
		// /**
276
		// * Creating dynObjects for current dynClasses and applying dynObject B
277
		// * as a property of A
278
		// */
279
		// DynObject dynObjA = new DefaultDynObject(dynClassA);
280
		// DynObject dynObjB = new DefaultDynObject(dynClassB);
281

  
282
		copyDefinitions(dynClassA, dynClassB);
283

  
284
		checkValues(dynClassA, dynClassB);
285
	}
286

  
287
	private List createList(Object[] values) {
288
		DynObjectValueItem[] valueItems = new DynObjectValueItem[values.length];
289
		for (int i = 0; i < values.length; i++) {
290
			valueItems[i] = new DynObjectValueItem(values[i]);
291
		}
292
		return Arrays.asList(valueItems);
293
	}
294

  
295
	/**
296
	 * Filling dynClass A with different parameters that will be duplicated into
297
	 * dynClass B afterwards.
298
	 */
299
	private void fillInitialDynfields(DynClass definition,
300
			String dynClassBFullName) {
301

  
302
		List items = new ArrayList();
303

  
304
		definition.addDynFieldString("name");
305
		definition.addDynField("dynObjB").setType(DataTypes.DYNOBJECT)
306
				.setSubtype(dynClassBFullName);
307

  
308
		definition.addDynFieldString("id").setReadOnly(true); // optional
309

  
310
		definition.addDynFieldString("language").setMandatory(true);
311

  
312
		definition.addDynFieldString("charachterset").setMandatory(true)
313
				.setAvailableValues(createList(new String[] { "a", "b", "c" }));
314

  
315
		definition
316
				.addDynFieldList("hierarchyLevels")
317
				.setElementsType(DataTypes.STRING)
318
				.setMandatory(true)
319
				.setAvailableValues(
320
						createList(new String[] { "hier1", "hier2", "hier3" }));
321

  
322
		definition.addDynFieldDate("date").setMandatory(true);
323

  
324
		definition.addDynFieldList("keywords")
325
				.setElementsType(DataTypes.STRING);
326

  
327
//		definition.addDynFieldList("dynObjects")
328
//				.setElementsType(DataTypes.DYNOBJECT).setMandatory(true)
329
//				.setSubtype(dynClassBFullName);
330

  
331
	}
332

  
333
	private void copyDefinitions(DynClass dynClassA, DynClass dynClassB) {
334
		DynField field;
335
		DynField fields[] = dynClassA.getDynFields();
336
		for (int i = 0; i < fields.length; i++) {
337
			field = fields[i];
338
			;
339
//			if (dynClassB.getDynField(field.getName()) == null) {
340
//				addDynField(dynClassB, field);
341
//			}
342
		}
343

  
344
		// dynFields must have the same number of items or more if some items
345
		// have been previously defined.
346
		assertTrue(dynClassB.getDynFields().length >= fields.length);
347
	}
348

  
349
	private void copyValues(DynObject oldDefinition, DynObject newDefinition) {
350
		Object value;
351
		DynField field;
352
		DynField fields[] = oldDefinition.getDynClass().getDynFields();
353
		for (int i = 0; i < fields.length; i++) {
354
			field = fields[i];
355
			value = oldDefinition.getDynValue(field.getName());
356
//			if (newDefinition.getDynClass().getDynField(field.getName()) == null) {
357
//				addDynField(newDefinition.getDynClass(), field);
358
//			}
359
			if (value == null) {
360
				continue;
361
			}
362
			if (value instanceof DynObject) {
363
				DynObject parent = (DynObject) value;
364
				DynStruct parentDefinition = parent.getDynClass();
365
				DynClass childDefinition = manager.get(
366
						parentDefinition.getName(),
367
						parentDefinition.getNamespace());
368
				DynObject child = manager.createDynObject(childDefinition);
369

  
370
				copyValues(child, parent);
371
				newDefinition.setDynValue(field.getName(), child);
372

  
373
			} else {
374
				newDefinition.setDynValue(field.getName(), value);
375
			}
376
		}
377
	}
378

  
379
	private void addDynField(DynStruct definition, DynField_v2 field) {
380
		DynField dynField = definition.addDynField(field.getName())
381
				.setType(field.getType()).setSubtype(field.getSubtype())
382
				.setAvailableValues(field.getAvailableValues())
383
				.setClassOfItems(field.getClassOfItems())
384
				.setClassOfValue(field.getClassOfValue())
385
				.setDefaultFieldValue(field.getDefaultValue())
386
				.setDescription(field.getDescription())
387
				.setGroup(field.getGroup()).setHidden(field.isHidden())
388
				.setMandatory(field.isMandatory())
389
				.setMaxValue(field.getMaxValue())
390
				.setMinValue(field.getMinValue()).setOrder(field.getOder())
391
				.setPersistent(field.isPersistent())
392
				.setReadOnly(field.isReadOnly());
393

  
394
		// PETO copyElementsType(dynField, XXXX);
395
	}
396

  
397
	private void copyElementsType(DynField dynField, DynField elementsType) {
398
		if (elementsType == null) {
399
			return;
400
		}
401
//
402
//		dynField.setElementsType(elementsType.getType());
403
//		dynField.getElementsType().setSubtype(elementsType.getSubtype())
404
//				.setAvailableValues(elementsType.getAvailableValues())
405
//				.setClassOfItems(elementsType.getClassOfItems())
406
//				.setClassOfValue(elementsType.getClassOfValue())
407
//				.setDefaultFieldValue(elementsType.getDefaultValue())
408
//				.setDescription(elementsType.getDescription())
409
//				.setGroup(elementsType.getGroup())
410
//				.setHidden(elementsType.isHidden())
411
//				.setMandatory(elementsType.isMandatory())
412
//				.setMaxValue(elementsType.getMaxValue())
413
//				.setMinValue(elementsType.getMinValue())
414
//				.setOrder(elementsType.getOder())
415
//				.setPersistent(elementsType.isPersistent())
416
//				.setReadOnly(elementsType.isReadOnly());
417
	}
418

  
419
	private void checkValues(DynClass dynObjA, DynClass dynObjB) {
420
		DynField[] dynFieldsA = dynObjA.getDynFields();
421
		DynField[] dynFieldsB = dynObjB.getDynFields();
422

  
423
		DynField field;
424
		boolean done = false;
425
		for (int i = 0; i < dynFieldsA.length; ++i) {
426
			field = dynFieldsA[i];
427
			for (int j = 0; j < dynFieldsB.length; ++j) {
428
				if (dynFieldsB[j].getName().equals(field.getName())) {
429
					checkDynField(field, dynFieldsB[j]);
430
					done = true;
431
				}
432
			}
433
			// dynField must be defined in both lists.
434
			assertTrue(done);
435

  
436
		}
437
	}
438

  
439
	private void checkDynField(DynField fieldA, DynField fieldB) {
440

  
441
		assertEquals(fieldA.getName(), fieldA.getName());
442
		assertEquals(fieldA.getType(), fieldB.getType());
443
		assertEquals(fieldA.getSubtype(), fieldB.getSubtype());
444
		assertEquals(fieldA.getAvailableValues(), fieldB.getAvailableValues());
445
		assertEquals(fieldA.getClassOfItems(), fieldB.getClassOfItems());
446
		assertEquals(fieldA.getClassOfValue(), fieldB.getClassOfValue());
447
		assertEquals(fieldA.getDefaultValue(), fieldB.getDefaultValue());
448
		assertEquals(fieldA.getDescription(), fieldB.getDescription());
449
		assertEquals(fieldA.getGroup(), fieldB.getGroup());
450
		assertEquals(fieldA.isHidden(), fieldB.isHidden());
451
		assertEquals(fieldA.isMandatory(), fieldB.isMandatory());
452
		assertEquals(fieldA.getMaxValue(), fieldB.getMaxValue());
453
		assertEquals(fieldA.getMinValue(), fieldB.getMinValue());
454
		assertEquals(fieldA.getOder(), fieldB.getOder());
455
		assertEquals(fieldA.isPersistent(), fieldB.isPersistent());
456
		assertEquals(fieldA.isReadOnly(), fieldB.isReadOnly());
457

  
458
	}
459

  
460
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/dynobject/impl/DefaultDynObjectTest.java
1
/**
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
/**
25
 *
26
 */
27
package org.gvsig.tools.dynobject.impl;
28

  
29
import org.easymock.MockControl;
30

  
31
import org.gvsig.tools.dynobject.DynObjectManager;
32
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
33

  
34
/**
35
 * @author jmvivo
36
 *
37
 */
38
public class DefaultDynObjectTest extends AbstractLibraryAutoInitTestCase {
39

  
40
    private MockControl managerControl;
41
	private DynObjectManager manager;
42
	private DefaultDynClass dynClass;
43

  
44
	/* (non-Javadoc)
45
	 * @see junit.framework.TestCase#setUp()
46
	 */
47
	protected void doSetUp() throws Exception {
48
		managerControl = MockControl.createNiceControl(DynObjectManager.class);
49
		manager = (DynObjectManager) managerControl.getMock();
50
		dynClass = new DefaultDynClass(manager, new DefaultDynClassName(
51
				"dynclass"), "description");
52
	}
53

  
54

  
55
    /**
56
     * Test method for {@link org.gvsig.tools.dynobject.impl.DefaultDynObject#hasDynValue(String)}.
57
     */
58
    public void testHasDynValueStringTest() {
59
    	final String fieldName = "myField";
60
		dynClass.addDynField(fieldName);
61

  
62
		DefaultDynObject obj = new DefaultDynObject(dynClass);
63

  
64
		assertFalse(obj.hasDynValue(fieldName));
65

  
66
		obj.setDynValue(fieldName, "Value");
67

  
68
		assertTrue(obj.hasDynValue(fieldName));
69

  
70
    }
71

  
72
	/**
73
	 * Test method for
74
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynObject#hasDynValue(String)}
75
	 * from extended class.
76
	 */
77
	public void testHasDynValueStringExtendedTest() {
78

  
79
        final String parent = "parent";
80

  
81
		DefaultDynClassName name = new DefaultDynClassName(parent);
82

  
83
		DefaultDynClass parentDynClass = new DefaultDynClass(manager, name,
84
				"parent description");
85

  
86
		managerControl.expectAndReturn(manager.has(parent), true);
87
		managerControl.expectAndReturn(manager.get(parent), parentDynClass);
88
		managerControl.replay();
89

  
90
		final String fieldName = "myField";
91
		parentDynClass.addDynField(fieldName);
92

  
93
		dynClass.extend(parent);
94

  
95
		DefaultDynObject obj = new DefaultDynObject(dynClass);
96

  
97
		assertFalse(obj.hasDynValue(fieldName));
98

  
99
		obj.setDynValue(fieldName, "Value");
100

  
101
		assertTrue(obj.hasDynValue(fieldName));
102

  
103
    }
104

  
105

  
106

  
107
	/**
108
	 * Test method for
109
	 * {@link org.gvsig.tools.dynobject.impl.DefaultDynObject#hasDynValue(String)}
110
	 * from extended class.
111
	 */
112
	public void testHasDynValueStringDelegatedTest() {
113

  
114
		final String delegated = "delegated";
115

  
116

  
117
		DefaultDynClassName name = new DefaultDynClassName(delegated);
118

  
119
		DefaultDynClass delegatedDynClass = new DefaultDynClass(manager, name,
120
				"parent description");
121

  
122
		managerControl.expectAndReturn(manager.has(delegated), true);
123
		managerControl.expectAndReturn(manager.get(delegated),
124
				delegatedDynClass);
125
		managerControl.replay();
126

  
127
		final String fieldName = "myField";
128
		delegatedDynClass.addDynField(fieldName);
129

  
130
		dynClass.addDynField(fieldName);
131

  
132
		DefaultDynObject delegatedObj = new DefaultDynObject(delegatedDynClass);
133

  
134
		DefaultDynObject obj = new DefaultDynObject(dynClass);
135

  
136
		assertFalse(obj.hasDynValue(fieldName));
137

  
138
		delegatedObj.setDynValue(fieldName, "DelegatedValue");
139

  
140
		obj.delegate(delegatedObj);
141

  
142
		assertTrue(obj.hasDynValue(fieldName));
143

  
144
		Object v1 = delegatedObj.getDynValue(fieldName); 
145
		Object v2 = obj.getDynValue(fieldName);
146
		assertEquals(v1,v2);
147

  
148

  
149
		obj.setDynValue(fieldName, "Value");
150

  
151
		assertTrue(obj.hasDynValue(fieldName));
152
		assertEquals("Value", obj.getDynValue(fieldName));
153

  
154

  
155
	}
156
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/locator/AbstractLocatorTest.java
1
/**
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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {{Company}}   {{Task}}
27
 */
28
package org.gvsig.tools.locator;
29

  
30
import java.util.Arrays;
31
import java.util.Collection;
32
import java.util.Map;
33

  
34
import junit.framework.TestCase;
35

  
36
/**
37
 * Unit tests for the AbstractLocator class.
38
 * 
39
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
40
 */
41
public class AbstractLocatorTest extends TestCase {
42

  
43
    private TestLocator locator;
44

  
45
    protected void setUp() throws Exception {
46
        super.setUp();
47
        locator = new TestLocator();
48
    }
49

  
50
    protected void tearDown() throws Exception {
51
        super.tearDown();
52
    }
53

  
54
    /**
55
     * Test method for {@link org.gvsig.tools.locator.AbstractLocator#getNames()}.
56
     */
57
    public void testGetNames() {
58
        assertNull("Empty locator must not return any names", locator
59
                .getNames());
60

  
61
        String name1 = "test1";
62
        String name2 = "test2";
63

  
64
        locator.register(name1, String.class);
65
        locator.register(name2, String.class);
66

  
67
        String[] names = locator.getNames();
68

  
69
        assertEquals("Number of registered names incorrect, must be 2", 2,
70
                names.length);
71

  
72
        Collection namesColl = Arrays.asList(names);
73
        assertTrue("The list of names does not contain the registered name: "
74
                + name1, namesColl.contains(name1));
75
        assertTrue("The list of names does not contain the registered name: "
76
                + name2, namesColl.contains(name2));
77
    }
78

  
79
    /**
80
     * Test method for
81
     * {@link org.gvsig.tools.locator.AbstractLocator#get(java.lang.String)} and
82
     * {@link org.gvsig.tools.locator.AbstractLocator#register(java.lang.String, java.lang.Class)}
83
     */
84
    public void testGetAndRegisterClass() {
85
        Class clazz = String.class;
86
        String name = "test";
87

  
88
        locator.register(name, clazz);
89

  
90
        Object ref = locator.get(name);
91

  
92
        assertEquals(clazz, ref.getClass());
93
    }
94

  
95
    /**
96
     * Test method for
97
     * {@link org.gvsig.tools.locator.AbstractLocator#get(java.lang.String)} and
98
     * {@link org.gvsig.tools.locator.AbstractLocator#register(String, LocatorObjectFactory)
99

  
100
     */
101
    public void testGetAndRegisterFactory() {
102
        final Object ref = new Object();
103
        LocatorObjectFactory factory = new LocatorObjectFactory() {
104

  
105
            public Object create() {
106
                return ref;
107
            }
108

  
109
            public Object create(Object[] args) {
110
                return ref;
111
            }
112

  
113
            public Object create(Map args) {
114
                return ref;
115
            }
116
        };
117

  
118
        String name = "test";
119

  
120
        locator.register(name, factory);
121

  
122
        Object locatorRef = locator.get(name);
123

  
124
        assertEquals(ref, locatorRef);
125
    }
126

  
127
    public class TestLocator extends AbstractLocator {
128
        public String getLocatorName() {
129
            return "TestLocator";
130
        }
131
    }
132

  
133
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.195/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/library/AbstractLibraryTest.java
1
/**
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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2009 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.tools.library;
29

  
30
import junit.framework.TestCase;
31

  
32
/**
33
 * Tests for the {@link AbstractLibrary} class.
34
 * 
35
 * @author gvSIG Team
36
 * @version $Id$
37
 */
38
public class AbstractLibraryTest extends TestCase {
39

  
40
	private AbstractLibrary baseLibrary;
41

  
42
	protected void setUp() throws Exception {
43
		super.setUp();
44
		baseLibrary = new DummyLibrary();
45
	}
46

  
47
	/**
48
	 * Test method for
49
	 * {@link org.gvsig.tools.library.AbstractLibrary.BaseLibrary#initialize()}.
50
	 */
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff