Statistics
| Revision:

root / trunk / extensions / extSymbology / src-test / org / gvsig / remoteClient / sld / symbolizerTests / TestParsePolygonSymbolizer.java @ 20768

History | View | Annotate | Download (8.8 KB)

1
package org.gvsig.remoteClient.sld.symbolizerTests;
2
import java.awt.BasicStroke;
3
import java.awt.Color;
4
import java.io.File;
5
import java.io.IOException;
6
import java.net.MalformedURLException;
7
import java.net.URL;
8
import java.net.UnknownHostException;
9
import java.util.ArrayList;
10

    
11
import junit.framework.TestCase;
12

    
13
import org.gvsig.remoteClient.sld.SLDProtocolHandler;
14
import org.gvsig.remoteClient.sld.SLDProtocolHandlerFactory;
15
import org.gvsig.remoteClient.sld.SLDUtils;
16
import org.gvsig.remoteClient.sld.UnsupportedSLDVersionException;
17
import org.gvsig.remoteClient.sld.layers.ISLDLayer;
18
import org.gvsig.remoteClient.sld.symbolizers.ISLDSymbolizer;
19
import org.gvsig.symbology.fmap.drivers.sld.FMapSLDDriver;
20
import org.gvsig.symbology.fmap.symbols.MarkerFillSymbol;
21
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol;
22
import org.xmlpull.v1.XmlPullParserException;
23

    
24
import com.iver.cit.gvsig.fmap.core.FShape;
25
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
26
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
27
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
28
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
29
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
30
import com.iver.cit.gvsig.fmap.drivers.legend.LegendDriverException;
31

    
32

    
33
public class TestParsePolygonSymbolizer extends TestCase {
34
        SLDProtocolHandler my_sld = null;        
35
        File f,f2,f3,f4;
36

    
37
        public void setUp() {
38
                f = new File("src-test/test-data/polygons/polygonSymbolizer.sld" );
39
                f2 = new File("src-test/test-data/polygons/polygonSymbolizerWithoutOgcPrefix.sld" );
40
                f3 = new File("src-test/test-data/polygons/SimpleFill.sld" );
41
                f4 = new File("src-test/test-data/polygons/PictureFill.sld" );
42
        }
43
        /**
44
         * Tests the parsing of an sld document where is included the prefix ogc: inside 
45
         * the filter expressions 
46
         */
47
        public void testParsing() {
48
                long t1 = System.nanoTime();
49
                try {
50
                        my_sld = SLDProtocolHandlerFactory.createVersionedProtocolHandler(f);                
51
                        ((SLDProtocolHandler) my_sld).parse(f);
52

    
53
                } catch (LegendDriverException e) {
54
                        e.printStackTrace();
55
                        fail();
56
                } catch (IOException e) {
57
                        e.printStackTrace();
58
                        fail();
59
                } catch (XmlPullParserException e) {
60
                        e.printStackTrace();
61
                        fail();
62
                } catch (UnsupportedSLDVersionException e) {
63
                        e.printStackTrace();
64
                        fail();
65
                }
66
                long t2 = System.nanoTime();
67
                System.out.println("Test done with apparently no errors in "+ (t2-(float)t1)/1000+" nano-seconds");
68
        }
69

    
70
        /**
71
         * Tests the parsing of an sld document where is not included the prefix ogc: inside 
72
         * the filter expressions 
73
         */
74
        public void testParsingWithoutOgcPrefix() {
75
                long t1 = System.nanoTime();
76
                try {
77
                        my_sld = SLDProtocolHandlerFactory.createVersionedProtocolHandler(f2);                
78
                        ((SLDProtocolHandler) my_sld).parse(f2);
79

    
80
                } catch (LegendDriverException e) {
81
                        e.printStackTrace();
82
                        fail();
83
                } catch (IOException e) {
84
                        e.printStackTrace();
85
                        fail();
86
                } catch (XmlPullParserException e) {
87
                        e.printStackTrace();
88
                        fail();
89
                } catch (UnsupportedSLDVersionException e) {
90
                        e.printStackTrace();
91
                        fail();
92
                }
93
                long t2 = System.nanoTime();
94
                System.out.println("Test done with apparently no errors in "+ (t2-(float)t1)/1000+" nano-seconds");
95
        }
96

    
97
        /**
98
         * Tests the parsing of an sld document and checks that the symbol that
99
         * is specified inside the sld is the same symbol that is created after that.
100
         */
101
        public void testParsingAndCreation() {
102

    
103
                long t1 = System.nanoTime();
104
                try {
105
                        my_sld = SLDProtocolHandlerFactory.createVersionedProtocolHandler(f3);                
106
                        ((SLDProtocolHandler) my_sld).parse(f3);
107

    
108
                } catch (LegendDriverException e) {
109
                        e.printStackTrace();
110
                        fail();
111
                } catch (IOException e) {
112
                        e.printStackTrace();
113
                        fail();
114
                } catch (XmlPullParserException e) {
115
                        e.printStackTrace();
116
                        fail();
117
                } catch (UnsupportedSLDVersionException e) {
118
                        e.printStackTrace();
119
                        fail();
120
                }
121

    
122

    
123
                ArrayList<ISLDLayer> sldLayers = new ArrayList<ISLDLayer>();
124
                sldLayers = my_sld.getLayers();
125
                ArrayList<ISLDSymbolizer> symbolizers;
126
                symbolizers = sldLayers.get(0).getSymbolizersByShapeType(FShape.POLYGON);
127
                IMultiLayerSymbol multiLayerSymbol = SymbologyFactory.createEmptyMultiLayerSymbol(FShape.POLYGON);
128

    
129
                if (multiLayerSymbol != null) {
130
                        for (int i = 0; i < symbolizers.size(); i++) {
131
                                ISymbol sym = null;
132
                                try {
133
                                        sym = FMapSLDDriver.SLDSymbolizer2ISymbol(symbolizers.get(i));
134
                                } catch (NumberFormatException e) {
135
                                        e.printStackTrace();
136
                                        fail();
137
                                } catch (LegendDriverException e) {
138
                                        e.printStackTrace();
139
                                        fail();
140
                                }
141
                                multiLayerSymbol.addLayer(sym);
142
                        }
143
                        ISymbol defaultSym = (multiLayerSymbol.getLayerCount() > 1) ?
144
                                        multiLayerSymbol :
145
                                                multiLayerSymbol.getLayer(0);
146

    
147
                        SimpleFillSymbol simpleFill =  (SimpleFillSymbol)defaultSym;
148
                        SimpleLineSymbol simpleLine = (SimpleLineSymbol) simpleFill.getOutline();
149

    
150
                        try {
151
                                if (!(simpleFill.getFillColor().equals(SLDUtils.convertHexStringToColor("#FFFFFF"))))
152
                                        fail();
153
                                else if (! (simpleFill.getFillColor().getAlpha() == 255))
154
                                        fail();
155
                                else if (! (simpleLine.getLineWidth() == 2))
156
                                        fail();
157
                                else if (! (simpleLine.getColor().equals(SLDUtils.convertHexStringToColor("#000000"))))
158
                                        fail();
159
                                else if (SLDUtils.convertLineCapToString(((BasicStroke)simpleLine.getLineStyle().getStroke()).getEndCap()).compareTo("butt") !=0 )
160
                                        fail();
161
                                else if (SLDUtils.convertLineJoinToString(((BasicStroke)simpleLine.getLineStyle().getStroke()).getLineJoin()).compareTo("miter") !=0 )
162
                                        fail();
163
                        } catch (NumberFormatException e) {
164
                                e.printStackTrace();
165
                                fail();
166
                        } catch (LegendDriverException e) {
167
                                e.printStackTrace();
168
                                fail();
169
                        }
170
                }
171

    
172
                long t2 = System.nanoTime();
173
                System.out.println("Test done with apparently no errors in "+ (t2-(float)t1)/1000+" nano-seconds");
174

    
175
        }
176

    
177
        public void testParsingAndCreation2() {
178

    
179
                long t1 = System.nanoTime();
180
                try {
181
                        my_sld = SLDProtocolHandlerFactory.createVersionedProtocolHandler(f4);                
182
                        ((SLDProtocolHandler) my_sld).parse(f4);
183

    
184
                } catch (LegendDriverException e) {
185
                        e.printStackTrace();
186
                        fail();
187
                } catch (IOException e) {
188
                        e.printStackTrace();
189
                        fail();
190
                } catch (XmlPullParserException e) {
191
                        e.printStackTrace();
192
                        fail();
193
                } catch (UnsupportedSLDVersionException e) {
194
                        e.printStackTrace();
195
                        fail();
196
                }
197

    
198

    
199
                ArrayList<ISLDLayer> sldLayers = new ArrayList<ISLDLayer>();
200
                sldLayers = my_sld.getLayers();
201
                ArrayList<ISLDSymbolizer> symbolizers;
202
                symbolizers = sldLayers.get(0).getSymbolizersByShapeType(FShape.POLYGON);
203
                IMultiLayerSymbol multiLayerSymbol = SymbologyFactory.createEmptyMultiLayerSymbol(FShape.POLYGON);
204

    
205
                if (multiLayerSymbol != null) {
206
                        for (int i = 0; i < symbolizers.size(); i++) {
207
                                ISymbol sym = null;
208
                                try {
209
                                        sym = FMapSLDDriver.SLDSymbolizer2ISymbol(symbolizers.get(i));
210
                                } catch (NumberFormatException e) {
211
                                        e.printStackTrace();
212
                                        fail();
213
                                } catch (LegendDriverException e) {
214
                                        e.printStackTrace();
215
                                        fail();
216
                                }
217
                                multiLayerSymbol.addLayer(sym);
218
                        }
219
                        ISymbol defaultSym = (multiLayerSymbol.getLayerCount() > 1) ?
220
                                        multiLayerSymbol :
221
                                                multiLayerSymbol.getLayer(0);
222

    
223
                        MarkerFillSymbol markerFill =  (MarkerFillSymbol)defaultSym;
224
                        SimpleLineSymbol simpleLine = (SimpleLineSymbol) markerFill.getOutline();
225
                        URL myURL = null;
226
                        try {
227
                                myURL = new URL("http://maps.massgis.state.ma.us/images/question_mark.gif");
228
                        } catch (MalformedURLException e) {
229
                                e.printStackTrace();
230
                                fail();
231
                        }
232

    
233

    
234
                        Color outlineColor = null;
235
                        try {
236
                                outlineColor = SLDUtils.convertHexStringToColor("#000000");
237
                        } catch (NumberFormatException e) {
238
                                e.printStackTrace();
239
                        } catch (LegendDriverException e) {
240
                                e.printStackTrace();
241
                                fail();
242
                        }
243
                        outlineColor = new Color(outlineColor.getRed(),outlineColor.getGreen(),outlineColor.getBlue(),(int)(0.2*255));
244

    
245
                        if (!(markerFill.getMarker().getRotation() == 0.5))
246
                                fail();
247
                        else if (!(markerFill.getMarker().getSize() == 30))
248
                                fail();
249
                        else if ((((PictureMarkerSymbol) markerFill.getMarker()).getImagePath().compareTo(myURL.toString()))!= 0)
250
                                fail();
251
                        else if (! (simpleLine.getLineWidth() == 1))
252
                                fail();
253
                        else if (!(simpleLine.getColor().equals(outlineColor)))
254
                                fail();
255
                        else if (!(simpleLine.getColor().getAlpha() == (0.2*255)))
256
                                fail();
257
                        else if (SLDUtils.convertLineCapToString(((BasicStroke)simpleLine.getLineStyle().getStroke()).getEndCap()).compareTo("butt") !=0 )
258
                                fail();
259
                        else if (SLDUtils.convertLineJoinToString(((BasicStroke)simpleLine.getLineStyle().getStroke()).getLineJoin()).compareTo("miter") !=0 )
260
                                fail();
261
                }
262

    
263
                long t2 = System.nanoTime();
264
                System.out.println("Test done with apparently no errors in "+ (t2-(float)t1)/1000+" nano-seconds");
265

    
266
        }
267
}
268