Revision 39455

View differences:

tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/commands/CommandTest.java
1
package org.gvsig.fmap.dal.commands;
2

  
3
import junit.framework.TestCase;
4

  
5
public class CommandTest extends TestCase {
6

  
7
	public static void main(String[] args) {
8
		junit.textui.TestRunner.run(CommandTest.class);
9
	}
10

  
11
	protected void setUp() throws Exception {
12
		super.setUp();
13
	}
14

  
15
	public void testInitialize() {
16

  
17
	}
18
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/DataStoreTest.java
1
package org.gvsig.fmap.dal;
2

  
3
import junit.framework.TestCase;
4

  
5
public class DataStoreTest extends TestCase {
6
    
7
    public void testVoid() {
8

  
9
    }
10
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/resource/spi/TestResource.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {}  {{Task}}
26
*/
27
package org.gvsig.fmap.dal.resource.spi;
28

  
29
import org.gvsig.fmap.dal.exception.InitializeException;
30
import org.gvsig.fmap.dal.resource.ResourceParameters;
31
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
32
import org.gvsig.fmap.dal.resource.exception.ResourceException;
33

  
34
/**
35
 * Test non blocking resource implementation.
36
 * 
37
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
38
 */
39
public class TestResource extends AbstractResource {
40

  
41
	protected TestResource(ResourceParameters parameters)
42
			throws InitializeException {
43
		super(parameters);
44
	}
45

  
46
	public Object get() throws AccessResourceException {
47
		return getName();
48
	}
49

  
50
	public String getName() throws AccessResourceException {
51
		return getClass().getName();
52
	}
53

  
54
	public boolean isThis(ResourceParameters parameters)
55
			throws ResourceException {
56
		return false;
57
	}
58
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/resource/spi/AbstractResourceTest.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {}  {{Task}}
26
*/
27
package org.gvsig.fmap.dal.resource.spi;
28

  
29
import junit.framework.TestCase;
30

  
31
import org.easymock.MockControl;
32
import org.gvsig.fmap.dal.resource.ResourceAction;
33
import org.gvsig.fmap.dal.resource.ResourceParameters;
34
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
35

  
36
/**
37
 * Unit tests for the class {@link AbstractResource}
38
 * 
39
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
40
 */
41
public class AbstractResourceTest extends TestCase {
42

  
43
	private AbstractResource resource;
44
	private MockControl paramsControl;
45
	private ResourceParameters params;
46

  
47
	protected void setUp() throws Exception {
48
		super.setUp();
49
		paramsControl = MockControl.createNiceControl(ResourceParameters.class);
50
		params = (ResourceParameters) paramsControl.getMock();
51
		resource = new TestResource(params);
52
	}
53

  
54
	/**
55
	 * Test method for {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#getLastTimeOpen()}.
56
	 */
57
	public void testGetLastTimeOpen() throws ResourceNotifyOpenException {
58
		long time = resource.getLastTimeOpen();
59
		resource.notifyOpen();
60
		assertTrue("The resource last time open hasn't been updated",
61
				resource.getLastTimeOpen() > time);
62
	}
63

  
64
	/**
65
	 * Test method for {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#getLastTimeUsed()}.
66
	 */
67
	public void testGetLastTimeUsed() throws Exception {
68
		long time = resource.getLastTimeUsed();
69
		resource.execute(new ResourceAction() {
70
			public Object run() throws Exception {
71
				try {
72
					// Wait for 100 milliseconds
73
					Thread.sleep(100);
74
				} catch (InterruptedException e) {
75
					// No problem
76
				}
77
				return null;
78
			}
79
		});
80
		assertTrue("The resource last time used hasn't been updated",
81
				resource.getLastTimeUsed() > time);
82
	}
83

  
84
	/**
85
	 * Test method for {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#openCount()}.
86
	 */
87
	public void testOpenCount() throws Exception {
88
		assertEquals(0, resource.openCount());
89
		resource.notifyOpen();
90
		assertEquals(1, resource.openCount());
91
		resource.notifyOpen();
92
		resource.notifyOpen();
93
		assertEquals(3, resource.openCount());
94
		resource.notifyClose();
95
		assertEquals(2, resource.openCount());
96
	}
97

  
98
	/**
99
	 * Test method for
100
	 * {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#execute(java.lang.Runnable)}
101
	 * .
102
	 */
103
	public void testExecute() throws Exception {
104
		final MutableBoolean testValue = new MutableBoolean();
105
		resource.execute(new ResourceAction() {
106
			public Object run() throws Exception {
107
				testValue.setValue(true);
108
				return null;
109
			}
110
		});
111
		assertTrue("Runnable execution not performed", testValue.isValue());
112
	}
113

  
114
	public class MutableBoolean {
115
		private boolean value = false;
116

  
117
		public void setValue(boolean value) {
118
			this.value = value;
119
		}
120

  
121
		public boolean isValue() {
122
			return value;
123
		}
124
	}
125
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/resource/spi/TestNonBlockingResource.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {}  {{Task}}
26
*/
27
package org.gvsig.fmap.dal.resource.spi;
28

  
29
import org.gvsig.fmap.dal.exception.InitializeException;
30
import org.gvsig.fmap.dal.resource.ResourceParameters;
31
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
32
import org.gvsig.fmap.dal.resource.exception.ResourceException;
33

  
34
/**
35
 * Test resource implementation.
36
 * 
37
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
38
 */
39
public class TestNonBlockingResource extends AbstractNonBlockingResource {
40

  
41
	protected TestNonBlockingResource(ResourceParameters parameters)
42
			throws InitializeException {
43
		super(parameters);
44
	}
45

  
46
	public Object get() throws AccessResourceException {
47
		return getName();
48
	}
49

  
50
	public String getName() throws AccessResourceException {
51
		return getClass().getName();
52
	}
53

  
54
	public boolean isThis(ResourceParameters parameters)
55
			throws ResourceException {
56
		return false;
57
	}
58
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/resource/spi/TestAbstractResourcePerformance.java
1
package org.gvsig.fmap.dal.resource.spi;
2

  
3
import junit.framework.TestCase;
4

  
5
import org.easymock.MockControl;
6
import org.gvsig.fmap.dal.resource.Resource;
7
import org.gvsig.fmap.dal.resource.ResourceAction;
8
import org.gvsig.fmap.dal.resource.ResourceParameters;
9

  
10
public class TestAbstractResourcePerformance extends TestCase {
11
	private MockControl paramsControl;
12
	private ResourceParameters params;
13
	private AbstractResource resource;
14
	private MockControl paramsControl2;
15
	private ResourceParameters params2;
16
	private AbstractNonBlockingResource nonBlockingResource;
17

  
18
	protected void setUp() throws Exception {
19
		super.setUp();
20
		paramsControl = MockControl.createNiceControl(ResourceParameters.class);
21
		params = (ResourceParameters) paramsControl.getMock();
22
		resource = new TestResource(params);
23
		paramsControl2 =
24
				MockControl.createNiceControl(ResourceParameters.class);
25
		params2 = (ResourceParameters) paramsControl2.getMock();
26
		nonBlockingResource = new TestNonBlockingResource(params2);
27
	}
28

  
29
	/**
30
	 * Test method for
31
	 * {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#execute(java.lang.Runnable)}
32
	 * .
33
	 */
34
	public void testExecuteBlockingPerformance() throws Exception {
35
		executePerformance(new ResourceAction() {
36
			public Object run() throws Exception {
37
				return null;
38
			}
39
		}, resource);
40
	}
41

  
42
	/**
43
	 * Test method for
44
	 * {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#execute(java.lang.Runnable)}
45
	 * .
46
	 */
47
	public void testExecuteNonBlockingPerformance() throws Exception {
48
		executePerformance(new ResourceAction() {
49
			public Object run() throws Exception {
50
				return null;
51
			}
52
		}, nonBlockingResource);
53
	}
54

  
55
	/**
56
	 * Test method for
57
	 * {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#execute(java.lang.Runnable)}
58
	 * .
59
	 */
60
	public void executePerformance(ResourceAction action, Resource resource)
61
			throws Exception {
62

  
63
		final int executions = 100000;
64
		long time1 = System.currentTimeMillis();
65
		for (int i = 0; i < executions; i++) {
66
			resource.execute(action);
67
		}
68
		long time2 = System.currentTimeMillis();
69

  
70
		System.out.print(resource.getName());
71
		System.out.print(": total time: " + (time2 - time1) + " ms.");
72
		System.out.println(" - time per execution: "
73
				+ ((float) (time2 - time1) / (float) executions) + " ms.");
74
	}
75
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/AllTests.java
1
package org.gvsig.fmap.dal;
2

  
3
import junit.framework.Test;
4
import junit.framework.TestCase;
5
import junit.framework.TestSuite;
6

  
7
import org.gvsig.fmap.dal.commands.CommandTest;
8
import org.gvsig.fmap.dal.feature.FeatureTest;
9

  
10
public class AllTests extends TestCase{
11
	public static Test suite() {
12
		TestSuite suite = new TestSuite("Test for libDataSource");
13
		//$JUnit-BEGIN$
14
		suite.addTestSuite(CommandTest.class);
15
		suite.addTestSuite(DataStoreTest.class);
16
		suite.addTestSuite(FeatureTest.class);
17

  
18
		//$JUnit-END$
19
		return suite;
20
	}
21
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/feature/FeatureSetTestParent.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}   {{Test indexed iterator retrieval}}
26
 */
27
package org.gvsig.fmap.dal.feature;
28

  
29
import junit.framework.TestCase;
30

  
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataManager;
33
import org.gvsig.fmap.dal.DataStoreParameters;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.tools.dispose.DisposableIterator;
36

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

  
44

  
45
    protected void setUp() throws Exception {
46
        super.setUp();
47
        register();
48
    }
49

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

  
54
    /**
55
     * Test method for
56
     * {@link org.gvsig.fmap.dal.feature.AbstractFeatureCollection#iterator(int)}
57
     * .
58
     *
59
     * @throws Exception
60
     */
61
    public void testIteratorInt() throws Exception {
62
        FeatureSet featureSet = createFeatureCollection();
63
        testIteratorInt(featureSet);
64
        featureSet.dispose();
65
    }
66

  
67
    public void testIteratorInt(FeatureSet featureSet) throws DataException {
68

  
69
        if (featureSet.getSize() < 3) {
70
            fail("The Collection to create for the test must contain "
71
                    + "at least 3 values");
72
        }
73

  
74
        try {
75
            featureSet.iterator(-5);
76
            fail("Iterator index accepted with a value < 0");
77
        } catch (Exception ex) {
78
            // Good
79
        }
80

  
81
        try {
82
            featureSet.iterator(featureSet.getSize() + 2);
83
            fail("Iterator index accepted with a value > collection size");
84
        } catch (Exception ex) {
85
            // Good
86
        }
87

  
88
        long index = featureSet.getSize() - 3l;
89
        DisposableIterator iter = featureSet.iterator(index);
90
        int iterations = 0;
91
        while (iter.hasNext()) {
92
            iter.next();
93
            iterations++;
94
        }
95
        assertEquals("The number of iterations remaining is not correct", 3,
96
                iterations);
97
        iter.dispose();
98

  
99
        iter = featureSet.iterator(0);
100
        iterations = 0;
101
        while (iter.hasNext()) {
102
            iter.next();
103
            iterations++;
104
        }
105
        assertEquals("The number of iterations is not the "
106
                + "total size of the collection", featureSet.getSize(),
107
                iterations);
108
        iter.dispose();
109
    }
110

  
111
    protected FeatureSet createFeatureCollection() throws Exception {
112
        FeatureStore store = createFeatureStore();
113
        FeatureType ft = store.getDefaultFeatureType();
114
        return createFeatureCollection(store, ft);
115
    }
116

  
117
    protected FeatureStore createFeatureStore() throws Exception {
118
        DataManager manager = DALLocator.getDataManager();
119
        return (FeatureStore) manager
120
                .createStore(createStoreParameters(manager));
121
    }
122

  
123
    protected abstract void register();
124

  
125
    protected abstract DataStoreParameters createStoreParameters(
126
            DataManager manager)
127
            throws Exception;
128

  
129
    /**
130
     * If this method is rewritten, the returned FeatureCollection must have at
131
     * least 3 values.
132
     */
133
    protected abstract FeatureSet createFeatureCollection(
134
            FeatureStore store,
135
            FeatureType ft) throws DataException;
136
}
0 137

  
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/feature/impl/MyTransform.java
1
package org.gvsig.fmap.dal.feature.impl;
2

  
3
import java.util.Arrays;
4

  
5
import org.gvsig.fmap.dal.DataTypes;
6
import org.gvsig.fmap.dal.exception.DataException;
7
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
8
import org.gvsig.fmap.dal.feature.EditableFeature;
9
import org.gvsig.fmap.dal.feature.EditableFeatureType;
10
import org.gvsig.fmap.dal.feature.Feature;
11
import org.gvsig.fmap.dal.feature.FeatureStore;
12
import org.gvsig.fmap.dal.feature.FeatureType;
13
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
14
import org.gvsig.fmap.geom.GeometryLocator;
15
import org.gvsig.fmap.geom.GeometryManager;
16
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
17
import org.gvsig.fmap.geom.Geometry.TYPES;
18
import org.gvsig.fmap.geom.primitive.Point;
19
import org.gvsig.tools.persistence.PersistentState;
20
import org.gvsig.tools.persistence.exception.PersistenceException;
21

  
22
/**
23
 *
24
 * This transform adds a new attribute of type Geometry to the original store's
25
 * default FeatureType. When applying the transform to a single feature this new
26
 * attribute is assigned the value of a point whose coordinates proceed from two
27
 * numeric attributes from the store, called xname, yname.
28
 *
29
 */
30
class MyTransform extends AbstractFeatureStoreTransform {
31

  
32
	private FeatureType originalType;
33
	private String geomName;
34
	private String xname;
35
	private String yname;
36

  
37
	/**
38
	 * Empty default constructor
39
	 */
40
	public MyTransform() {
41
	}
42

  
43
	/**
44
	 * Initializes the transform by assigning the source store and the names of
45
	 * the necessary attributes.
46
	 *
47
	 * @param store
48
	 *            source store.
49
	 *
50
	 * @param geomName
51
	 *            name of the geometry attribute in the default feature type
52
	 *            from the source store.
53
	 *
54
	 * @param xname
55
	 *            name of the attribute containing the X coordinates
56
	 *
57
	 * @param yname
58
	 *            name of the attribute containing the Y coordinates
59
	 *
60
	 * @throws DataException
61
	 */
62
	public void initialize(FeatureStore store, String geomName, String xname, String yname) throws DataException {
63

  
64
		// Initialize some data
65
		this.setFeatureStore(store);
66
		this.geomName = geomName;
67
		this.xname = xname;
68
		this.yname = yname;
69

  
70
		this.originalType = store.getDefaultFeatureType();
71
		// obtain the feature type, add the new attribute and keep a reference to the resulting feature type
72
		EditableFeatureType type = store.getDefaultFeatureType().getEditable();
73
		type.add(geomName, DataTypes.GEOMETRY);
74
		FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() };
75
		setFeatureTypes(Arrays.asList(types), types[0]);
76
	}
77

  
78
	/**
79
	 * Applies this transform to a target editable feature, using data from the
80
	 * source feature.
81
	 */
82
	public void applyTransform(Feature source, EditableFeature target)
83
			throws DataException {
84

  
85
		// copy source feature data over target feature
86
		target.copyFrom(source);
87

  
88
		// calculate and assign new attribute's value
89
		GeometryManager geomManager = GeometryLocator.getGeometryManager();
90
		Point point;
91
		try {
92
			point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
93
			point.setX(source.getDouble(xname));
94
			point.setY(source.getDouble(yname));
95
			target.setGeometry(this.geomName, point);
96
		} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
97
        	throw new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e);
98
		}
99
	}
100

  
101
	public void saveToState(PersistentState state) throws PersistenceException {
102
		state.set("geomName", this.geomName);
103
		state.set("xname", this.xname);
104
		state.set("yname", this.yname);
105
	}
106

  
107
	public void loadFromState(PersistentState state) throws PersistenceException {
108
	}
109

  
110
	/*
111
	 * (non-Javadoc)
112
	 *
113
	 * @see
114
	 * org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom
115
	 * (org.gvsig.fmap.dal.feature.FeatureType)
116
	 */
117
	public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
118
		return originalType;
119
	}
120

  
121
	public boolean isTransformsOriginalValues() {
122
		return false;
123
	}
124

  
125
}
0 126

  
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/feature/impl/DefaultFeatureReferenceSelectionTest.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {{Task}}
26
 */
27
package org.gvsig.fmap.dal.feature.impl;
28

  
29
import java.util.Iterator;
30

  
31
import junit.framework.TestCase;
32

  
33
import org.easymock.MockControl;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.FeatureReference;
36
import org.gvsig.fmap.dal.feature.FeatureReferenceSelection;
37
import org.gvsig.fmap.dal.feature.FeatureSet;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.tools.exception.BaseException;
40
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
41
import org.gvsig.tools.visitor.Visitor;
42

  
43
/**
44
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
45
 */
46
public class DefaultFeatureReferenceSelectionTest extends AbstractLibraryAutoInitTestCase {
47

  
48
    private FeatureReferenceSelection selection;
49
    private MockControl refControl1;
50
    private FeatureReference ref1;
51
    private MockControl refControl2;
52
    private FeatureReference ref2;
53
    private MockControl storeControl;
54
    private FeatureStore store;
55
    private MockControl fsetControl;
56
    private FeatureSet featureSet;
57
    private MockControl helperControl;
58
    private FeatureSelectionHelper helper;
59

  
60
    private int total = 10;
61

  
62
    protected void doSetUp() throws Exception {     
63
        refControl1 = MockControl.createNiceControl(FeatureReference.class);
64
        ref1 = (FeatureReference) refControl1.getMock();
65
        refControl2 = MockControl.createNiceControl(FeatureReference.class);
66
        ref2 = (FeatureReference) refControl2.getMock();
67

  
68
        storeControl = MockControl.createNiceControl(FeatureStore.class);
69
        store = (FeatureStore) storeControl.getMock();
70
        storeControl.expectAndReturn(store.getFeatureCount(), total);
71

  
72
        helperControl = MockControl
73
                .createNiceControl(FeatureSelectionHelper.class);
74
        helper = (FeatureSelectionHelper) helperControl.getMock();
75
        helperControl.expectAndDefaultReturn(helper.getFeatureStoreDeltaSize(),
76
                0);
77

  
78
        fsetControl = MockControl.createNiceControl(FeatureSet.class);
79
        featureSet = (FeatureSet) fsetControl.getMock();
80

  
81
        storeControl.expectAndReturn(store.getFeatureSet(), featureSet);
82
        fsetControl.expectAndReturn(featureSet.getSize(), total);
83

  
84

  
85

  
86
        storeControl.replay();
87
        fsetControl.replay();
88
        helperControl.replay();
89

  
90
        selection = new DefaultFeatureReferenceSelection(store, helper);
91
    }
92

  
93
    protected void tearDown() throws Exception {
94
        super.tearDown();
95
    }
96

  
97
    public void testSelect() throws Exception {
98
        // Add a feature
99
        selection.select(ref1);
100
        assertTrue(selection.isSelected(ref1));
101
        assertFalse(selection.isSelected(ref2));
102

  
103
        selection.select(ref2);
104
        assertTrue(selection.isSelected(ref2));
105
    }
106

  
107
    public void testSelectAll() throws Exception {
108
        selection.selectAll();
109
        assertTrue(selection.isSelected(ref1));
110
        assertTrue(selection.isSelected(ref2));
111
    }
112

  
113
    public void testDeselect() throws Exception {
114
        // Remove a feature
115
        selection.select(ref1);
116
        selection.select(ref2);
117
        assertTrue(selection.isSelected(ref2));
118
        selection.deselect(ref2);
119
        assertFalse(selection.isSelected(ref2));
120
        selection.deselect(ref1);
121
        assertFalse(selection.isSelected(ref1));
122
    }
123

  
124
    public void testDeselectAll() throws Exception {
125
        selection.select(ref1);
126
        selection.select(ref2);
127
        selection.deselectAll();
128
        assertFalse(selection.isSelected(ref1));
129
        assertFalse(selection.isSelected(ref2));
130
    }
131

  
132
    public void testReverse() throws Exception {
133
        // Reverse selection
134
        selection.select(ref1);
135
        selection.reverse();
136
        assertFalse(selection.isSelected(ref1));
137
        assertTrue(selection.isSelected(ref2));
138
    }
139

  
140
    public void testGetSelectedCount() throws Exception {
141
        // None selected
142
        assertEquals(0, selection.getSelectedCount());
143
        selection.select(ref1);
144
        selection.select(ref2);
145
        // Two selected
146
        assertEquals(2, selection.getSelectedCount());
147
        // Deselect one, so one left selected
148
        selection.deselect(ref2);
149
        assertEquals(1, selection.getSelectedCount());
150
        // Reverse the selection, so all buy one selected
151
        selection.reverse();
152
        assertEquals(total - 1, selection.getSelectedCount());
153
        // Deselect another one, so all but two selected
154
        selection.deselect(ref2);
155
        assertEquals(total - 2, selection.getSelectedCount());
156
        // Deselect an already deselected one, nothing changes
157
        selection.deselect(ref2);
158
        assertEquals(total - 2, selection.getSelectedCount());
159
    }
160

  
161
    public void testGetSelectedValues() throws DataException {
162
        selection.deselectAll();
163
        selection.select(ref1);
164
        selection.select(ref2);
165

  
166
        boolean ref1Pending = true;
167
        boolean ref2Pending = true;
168

  
169
        for (Iterator references = selection.referenceIterator(); references
170
                .hasNext();) {
171
            FeatureReference reference = (FeatureReference) references.next();
172
            if (reference.equals(ref1)) {
173
                if (ref1Pending) {
174
                    ref1Pending = false;
175
                } else {
176
                    fail("FeatureReference 1 was already obtained");
177
                }
178
            } else if (reference.equals(ref2)) {
179
                if (ref2Pending) {
180
                    ref2Pending = false;
181
                } else {
182
                    fail("FeatureReference 2 was already obtained");
183
                }
184
            } else {
185
                fail("A reference not selected was obtained: " + reference);
186
            }
187
        }
188
    }
189

  
190
    public void testDispose() {
191
        selection.select(ref1);
192
        selection.select(ref2);
193
        selection.dispose();
194
        assertFalse(selection.isSelected(ref1));
195
        assertFalse(selection.isSelected(ref2));
196
    }
197

  
198
    public void testAccept() throws BaseException {
199
        // Create a Mopck Visitor and add expected usage
200
        MockControl visitorControl = MockControl
201
                .createNiceControl(Visitor.class);
202
        Visitor visitor = (Visitor) visitorControl.getMock();
203

  
204
        visitor.visit(ref1);
205
        visitor.visit(ref2);
206
        visitorControl.replay();
207

  
208
        // Add selected references
209
        selection.select(ref1);
210
        selection.select(ref2);
211

  
212
        // Use visitor
213
        selection.accept(visitor);
214

  
215
        // check correct visitor usage
216
        visitorControl.verify();
217
    }
218
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/feature/impl/DefaultFeatureSelectionTest.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {{Task}}
26
 */
27
package org.gvsig.fmap.dal.feature.impl;
28

  
29
import org.easymock.MockControl;
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureReference;
32
import org.gvsig.fmap.dal.feature.FeatureSelection;
33
import org.gvsig.fmap.dal.feature.FeatureSet;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
37

  
38
/**
39
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
40
 */
41
public class DefaultFeatureSelectionTest extends AbstractLibraryAutoInitTestCase {
42
    private FeatureSelection selection;
43
    private MockControl refControl1;
44
    private FeatureReference ref1;
45
    private MockControl refControl2;
46
    private FeatureReference ref2;
47
    private MockControl fControl1;
48
    private Feature feature1;
49
    private MockControl fControl2;
50
    private Feature feature2;
51
    private MockControl storeControl;
52
    private FeatureType featureType;
53
    private MockControl fTypeControl;
54
    private FeatureStore store;
55
    private MockControl fsetControl;
56
    private FeatureSet featureSet;
57
    private MockControl helperControl;
58
    private FeatureSelectionHelper helper;
59

  
60
    private int total = 10;
61

  
62
    protected void doSetUp() throws Exception { 
63
        refControl1 = MockControl.createNiceControl(FeatureReference.class);
64
        ref1 = (FeatureReference) refControl1.getMock();
65
        refControl2 = MockControl.createNiceControl(FeatureReference.class);
66
        ref2 = (FeatureReference) refControl2.getMock();
67

  
68
        fControl1 = MockControl.createControl(Feature.class);
69
        feature1 = (Feature) fControl1.getMock();
70
        fControl2 = MockControl.createControl(Feature.class);
71
        feature2 = (Feature) fControl2.getMock();
72

  
73
        fTypeControl = MockControl.createNiceControl(FeatureType.class);
74
        featureType = (FeatureType) fTypeControl.getMock();
75

  
76
        fControl1.expectAndDefaultReturn(feature1.getType(), featureType);
77
        fControl2.expectAndDefaultReturn(feature2.getType(), featureType);
78

  
79
        storeControl = MockControl.createNiceControl(FeatureStore.class);
80
        store = (FeatureStore) storeControl.getMock();
81
        storeControl.expectAndReturn(store.getFeatureCount(), total);
82

  
83
        helperControl = MockControl
84
                .createNiceControl(FeatureSelectionHelper.class);
85
        helper = (FeatureSelectionHelper) helperControl.getMock();
86
        helperControl.expectAndDefaultReturn(helper.getFeatureStoreDeltaSize(),
87
                0);
88

  
89
        fsetControl = MockControl.createNiceControl(FeatureSet.class);
90
        featureSet = (FeatureSet) fsetControl.getMock();
91

  
92
        storeControl.expectAndReturn(store.getFeatureSet(), featureSet);
93
        fsetControl.expectAndReturn(featureSet.getSize(), total);
94

  
95
        storeControl.replay();
96
        fsetControl.replay();
97
        helperControl.replay();
98

  
99
        selection = new DefaultFeatureSelection(store, helper);
100
    }
101

  
102
    protected void tearDown() throws Exception {
103
        super.tearDown();
104
    }
105

  
106
    /**
107
     * Test method for
108
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#select(org.gvsig.fmap.dal.feature.Feature)}
109
     * .
110
     */
111
    public void testSelectFeature() {
112
        fControl1.expectAndReturn(feature1.getReference(), ref1, 2);
113
        fControl2.expectAndReturn(feature2.getReference(), ref2, 3);
114
        fControl1.replay();
115
        fControl2.replay();
116

  
117
        // Add a feature
118
        selection.select(feature1);
119
        assertTrue("Selected feature1 is not recognized as selected", selection
120
                .isSelected(feature1));
121
        assertFalse("Selected feature2 is recognized as selected", selection
122
                .isSelected(feature2));
123

  
124
        selection.select(feature2);
125
        assertTrue(selection.isSelected(feature2));
126
    }
127

  
128
    /**
129
     * Test method for
130
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#deselect(org.gvsig.fmap.dal.feature.Feature)}
131
     * .
132
     */
133
    public void testDeselectFeature() {
134
        fControl1.expectAndReturn(feature1.getReference(), ref1, 3);
135
        fControl2.expectAndReturn(feature2.getReference(), ref2, 4);
136
        fControl1.replay();
137
        fControl2.replay();
138

  
139
        // Remove a feature
140
        selection.select(feature1);
141
        selection.select(feature2);
142
        assertTrue(selection.isSelected(feature2));
143
        selection.deselect(feature2);
144
        assertFalse(selection.isSelected(feature2));
145
        selection.deselect(feature1);
146
        assertFalse(selection.isSelected(feature1));
147
    }
148

  
149
    /**
150
     * Test method for
151
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#getSize()}
152
     * .
153
     */
154
    public void testGetSize() throws Exception {
155
        fControl1.expectAndReturn(feature1.getReference(), ref1, 1);
156
        fControl2.expectAndReturn(feature2.getReference(), ref2, 4);
157
        fControl1.replay();
158
        fControl2.replay();
159

  
160
        // None selected
161
        assertEquals(0, selection.getSize());
162
        selection.select(feature1);
163
        selection.select(feature2);
164
        // Two selected
165
        assertEquals(2, selection.getSize());
166
        // Deselect one, so one left selected
167
        selection.deselect(feature2);
168
        assertEquals(1, selection.getSize());
169
        // Reverse the selection, so all buy one selected
170
        selection.reverse();
171
        assertEquals(total - 1, selection.getSize());
172
        // Deselect another one, so all but two selected
173
        selection.deselect(feature2);
174
        assertEquals(total - 2, selection.getSize());
175
        // Deselect an already deselected one, nothing changes
176
        selection.deselect(feature2);
177
        assertEquals(total - 2, selection.getSize());
178
    }
179

  
180
    /**
181
     * Test method for
182
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#isEmpty()}
183
     * .
184
     */
185
    public void testIsEmpty() throws Exception {
186
        fControl1.expectAndReturn(feature1.getReference(), ref1, 1);
187
        fControl2.expectAndReturn(feature2.getReference(), ref2, 4);
188
        fControl1.replay();
189
        fControl2.replay();
190

  
191
        // None selected
192
        assertTrue(selection.isEmpty());
193

  
194
        // One selected
195
        selection.select(feature1);
196
        assertFalse(selection.isEmpty());
197

  
198
        // Deselect all
199
        selection.deselectAll();
200
        assertTrue(selection.isEmpty());
201

  
202
        // Reverse
203
        selection.reverse();
204
        assertFalse(selection.isEmpty());
205
    }
206

  
207
}
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/feature/impl/JoinTransform.java
1
package org.gvsig.fmap.dal.feature.impl;
2

  
3
import java.util.Arrays;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.Map;
7

  
8
import org.gvsig.fmap.dal.DALLocator;
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
11
import org.gvsig.fmap.dal.feature.EditableFeature;
12
import org.gvsig.fmap.dal.feature.EditableFeatureType;
13
import org.gvsig.fmap.dal.feature.Feature;
14
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.dal.feature.FeatureQuery;
16
import org.gvsig.fmap.dal.feature.FeatureSet;
17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.gvsig.tools.dispose.DisposableIterator;
20
import org.gvsig.tools.dispose.DisposeUtils;
21
import org.gvsig.tools.evaluator.Evaluator;
22
import org.gvsig.tools.persistence.PersistentState;
23
import org.gvsig.tools.persistence.exception.PersistenceException;
24

  
25
public class JoinTransform extends AbstractFeatureStoreTransform {
26

  
27
	/**
28
	 * Store from which the join transform will get the additional attributes
29
	 */
30
	private FeatureStore store2;
31

  
32
	/**
33
	 * name of the key attr in store1 that will be used to match features in
34
	 * store2
35
	 */
36
	private String keyAttr1;
37

  
38
	/**
39
	 * name of the key attr in store2 that will be used to match features in
40
	 * store1
41
	 */
42
	private String keyAttr2;
43

  
44
	/**
45
	 * names of the attributes to join from store2 to store1
46
	 */
47
	private String[] attrs;
48

  
49
	/**
50
	 * Attribute names may change after transformation if they are repeated in
51
	 * both stores. This map keeps correspondence between store2 original names
52
	 * and their transformed counterparts.
53
	 */
54
	private Map targetNamesMap;
55

  
56
	private FeatureType originalFeatureType;
57

  
58
	/**
59
	 * A default constructor
60
	 */
61
	public JoinTransform() {
62
		targetNamesMap = new HashMap();
63
	}
64

  
65
	/**
66
	 * Initializes all the necessary data for this transform
67
	 *
68
	 * @param store1
69
	 *            store whose default feature type is the target of this
70
	 *            transform
71
	 *
72
	 * @param store2
73
	 *            store whose default feature type will provide the new
74
	 *            attributes to join
75
	 *
76
	 * @param keyAttr1
77
	 *            key attribute in store1 that matches keyAttr2 in store2
78
	 *            (foreign key), used for joining both stores.
79
	 *
80
	 * @param keyAttr2
81
	 *            key attribute in store2 that matches keyAttr1 in store2
82
	 *            (foreign key), used for joining both stores.
83
	 *
84
	 * @param attrs
85
	 *            names of the attributes in store2 that will be joined to
86
	 *            store1.
87
	 */
88
	public void initialize(FeatureStore store1, FeatureStore store2,
89
			String keyAttr1, String keyAttr2, String[] attrs)
90
			throws DataException {
91

  
92
		// Initialize needed data
93
		this.setFeatureStore(store1);
94

  
95
		this.store2 = store2;
96
		this.keyAttr1 = keyAttr1;
97
		this.keyAttr2 = keyAttr2;
98
		this.attrs = attrs;
99
		this.originalFeatureType = this.getFeatureStore()
100
				.getDefaultFeatureType();
101

  
102
		// calculate this transform resulting feature type
103
		// by adding all specified attrs from store2 to store1's default
104
		// feature type
105
		EditableFeatureType type = this.getFeatureStore().getDefaultFeatureType().getEditable();
106

  
107
		for (int i = 0; i < attrs.length; i++) {
108
			String name = attrs[i];
109

  
110
			// If an attribute already exists with the same name in store1's
111
			// default feature type,
112
			// calculate an alternate name and add it to our type
113
			int j = 0;
114
			while (type.getIndex(name) >= 0) {
115
				name = attrs[i] + "_" + ++j;
116
			}
117
			type.add(name, store2.getDefaultFeatureType()
118
					.getAttributeDescriptor(attrs[i]).getType());
119

  
120
			// keep correspondence between original name and transformed name
121
			this.targetNamesMap.put(attrs[i], name);
122
		}
123

  
124
		// assign calculated feature type as this transform's feature type
125
		FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() };
126
		setFeatureTypes(Arrays.asList(types), types[0]);
127
	}
128

  
129
	/**
130
	 *
131
	 *
132
	 * @param source
133
	 *
134
	 * @param target
135
	 *
136
	 * @throws DataException
137
	 */
138
	public void applyTransform(Feature source, EditableFeature target)
139
			throws DataException {
140

  
141
		// copy the data from store1 into the resulting feature
142
		target.copyFrom(source);
143

  
144
		// ask store2 for the specified attributes, filtering by the key
145
		// attribute value
146
		// from the source feature
147
		Evaluator eval = DALLocator.getDataManager().createExpresion(
148
				keyAttr2 + "=" + source.get(keyAttr1));
149
		FeatureQuery query = this.getFeatureStore().createFeatureQuery();
150
		query.setAttributeNames(attrs);
151
		query.setFilter(eval);
152

  
153
		FeatureSet set = null;
154
		DisposableIterator it = null;
155

  
156
		try {
157
			set = store2.getFeatureSet(query);
158

  
159
			// In this join implementation, we will take only the first matching
160
			// feature found in store2
161
			it = set.iterator();
162
			if (it.hasNext()) {
163
				Feature feat = (Feature) it.next();
164

  
165
				// copy all attributes from joined feature to target
166
				Iterator it2 = feat.getType().iterator();
167
				while (it2.hasNext()) {
168
					FeatureAttributeDescriptor attr =
169
							(FeatureAttributeDescriptor) it2.next();
170
					// find original attribute name
171
					String targetName =
172
							(String) this.targetNamesMap.get(attr.getName());
173
					// copy its value to target feature attribute
174
					target.set(targetName, feat.get(attr.getName()));
175
				}
176
			}
177
		} finally {
178
			DisposeUtils.dispose(set);
179
			DisposeUtils.dispose(it);
180
		}
181
	}
182

  
183
	public void saveToState(PersistentState state) throws PersistenceException {
184
		// TODO Auto-generated method stub
185

  
186
	}
187

  
188
	public void loadFromState(PersistentState state) throws PersistenceException {
189
		// TODO Auto-generated method stub
190

  
191
	}
192

  
193
	/*
194
	 * (non-Javadoc)
195
	 *
196
	 * @see
197
	 * org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom
198
	 * (org.gvsig.fmap.dal.feature.FeatureType)
199
	 */
200
	public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
201
		return this.originalFeatureType;
202
	}
203

  
204
	public boolean isTransformsOriginalValues() {
205
		return false;
206
	}
207

  
208
}
0 209

  
tags/v2_0_0_Build_2061/libraries/libFMap_dal/src-test/org/gvsig/fmap/dal/feature/BaseTestFeatureStore.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I. S.A.   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.feature;
32

  
33
import java.util.ArrayList;
34
import java.util.Collections;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Random;
38
import java.util.TreeSet;
39

  
40
import org.gvsig.fmap.dal.DALLocator;
41
import org.gvsig.fmap.dal.DataManager;
42
import org.gvsig.fmap.dal.DataServerExplorer;
43
import org.gvsig.fmap.dal.DataStoreParameters;
44
import org.gvsig.fmap.dal.DataTypes;
45
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
46
import org.gvsig.fmap.dal.exception.DataException;
47
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
48
import org.gvsig.fmap.dal.feature.testmulithread.StoreTask;
49
import org.gvsig.fmap.dal.resource.ResourceManager;
50
import org.gvsig.fmap.geom.Geometry;
51
import org.gvsig.tools.ToolsLocator;
52
import org.gvsig.tools.dispose.DisposableIterator;
53
import org.gvsig.tools.dynobject.DynClass;
54
import org.gvsig.tools.dynobject.DynField;
55
import org.gvsig.tools.dynobject.DynObject;
56
import org.gvsig.tools.evaluator.Evaluator;
57
import org.gvsig.tools.evaluator.EvaluatorData;
58
import org.gvsig.tools.evaluator.EvaluatorException;
59
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
60
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
61
import org.gvsig.tools.persistence.PersistentState;
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

  
65
/**
66
 * @author jmvivo
67
 *
68
 */
69
public abstract class BaseTestFeatureStore extends
70
		AbstractLibraryAutoInitTestCase {
71

  
72
	private static Logger logger = null;
73

  
74
	protected DataManager dataManager = null;
75
	private static Random rnd;
76

  
77
	public Logger getLogger() {
78
		if (logger == null) {
79
			logger = LoggerFactory.getLogger(this.getClass());
80
		}
81
		return logger;
82
	}
83

  
84
	public abstract boolean usesResources();
85

  
86
	public abstract boolean hasExplorer();
87

  
88
	public FeatureQuery getDefaultQuery(FeatureStore store)
89
			throws DataException {
90
		FeatureQuery query = store.createFeatureQuery();
91
		FeatureAttributeDescriptor[] key = store.getDefaultFeatureType()
92
				.getPrimaryKey();
93
		for (int i = 0; i < key.length; i++) {
94
			query.getOrder().add(key[i].getName(), true);
95
		}
96

  
97
		return query;
98
	}
99

  
100

  
101
	public abstract DataStoreParameters getDefaultDataStoreParameters()
102
			throws DataException;
103

  
104

  
105
	protected void doSetUp() throws Exception {
106
		dataManager = DALLocator.getDataManager();
107
	}
108

  
109

  
110
	//=================================================
111

  
112

  
113
	public void printFeature(Feature feature, int maxColSize) {
114
		printFeature(feature, true, maxColSize);
115
	}
116

  
117

  
118
	public void printFeature(Feature feature, boolean showColName,int maxColSize) {
119
		FeatureType fType = feature.getType();
120
		if (showColName){
121
			this.printFeatureTypeColNames(feature.getType(), maxColSize);
122
		}
123
		StringBuffer row = new StringBuffer();
124
		Iterator iter = fType.iterator();
125
		FeatureAttributeDescriptor attr;
126

  
127
		while (iter.hasNext()) {
128
			attr = (FeatureAttributeDescriptor) iter.next();
129
			row.append(truncateOrFillString(feature.get(attr.getName()),
130
					maxColSize + 1, ' '));
131
		}
132
		System.out.println(row.toString());
133
	}
134

  
135
	public String truncateOrFillString(Object str, int max, char fillWith) {
136
		if (str == null) {
137
			return truncateOrFillString("{null}", max, fillWith);
138
		}
139
		return truncateOrFillString(str.toString(), max, fillWith);
140
	}
141

  
142
	public String truncateOrFillString(String str, int max, char fillWith) {
143
		if (str.length() > max) {
144
			return str.substring(0, max - 1) + " ";
145
		} else {
146
			StringBuffer strB = new StringBuffer(str);
147
			while (strB.length() < max) {
148
				strB.append(fillWith);
149
			}
150
			return strB.toString();
151
		}
152

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff