1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.domain.geoloc.importer;
24  
25  import static junit.framework.Assert.assertEquals;
26  import static junit.framework.Assert.assertTrue;
27  import static junit.framework.Assert.fail;
28  import static org.junit.Assert.assertFalse;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import junit.framework.Assert;
36  
37  import org.junit.Test;
38  
39  import com.gisgraphy.domain.valueobject.NameValueDTO;
40  import com.gisgraphy.helper.FileHelper;
41  import com.gisgraphy.test.GeolocTestHelper;
42  
43  public class GeonamesFileRetrieverTest {
44  
45      @Test
46      public void rollbackShouldRollback() {
47  	GeonamesFileRetriever geonamesFileRetriever = new GeonamesFileRetriever();
48  	ImporterConfig importerConfig = new ImporterConfig();
49  	File tempDir = FileHelper.createTempDir(this.getClass()
50  		.getSimpleName());
51  	File file = new File(tempDir.getAbsolutePath()
52  		+ System.getProperty("file.separator") + "FR.zip");
53  	try {
54  	    assertTrue(file.createNewFile());
55  	} catch (IOException e) {
56  	    fail("Can not create file " + file.getAbsolutePath());
57  	}
58  
59  	importerConfig.setGeonamesDir(tempDir.getAbsolutePath());
60  	importerConfig.setGeonamesFilesToDownload("FR.zip");
61  	geonamesFileRetriever.setImporterConfig(importerConfig);
62  	List<NameValueDTO<Integer>> list = geonamesFileRetriever.rollback();
63  	assertEquals(1, list.size());
64  	assertFalse("The importable file should have been deleted", file
65  		.exists());
66  
67  	// delete temp dir
68  	assertTrue("The tempDir has not been deleted", GeolocTestHelper
69  		.DeleteNonEmptyDirectory(tempDir));
70  
71      }
72      
73      @Test
74      public void shouldBeSkipShouldReturnCorrectValue(){
75  	ImporterConfig importerConfig = new ImporterConfig();
76  	GeonamesFileRetriever geonamesFileRetriever = new GeonamesFileRetriever();
77  	geonamesFileRetriever.setImporterConfig(importerConfig);
78  	
79  	importerConfig.setGeonamesImporterEnabled(false);
80  	Assert.assertTrue(geonamesFileRetriever.shouldBeSkipped());
81  	
82  	importerConfig.setGeonamesImporterEnabled(true);
83  	Assert.assertFalse(geonamesFileRetriever.shouldBeSkipped());
84  	
85      }
86      
87      @Test
88      public void getFilesToDownloadShouldReturnTheImporterConfigOption(){
89  	ImporterConfig importerConfig = new ImporterConfig();
90  	String fileTobeDownload = "ZW.zip";
91  	List<String> filesToDownload =new ArrayList<String>();
92  	filesToDownload.add(fileTobeDownload);
93  	importerConfig.setGeonamesFilesToDownload(fileTobeDownload);
94  	GeonamesFileRetriever geonamesFileRetriever = new GeonamesFileRetriever();
95  	geonamesFileRetriever.setImporterConfig(importerConfig);
96  	Assert.assertEquals("getFilesToDownload should return the importerConfig Option",filesToDownload, geonamesFileRetriever.getFilesToDownload());
97  	
98  	
99      }
100 
101 }