root / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / test / java / org / gvsig / installer / lib / impl / InstallerServiceTest.java @ 32413
History | View | Annotate | Download (3.2 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 |
* 2010 {Prodevelop} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.installer.lib.impl; |
29 |
|
30 |
import java.io.BufferedReader; |
31 |
import java.io.File; |
32 |
import java.io.FileInputStream; |
33 |
import java.io.FileOutputStream; |
34 |
import java.io.FileReader; |
35 |
import java.io.IOException; |
36 |
import java.io.InputStream; |
37 |
import java.io.OutputStream; |
38 |
|
39 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
40 |
|
41 |
/**
|
42 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
43 |
*/
|
44 |
public abstract class InstallerServiceTest extends AbstractLibraryAutoInitTestCase{ |
45 |
private boolean isCopied = false; |
46 |
|
47 |
|
48 |
public File getApplicationDirectory() throws IOException{ |
49 |
File templateFile = new File(getClass().getClassLoader().getResource("application").getFile()); |
50 |
File applicationDirectory = new File(System.getProperty("java.io.tmpdir") + File.separator + "tmp_gvsig_installer"); |
51 |
|
52 |
if (!isCopied){
|
53 |
if (applicationDirectory.exists()){
|
54 |
deleteDir(applicationDirectory); |
55 |
} |
56 |
copy(templateFile, applicationDirectory); |
57 |
isCopied = true;
|
58 |
} |
59 |
|
60 |
return applicationDirectory;
|
61 |
} |
62 |
|
63 |
public File getPluginsDirectory() throws IOException{ |
64 |
File applicationDirectory = getApplicationDirectory();
|
65 |
return new File(applicationDirectory + File.separator + "gvSIG" + File.separator + "extensiones"); |
66 |
} |
67 |
|
68 |
public boolean deleteDir(File dir) { |
69 |
if (dir.isDirectory()) {
|
70 |
String[] children = dir.list(); |
71 |
for (int i=0; i<children.length; i++) { |
72 |
boolean success = deleteDir(new File(dir, children[i])); |
73 |
if (!success) {
|
74 |
return false; |
75 |
} |
76 |
} |
77 |
} |
78 |
return dir.delete();
|
79 |
} |
80 |
|
81 |
public void copy(File sourceLocation , File targetLocation) throws IOException { |
82 |
if (sourceLocation.isDirectory()) {
|
83 |
if (!targetLocation.exists()) {
|
84 |
targetLocation.mkdir(); |
85 |
} |
86 |
|
87 |
String[] children = sourceLocation.list(); |
88 |
for (int i=0; i<children.length; i++) { |
89 |
copy(new File(sourceLocation, children[i]), |
90 |
new File(targetLocation, children[i])); |
91 |
} |
92 |
} else {
|
93 |
targetLocation.getParentFile().mkdirs(); |
94 |
|
95 |
InputStream in = new FileInputStream(sourceLocation); |
96 |
OutputStream out = new FileOutputStream(targetLocation); |
97 |
|
98 |
// Copy the bits from instream to outstream
|
99 |
byte[] buf = new byte[1024]; |
100 |
int len;
|
101 |
while ((len = in.read(buf)) > 0) { |
102 |
out.write(buf, 0, len);
|
103 |
} |
104 |
in.close(); |
105 |
out.close(); |
106 |
} |
107 |
} |
108 |
|
109 |
|
110 |
} |
111 |
|