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