svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.dbf / src / test / java / org / gvsig / fmap / dal / explorer / filesystem / FileSystemExplorerTest.java @ 40435
History | View | Annotate | Download (5.15 KB)
1 | 40435 | jjdelcerro | /* 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 T.I. S.A. {{Task}}
|
||
26 | */
|
||
27 | |||
28 | package org.gvsig.fmap.dal.explorer.filesystem; |
||
29 | |||
30 | import java.io.File; |
||
31 | import java.util.Iterator; |
||
32 | import java.util.List; |
||
33 | |||
34 | import org.gvsig.fmap.dal.DALLocator; |
||
35 | import org.gvsig.fmap.dal.DataManager; |
||
36 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
37 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
38 | import org.gvsig.fmap.dal.DataTypes; |
||
39 | import org.gvsig.fmap.dal.NewDataStoreParameters; |
||
40 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
41 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
42 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
43 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
44 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
45 | import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
||
46 | import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider; |
||
47 | import org.gvsig.fmap.dal.store.dbf.TestDBF; |
||
48 | import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
||
49 | |||
50 | public class FileSystemExplorerTest extends AbstractLibraryAutoInitTestCase { |
||
51 | |||
52 | private static DataManager dataManager = null; |
||
53 | public static final File dbf_data = new File(TestDBF.class.getResource( |
||
54 | "data/prueba.dbf").getFile()).getParentFile();
|
||
55 | |||
56 | protected void doSetUp() throws Exception { |
||
57 | dataManager = DALLocator.getDataManager(); |
||
58 | } |
||
59 | |||
60 | public void testList() throws Exception { |
||
61 | FilesystemServerExplorerParameters exParam = (FilesystemServerExplorerParameters) dataManager |
||
62 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
63 | |||
64 | exParam.setRoot(dbf_data.getPath()); |
||
65 | |||
66 | DataServerExplorer ex = dataManager.createServerExplorer(exParam); |
||
67 | |||
68 | List list = ex.list();
|
||
69 | |||
70 | assertEquals(2, list.size());
|
||
71 | |||
72 | if (((FilesystemStoreParameters) list.get(0)).getFile().getPath() |
||
73 | .equals(TestDBF.dbf_prueba.getPath())) { |
||
74 | assertEquals(((FilesystemStoreParameters) list.get(1)).getFile()
|
||
75 | .getPath(), TestDBF.dbf_pruebaNull.getPath()); |
||
76 | |||
77 | } else {
|
||
78 | assertEquals(((FilesystemStoreParameters) list.get(0)).getFile()
|
||
79 | .getPath(), TestDBF.dbf_pruebaNull.getPath()); |
||
80 | assertEquals(((FilesystemStoreParameters) list.get(1)).getFile()
|
||
81 | .getPath(), TestDBF.dbf_prueba.getPath()); |
||
82 | } |
||
83 | |||
84 | Iterator iter = list.iterator();
|
||
85 | TestDBF testDBF = new TestDBF();
|
||
86 | while (iter.hasNext()) {
|
||
87 | testDBF.testSimpleIteration((DBFStoreParameters) iter.next()); |
||
88 | } |
||
89 | |||
90 | |||
91 | |||
92 | } |
||
93 | |||
94 | private boolean deleteDir(File dir) { |
||
95 | File[] files = dir.listFiles(); |
||
96 | for (int i = 0; i < files.length; i++) { |
||
97 | if (files[i].isDirectory()) {
|
||
98 | if (!deleteDir(files[i])) {
|
||
99 | return false; |
||
100 | } |
||
101 | } |
||
102 | if (!files[i].delete()) {
|
||
103 | return false; |
||
104 | } |
||
105 | } |
||
106 | return dir.delete();
|
||
107 | } |
||
108 | |||
109 | public void testAddRemove() throws Exception { |
||
110 | File tmpDir = new File("target/_FilesystemExplorer.tmp"); |
||
111 | |||
112 | if (tmpDir.exists()) {
|
||
113 | if (!deleteDir(tmpDir)) {
|
||
114 | fail("Clean tmp dir:" + tmpDir.getAbsolutePath());
|
||
115 | } |
||
116 | } |
||
117 | |||
118 | tmpDir.mkdir(); |
||
119 | |||
120 | FilesystemServerExplorerParameters exParam = (FilesystemServerExplorerParameters) dataManager |
||
121 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
122 | |||
123 | exParam.setRoot(tmpDir.getAbsolutePath()); |
||
124 | |||
125 | DataServerExplorer ex = dataManager.createServerExplorer(exParam); |
||
126 | |||
127 | assertTrue(ex.canAdd()); |
||
128 | |||
129 | assertTrue(ex.canAdd(DBFStoreProvider.NAME)); |
||
130 | |||
131 | assertEquals(0, ex.list().size());
|
||
132 | |||
133 | NewDataStoreParameters newParams = ex |
||
134 | .getAddParameters(DBFStoreProvider.NAME); |
||
135 | |||
136 | ((FilesystemStoreParameters) newParams).setFile(new File(tmpDir, |
||
137 | "tmp1.dbf"));
|
||
138 | EditableFeatureType fType; |
||
139 | if (((NewFeatureStoreParameters) newParams).getDefaultFeatureType() instanceof EditableFeatureType) { |
||
140 | fType = (EditableFeatureType) ((NewFeatureStoreParameters) newParams) |
||
141 | .getDefaultFeatureType(); |
||
142 | } else {
|
||
143 | fType = ((NewFeatureStoreParameters) newParams) |
||
144 | .getDefaultFeatureType().getEditable(); |
||
145 | } |
||
146 | fType.add("campo1", DataTypes.STRING).setSize(10); |
||
147 | ((NewFeatureStoreParameters) newParams).setDefaultFeatureType(fType); |
||
148 | ex.add(DBFStoreProvider.NAME, newParams, false);
|
||
149 | |||
150 | List list = ex.list();
|
||
151 | assertEquals(1, list.size());
|
||
152 | |||
153 | Iterator iter = list.iterator();
|
||
154 | TestDBF testDBF = new TestDBF();
|
||
155 | while (iter.hasNext()) {
|
||
156 | testDBF.testSimpleIteration((DBFStoreParameters) iter.next()); |
||
157 | } |
||
158 | |||
159 | ex.remove((DataStoreParameters) list.get(0));
|
||
160 | |||
161 | assertEquals(0, ex.list().size());
|
||
162 | |||
163 | |||
164 | } |
||
165 | } |