Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.annotation / org.gvsig.annotation.lib / org.gvsig.annotation.lib.api / src / test / java / org / gvsig / annotation / AnnotationCreationServiceTest.java @ 42488

History | View | Annotate | Download (7.59 KB)

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 3
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.annotation;
25

    
26
import java.io.File;
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import org.gvsig.fmap.crs.CRSFactory;
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.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureSet;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.exception.CreateGeometryException;
40
import org.gvsig.fmap.geom.primitive.Point;
41
import org.gvsig.i18n.Messages;
42
import org.gvsig.tools.dispose.DisposableIterator;
43
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
44

    
45

    
46
/**
47
 * API compatibility tests for {@link AnnotationCreationService} implementations.
48
 * 
49
 * @author gvSIG Team
50
 * @version $Id$
51
 */
52
public abstract class AnnotationCreationServiceTest extends
53
    AbstractLibraryAutoInitTestCase {
54

    
55
    protected AnnotationManager manager;
56
    protected DataManager dataManager;
57

    
58
    @Override
59
    protected void doSetUp() throws Exception {
60
        manager = AnnotationLocator.getManager();
61
        dataManager = DALLocator.getDataManager();
62
    }
63

    
64
    /**
65
     * Returns an instance of the {@link AnnotationCreationService}.
66
     * 
67
     * @return a {@link AnnotationCreationService} instance
68
     * @throws Exception
69
     *             if there is any error creating the instance
70
     */
71
    protected AnnotationCreationService createService() throws Exception {
72
            String sourceFileName = getClass().getClassLoader().getResource("org/gvsig/annotation/data/andalucia.shp").getFile();
73
            DataStoreParameters sourceParameters = dataManager.createStoreParameters("Shape");
74
             sourceParameters.setDynValue("shpfile", sourceFileName);
75
             sourceParameters.setDynValue("crs", CRSFactory.getCRS("EPSG:23030"));
76
             FeatureStore sourceStore = (FeatureStore) dataManager.openStore("Shape", sourceParameters);
77
            return manager.getAnnotationCreationService(sourceStore);
78
    }
79

    
80
    /**
81
     * Test for the {@link AnnotationCreationService#getMessage()} method.
82
     * 
83
     * @throws Exception
84
     *             if there is any error in the tests
85
     */
86
    public void testAnnotationServiceMessage() throws Exception {
87
        AnnotationCreationService annotationCreationService = createService();
88
            
89
        File temporalFolder = new File(System.getProperty("java.io.tmpdir") +  File.separator + "tmp_gvsig_annotation");
90
        if (!temporalFolder.exists()){
91
                if (!temporalFolder.mkdir()){
92
                        throw new Exception("Impossible to create the destination folder");
93
                }
94
        }
95
        
96
        String destinationFileName =  temporalFolder.getAbsolutePath() + File.separator + Math.random() + ".shp";                
97
                    
98
        annotationCreationService.createAnnotationStore(destinationFileName, 1);
99
        
100
        DataStoreParameters destinationParameters = dataManager.createStoreParameters("Shape");
101
        destinationParameters.setDynValue("shpfile", destinationFileName);
102
        destinationParameters.setDynValue("crs", CRSFactory.getCRS("EPSG:23030"));
103
            FeatureStore destinationStore = (FeatureStore) dataManager.openStore("Shape", destinationParameters);
104
        
105
            assertNotNull(destinationStore);
106
            
107
            FeatureType featureType = destinationStore.getDefaultFeatureType();
108
            assertEquals(7, featureType.getAttributeDescriptors().length);
109
            
110
             assertEquals(destinationStore.getFeatureCount(), annotationCreationService.getFeatureStore().getFeatureCount());
111
            
112
            assertNotNull(featureType.getDefaultGeometryAttribute());
113
            assertEquals(featureType.getDefaultGeometryAttribute().getGeometryType(), Geometry.TYPES.POINT);
114
                
115
            //Check the geometries
116
            FeatureSet featureSet = destinationStore.getFeatureSet();
117
            DisposableIterator iterator = featureSet.fastIterator();
118
            
119
            Feature feature;
120
            TextPointPairList textPointPairList = getResult();
121
            while (iterator.hasNext()){
122
                    feature = (Feature)iterator.next();
123
                    assertNotNull(feature.get(Messages.getText(AnnotationManager.TEXTVALUE_ATTRIBUTE_NAME)));
124
                    String text = feature.getString(AnnotationManager.TEXTVALUE_ATTRIBUTE_NAME);
125
                    TextPointPair textPointPair = textPointPairList.search(text);
126
                    if (textPointPair != null){
127
                            //Check the geometry
128
                            Point point = (Point)feature.getDefaultGeometry();
129
                            assertEquals(point.getX(), textPointPair.getPoint().getX(), 0.01);
130
                            assertEquals(point.getY(), textPointPair.getPoint().getY(), 0.01);
131
                    }
132
                    int color = feature.getInt(AnnotationManager.FONTCOLOR_ATTRIBUTE_NAME);
133
                    assertEquals(color, manager.getDefaultFontColor()); 
134
                    
135
                    double heigth = feature.getDouble(AnnotationManager.FONTHEGTH_ATTRIBUTE_NAME);
136
                    assertEquals(heigth, manager.getDefaultFontHeight()); 
137
                    
138
                    double rotation = feature.getDouble(AnnotationManager.FONTROTATION_ATTRIBUTE_NAME);
139
                    assertEquals(rotation, manager.getDefaultFontRotation()); 
140
                    
141
                    String type = feature.getString(AnnotationManager.FONTTYPE_ATTRIBUTE_NAME);
142
                    assertEquals(type, manager.getDefaultFontType()); 
143
                    
144
                    String style = feature.getString(AnnotationManager.FONTSTYLE_ATTRIBUTE_NAME);
145
                    assertEquals(style, manager.getDefaultFontStyle());                     
146
            }
147
            
148
            destinationStore.dispose();
149
            annotationCreationService.getFeatureStore().dispose();
150
    }
151
    
152
    public abstract TextPointPairList getResult() throws CreateGeometryException;
153
    
154
    public class TextPointPairList{
155
            List<TextPointPair> textPointPairs = new ArrayList<TextPointPair>();
156

    
157
                public TextPointPairList() {
158
                        super();
159
                }
160
            
161
                public void addPoint(String text, Point point){
162
                        textPointPairs.add(new TextPointPair(text, point));
163
                }
164
                
165
                public TextPointPair search(String text){
166
                        for (TextPointPair textPointPair : textPointPairs) {
167
                                if (textPointPair.getText().equals(text)){
168
                                        return textPointPair;
169
                                }
170
                        }
171
                        return null;
172
                }            
173
    }
174
    
175
    
176
    
177
    private class TextPointPair{
178
            private String text;
179
                private Point point;
180
                
181
            public TextPointPair(String text, Point point) {
182
                        super();
183
                        this.text = text;
184
                        this.point = point;
185
                }
186
            
187
            public String getText() {
188
                        return text;
189
                }
190

    
191
                public Point getPoint() {
192
                        return point;
193
                }            
194
    }
195

    
196
    /**
197
     * Test for the {@link AnnotationCreationService#getManager()} method.
198
     * 
199
     * @throws Exception
200
     *             if there is any error in the tests
201
     */
202
    public void testAnnotationServiceManager() throws Exception {
203
        AnnotationCreationService annotationCreationService = createService();
204
        assertEquals(manager, annotationCreationService.getManager());
205
    }
206
}