Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / test / java / org / gvsig / installer / lib / impl / InstallerServiceTest.java @ 40560

History | View | Annotate | Download (3.97 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2010 {Prodevelop}   {Task}
27
 */
28

    
29
package org.gvsig.installer.lib.impl;
30

    
31
import java.io.BufferedOutputStream;
32
import java.io.DataOutputStream;
33
import java.io.File;
34
import java.io.FileInputStream;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
37
import java.io.InputStream;
38
import java.io.OutputStream;
39

    
40
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
41

    
42
/**
43
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
44
 */
45
public abstract class InstallerServiceTest extends
46
                AbstractLibraryAutoInitTestCase {
47

    
48
        public File getApplicationDirectory() throws IOException {
49
                File templateFile = new File(getClass().getClassLoader().getResource(
50
                                "application").getFile());
51
                File applicationDirectory = new File(System
52
                                .getProperty("java.io.tmpdir")
53
                                + File.separator + "tmp_gvsig_installer");
54

    
55
                if (applicationDirectory.exists()) {
56
                        deleteDir(applicationDirectory);
57
                }
58
                copy(templateFile, applicationDirectory);
59

    
60
                return applicationDirectory;
61
        }
62

    
63
        public File getTemporalFile() {
64
                return new File(System.getProperty("java.io.tmpdir") + File.separator
65
                                + "installer" + Math.random());
66
        }
67

    
68
        public File getInstallersDirectory() throws IOException {
69
                File applicationDirectory = getApplicationDirectory();
70
                return new File(applicationDirectory + File.separator + "install");
71
        }
72

    
73
        public File getPluginsDirectory() throws IOException {
74
                File applicationDirectory = getApplicationDirectory();
75
                return new File(applicationDirectory + File.separator + "gvSIG"
76
                                + File.separator + "extensiones");
77
        }
78

    
79
        public boolean deleteDir(File dir) {
80
                if (dir.isDirectory()) {
81
                        String[] children = dir.list();
82
                        for (int i = 0; i < children.length; i++) {
83
                                boolean success = deleteDir(new File(dir, children[i]));
84
                                if (!success) {
85
                                        return false;
86
                                }
87
                        }
88
                }
89
                return dir.delete();
90
        }
91

    
92
        public void copy(File sourceLocation, File targetLocation)
93
                        throws IOException {
94
                if (sourceLocation.isDirectory()) {
95
                        if (!targetLocation.exists()) {
96
                                targetLocation.mkdir();
97
                        }
98

    
99
                        String[] children = sourceLocation.list();
100
                        for (int i = 0; i < children.length; i++) {
101
                                copy(new File(sourceLocation, children[i]), new File(
102
                                                targetLocation, children[i]));
103
                        }
104
                } else {
105
                        targetLocation.getParentFile().mkdirs();
106

    
107
                        InputStream in = new FileInputStream(sourceLocation);
108
                        OutputStream out = new FileOutputStream(targetLocation);
109

    
110
                        // Copy the bits from instream to outstream
111
                        byte[] buf = new byte[1024];
112
                        int len;
113
                        while ((len = in.read(buf)) > 0) {
114
                                out.write(buf, 0, len);
115
                        }
116
                        in.close();
117
                        out.close();
118
                }
119
        }
120

    
121
        public void writeToFile(InputStream is, File file) {
122
                try {
123
                        DataOutputStream out = new DataOutputStream(
124
                                        new BufferedOutputStream(new FileOutputStream(file)));
125
                        InputStream is2 = is;
126
                        boolean again = true;
127
                        while (again) {
128
                                if (is2.read() > -1) {
129
                                        out.writeByte(is.read());
130
                                } else {
131
                                        again = false;
132
                                }
133
                        }
134
                        is.close();
135
                        out.close();
136
                } catch (IOException e) {
137
                        System.err.println("Error Writing/Reading Streams.");
138
                }
139
        }
140

    
141
}