Statistics
| Revision:

svn-gvsig-desktop / 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 @ 32451

History | View | Annotate | Download (3.71 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.BufferedOutputStream;
31
import java.io.DataOutputStream;
32
import java.io.File;
33
import java.io.FileInputStream;
34
import java.io.FileOutputStream;
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&aacute;</a>
43
 */
44
public abstract class InstallerServiceTest extends AbstractLibraryAutoInitTestCase{
45

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

    
50
                if (applicationDirectory.exists()){
51
                        deleteDir(applicationDirectory);
52
                }
53
                copy(templateFile, applicationDirectory);
54
                
55
                return applicationDirectory;
56
        }
57
        
58
        public File getTemporalFile(){
59
                return new File(System.getProperty("java.io.tmpdir") + File.separator + "installer" + Math.random());
60
        }
61

    
62
        public File getPluginsDirectory() throws IOException{
63
                File applicationDirectory = getApplicationDirectory();
64
                return new File(applicationDirectory + File.separator + "gvSIG" + File.separator + "extensiones");
65
        }
66

    
67
        public boolean deleteDir(File dir) { 
68
                if (dir.isDirectory()) {
69
                        String[] children = dir.list();
70
                        for (int i=0; i<children.length; i++) {
71
                                boolean success = deleteDir(new File(dir, children[i]));
72
                                if (!success) { 
73
                                        return false; 
74
                                } 
75
                        }
76
                }
77
                return dir.delete();                 
78
        }
79

    
80
        public void copy(File sourceLocation , File targetLocation) throws IOException { 
81
                if (sourceLocation.isDirectory()) {
82
                        if (!targetLocation.exists()) {
83
                                targetLocation.mkdir();
84
                        }
85

    
86
                        String[] children = sourceLocation.list();
87
                        for (int i=0; i<children.length; i++) {
88
                                copy(new File(sourceLocation, children[i]),
89
                                                new File(targetLocation, children[i]));
90
                        }
91
                } else {
92
                        targetLocation.getParentFile().mkdirs();
93

    
94
                        InputStream in = new FileInputStream(sourceLocation);
95
                        OutputStream out = new FileOutputStream(targetLocation);
96

    
97
                        // Copy the bits from instream to outstream
98
                        byte[] buf = new byte[1024];
99
                        int len;
100
                        while ((len = in.read(buf)) > 0) {
101
                                out.write(buf, 0, len);
102
                        }
103
                        in.close();
104
                        out.close();
105
                }                 
106
        }
107
        
108
        public void writeToFile(InputStream is, File file) {
109
                try {
110
                        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
111
                        InputStream is2 = is;
112
                        boolean again = true;
113
                        while(again) {
114
                                if(is2.read() > -1) {
115
                                        out.writeByte(is.read());
116
                                }
117
                                else again = false;
118
                        }
119
                        is.close();
120
                        out.close();
121
                }
122
                catch(IOException e) {
123
                        System.err.println("Error Writing/Reading Streams.");
124
                }
125
        }
126
        
127
        
128
}
129