Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.api / src / test / java / org / gvsig / raster / cache / tile / TestThreadPool.java @ 990

History | View | Annotate | Download (3.17 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
package org.gvsig.raster.cache.tile;
23

    
24
import java.io.File;
25

    
26
import org.gvsig.raster.cache.tile.exception.TileGettingException;
27
import org.gvsig.raster.cache.tile.pool.ThreadPool;
28
import org.gvsig.raster.cache.tile.provider.Downloader;
29
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
30

    
31
/**
32
 * 
33
 * @author Nacho Brodin (nachobrodin@gmail.com)
34
 */
35
public class TestThreadPool extends AbstractLibraryAutoInitTestCase {
36
        private static final int  WIDTH           = 3;
37
        private static final int  HEIGHT          = 3;
38
        private static final int  DOWNLOAD_TIME   = 500;
39
        
40
        public class MyDownloader implements Downloader {
41

    
42
                public Tile downloadTile(Tile tile) {
43
                        //tile.getFile() -> Aqui se guarda el fichero descargado
44
                        try {
45
                                Thread.sleep(DOWNLOAD_TIME);
46
                        } catch (InterruptedException e) {
47
                                e.printStackTrace();
48
                        }
49
                        return tile;
50
                }
51

    
52
                public Tile readTileFromDisk(Tile tile) throws TileGettingException {
53
                        // TODO Auto-generated method stub
54
                        return null;
55
                }
56
                
57
                /*
58
                 * (non-Javadoc)
59
                 * @see org.gvsig.raster.cache.tile.provider.Downloader#cancelDownloading(boolean)
60
                 */
61
                public void cancelDownloading(boolean cancel) {
62
                        
63
                }
64

    
65
                public Object[] getCrashImage(int w, int h) {
66
                        // TODO Auto-generated method stub
67
                        return null;
68
                }
69

    
70
                public Object[] getDownloadingImage(int w, int h) {
71
                        // TODO Auto-generated method stub
72
                        return null;
73
                }
74
                
75
        }
76
        
77
        public void start() {
78
                try {
79
                        this.doSetUp();
80
                } catch (Exception e) {
81
                        e.printStackTrace();
82
                }
83
                this.testStack();
84
        }
85
        
86
        @Override
87
        protected void doSetUp() throws Exception {
88
                System.err.println("TestThreadPool running...");
89
        }
90
        
91
        public void testStack() {
92
                ThreadPool pool = TileCacheLocator.getManager().createThreadPool(false);
93
                MyDownloader d = new MyDownloader();
94
                
95
                for (int x = 0; x < HEIGHT; x++) {
96
                        for (int y = 0; y < WIDTH; y++) {
97
                                Tile t = TileCacheLocator.getManager().createTile(0, x, y);
98
                                t.setSharedPipe(pool.getTilePipe());
99
                                t.setDownloader(d);
100
                                t.setFile(new File(""));
101
                                pool.addTask(t);
102
                        }
103
                }
104
                
105
                int nCollected = 0;
106
                while (nCollected < (WIDTH * HEIGHT)) {
107
                        Tile tile = (Tile)pool.getTilePipe().getAtomicTask();
108
                        System.out.println("Getting " + tile.getRow() + " " + tile.getCol() + " P:" + tile.getPriority());
109
                        nCollected ++;
110
                }
111
                if(pool != null)
112
                        pool.destroyPool();
113
        }
114

    
115
}