Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src-test / com / iver / cit / gvsig / gui / styling / TestSymbolLibrary.java @ 22072

History | View | Annotate | Download (5.99 KB)

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 IVER   {{Task}}
26
*/
27
package com.iver.cit.gvsig.gui.styling;
28

    
29
import java.io.File;
30
import java.io.FileNotFoundException;
31

    
32
import javax.swing.tree.DefaultMutableTreeNode;
33

    
34
import junit.framework.TestCase;
35

    
36
import org.exolab.castor.xml.MarshalException;
37
import org.exolab.castor.xml.ValidationException;
38

    
39
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
40
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
41
import com.iver.utiles.XMLEntity;
42
import com.iver.utiles.xml.XMLEncodingUtils;
43
import com.iver.utiles.xmlEntity.generate.XmlTag;
44

    
45
/**
46
 * Test to check the correct operation of the symbol library. Adds a created symbol
47
 * and recovers it using a name. At the end the folder and the symbol will be eliminated
48
 *
49
 * @author Eustaquio Vercher G?mez
50
 */
51
public class TestSymbolLibrary extends TestCase {
52
        private String folderTestName = "_____TestFolder_____/";
53
        private String test_fileName = "____testsym____"+System.currentTimeMillis()+ SymbolLibrary.SYMBOL_FILE_EXTENSION;
54
        private SymbolLibrary lib = SymbolLibrary.getInstance();
55
        private File testFolder =  new File(SymbologyFactory.SymbolLibraryPath + File.separator + folderTestName);
56
        
57
        @Override
58
        protected void setUp() throws Exception {
59
                super.setUp();
60
                // this only prepares the test, it is not the test itself
61
                File f = new File(SymbologyFactory.SymbolLibraryPath + File.separator + folderTestName);
62
                if (f.exists()) {
63
                        deleteRecursively(f);
64
                }
65

    
66
        };
67

    
68
        @Override
69
        public void tearDown(){
70
                // this only prepares the test, it is not the test itself
71
                File f = new File(SymbologyFactory.SymbolLibraryPath + File.separator + folderTestName);
72
                if (f.exists()) {
73
                        deleteRecursively(f);
74
                }
75
        }
76
        
77
        private void deleteRecursively(File f) {
78
                if (f.isDirectory()) {
79
                        for (int i = f.list().length-1; i >= 0; i--) {
80
                                deleteRecursively(new File(f.getAbsolutePath()+File.separator+f.list()[i]));
81
                        }
82
                }
83
                f.delete();
84
        }
85

    
86
        /**
87
         * Tests the correct operation of the library
88
         */
89
        public void testAddFolder(){
90
                
91
                int childs = lib.getChildCount(lib.getRoot());
92

    
93
                //adding one folder
94
                lib.addFolder(lib.getRoot(), folderTestName);
95
                int childsExpected = lib.getChildCount(lib.getRoot());
96

    
97
                assertEquals("Folder does not have been added to Library",childs+1,childsExpected);
98
        }
99
        
100
        
101

    
102
        public void testAddSymbol() {
103
                testAddFolder();
104
                
105
                //add symbol to the created folder
106
                ILineSymbol lineIn = getTestLineSymbol();
107
                lib.addElement(lineIn, test_fileName, testFolder);
108

    
109
                DefaultMutableTreeNode lineAux = (DefaultMutableTreeNode) lib.getElement(
110
                                testFolder, test_fileName);
111
                
112
                File obj = (File)lineAux.getUserObject();
113

    
114
                assertTrue("File does not exist", obj.exists());
115
        }
116
        
117
        private ILineSymbol getTestLineSymbol() {
118
                ILineSymbol lineIn = (ILineSymbol) SymbologyFactory.createDefaultLineSymbol();
119
                lineIn.setLineWidth(328975); // a non usual value for the test (avoid false positives)
120
                lineIn.setDescription("TestingSymbolLibrary");
121
                return lineIn;
122
        }
123
        
124

    
125
        public void testGetSymbol() {
126
                testAddSymbol();
127
                DefaultMutableTreeNode lineAux = (DefaultMutableTreeNode) lib.getElement(testFolder, test_fileName);
128
                File obj = (File)lineAux.getUserObject();
129

    
130
                ILineSymbol lineIn = getTestLineSymbol();
131
                XMLEntity xml = null;
132
                try {
133
                        xml = new XMLEntity((XmlTag) XmlTag.unmarshal(XMLEncodingUtils.getReader(obj)));
134
                } catch (MarshalException e) {
135
                        e.printStackTrace();
136
                } catch (ValidationException e) {
137
                        e.printStackTrace();
138
                } catch (FileNotFoundException e) {
139
                        e.printStackTrace();
140
                }
141

    
142
                ILineSymbol lineOut = (ILineSymbol) SymbologyFactory.createSymbolFromXML(xml, obj.getName());
143

    
144
                assertTrue("Symbol in library is different to created symbol",
145
                                lineIn.getLineWidth() == lineOut.getLineWidth() &&
146
                                lineIn.getDescription().equals(lineIn.getDescription()) &&
147
                                lineIn.getClassName().equals(lineOut.getClassName()));
148
        }
149
        
150
        public void testRemoveElement() {
151
                testAddSymbol(); // <- to previously create the test symbol
152
                
153
                assertTrue("removing something that does not existr", 
154
                                lib.getElement(testFolder, test_fileName) != null);
155
                //delete created element
156
                lib.removeElement(test_fileName, testFolder);
157
                assertFalse("library didn't delete the symbol", 
158
                                lib.getElement(testFolder, test_fileName) != null);
159
                
160
                // TODO faltar?a ver si lo que se ha eliminado es lo que esperamos!
161
        }
162

    
163
        public void testRemoveFolder() {
164
                testAddSymbol(); // <- to previously create the test symbol
165
                /*
166
                 * For the childs, childsExpected, lineAux and obj variables to contain
167
                 * right values, it is necessary that the tests testAddFolder, testAddSymbol and testGetSymbol
168
                 * do not fail before to run this one. If one of the previous test has failed,
169
                 * the result of this test will not be correct.
170
                 */
171
                int childsBefore = lib.getChildCount(lib.getRoot());
172
                
173
                //delete folder
174
                File f = new File(SymbologyFactory.SymbolLibraryPath);
175
                lib.removeElement(folderTestName,f);
176
                int childsAfter = lib.getChildCount(lib.getRoot());
177

    
178
                assertEquals("Folder is not deleted", childsBefore, childsAfter+1);
179

    
180
        }
181
        
182

    
183
}