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.repository;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileNotFoundException;
28  import java.io.FileReader;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import junit.framework.TestCase;
34  
35  import org.junit.Test;
36  
37  import com.gisgraphy.domain.geoloc.importer.ImporterConfig;
38  import com.gisgraphy.domain.valueobject.ImporterStatus;
39  import com.gisgraphy.domain.valueobject.ImporterStatusDto;
40  import com.gisgraphy.helper.FileHelper;
41  import com.gisgraphy.test.GeolocTestHelper;
42  
43  public class ImporterStatusListDaoTest extends TestCase {
44  
45      private static final String STATUS_MESSAGE = "message";
46  
47      private static final int TOTAL_READ_LINE = 3;
48  
49      private static final int TOTAL_LINE_TO_PROCESS = 10;
50  
51      private static final String CURRENT_FILE_NAME = "currentFileName";
52  
53      private static final String PROCESSOR_NAME = "processorName";
54  
55      private static final String PROCESSOR_NAME_2 = "processorName2";
56  
57      private static final int CURRENT_LINE = 2;
58  
59      @Test
60      public void testSave() {
61  	ImporterStatusListDao importerstatusDao = new ImporterStatusListDao();
62  	// create a temporary directory to download files
63  	File tempDir = FileHelper.createTempDir(this.getClass()
64  		.getSimpleName());
65  	ImporterConfig importerConfig = new ImporterConfig();
66  	importerConfig.setGeonamesDir(tempDir.getAbsolutePath());
67  	importerstatusDao.setImporterConfig(importerConfig);
68  	ImporterStatusDto importerStatus = new ImporterStatusDto(
69  		PROCESSOR_NAME, CURRENT_FILE_NAME, CURRENT_LINE,
70  		TOTAL_LINE_TO_PROCESS, TOTAL_READ_LINE, STATUS_MESSAGE,
71  		ImporterStatus.PROCESSING);
72  
73  	ImporterStatusDto importerStatus2 = new ImporterStatusDto(
74  		PROCESSOR_NAME_2, CURRENT_FILE_NAME, CURRENT_LINE,
75  		TOTAL_LINE_TO_PROCESS, TOTAL_READ_LINE, STATUS_MESSAGE,
76  		ImporterStatus.PROCESSING);
77  	List<ImporterStatusDto> importerStatusDtoList = new ArrayList<ImporterStatusDto>();
78  	importerStatusDtoList.add(importerStatus);
79  	importerStatusDtoList.add(importerStatus2);
80  	importerstatusDao.saveOrUpdate(importerStatusDtoList);
81  	assertTrue(new File(importerstatusDao.getSavedFilePath()).exists());
82  	FileReader fileReader = null;
83  	;
84  	try {
85  	    fileReader = new FileReader(new File(importerstatusDao
86  		    .getSavedFilePath()));
87  	} catch (FileNotFoundException e) {
88  	    fail(e.getMessage());
89  	}
90  	int count = 0;
91  	BufferedReader bufferReader = null;
92  	try {
93  	    bufferReader = new BufferedReader(fileReader);
94  	    String line = bufferReader.readLine();
95  	    while (line != null) {
96  		count++;
97  		line = bufferReader.readLine();
98  	    }
99  	} catch (IOException e) {
100 	    fail(e.getMessage());
101 	} finally {
102 	    if (bufferReader != null) {
103 		try {
104 		    bufferReader.close();
105 		} catch (IOException e) {
106 		    fail(e.getMessage());
107 		}
108 	    }
109 
110 	}
111 	assertEquals(2, count);
112 	GeolocTestHelper.DeleteNonEmptyDirectory(tempDir);
113     }
114 
115     @Test
116     public void testGet() {
117 	ImporterStatusListDao importerstatusDao = new ImporterStatusListDao();
118 	// create a temporary directory to download files
119 	File tempDir = FileHelper.createTempDir(this.getClass()
120 		.getSimpleName());
121 	ImporterConfig importerConfig = new ImporterConfig();
122 	importerConfig.setGeonamesDir(tempDir.getAbsolutePath());
123 	importerstatusDao.setImporterConfig(importerConfig);
124 	ImporterStatusDto importerStatus = new ImporterStatusDto(
125 		PROCESSOR_NAME, CURRENT_FILE_NAME, CURRENT_LINE,
126 		TOTAL_LINE_TO_PROCESS, TOTAL_READ_LINE, STATUS_MESSAGE,
127 		ImporterStatus.PROCESSING);
128 	List<ImporterStatusDto> importerStatusDtoList = new ArrayList<ImporterStatusDto>();
129 	importerStatusDtoList.add(importerStatus);
130 	importerstatusDao.saveOrUpdate(importerStatusDtoList);
131 	List<ImporterStatusDto> importerStatusDtoListExpected = importerstatusDao
132 		.get();
133 	assertEquals(1, importerStatusDtoListExpected.size());
134 	assertEquals(importerStatus.toCSV(), importerStatusDtoListExpected.get(
135 		0).toCSV());
136 	GeolocTestHelper.DeleteNonEmptyDirectory(tempDir);
137     }
138 
139     @Test
140     public void testDelete() {
141 	ImporterStatusListDao importerstatusDao = new ImporterStatusListDao();
142 	// create a temporary directory to download files
143 	File tempDir = FileHelper.createTempDir(this.getClass()
144 		.getSimpleName());
145 	ImporterConfig importerConfig = new ImporterConfig();
146 	importerConfig.setGeonamesDir(tempDir.getAbsolutePath());
147 	importerstatusDao.setImporterConfig(importerConfig);
148 	ImporterStatusDto importerStatus = new ImporterStatusDto(
149 		PROCESSOR_NAME, CURRENT_FILE_NAME, CURRENT_LINE,
150 		TOTAL_LINE_TO_PROCESS, TOTAL_READ_LINE, STATUS_MESSAGE,
151 		ImporterStatus.PROCESSING);
152 	List<ImporterStatusDto> importerStatusDtoList = new ArrayList<ImporterStatusDto>();
153 	importerStatusDtoList.add(importerStatus);
154 	importerstatusDao.saveOrUpdate(importerStatusDtoList);
155 	assertTrue("the delete method shoul return true for success",
156 		importerstatusDao.delete());
157 	List<ImporterStatusDto> importerStatusDtoListExpected = importerstatusDao
158 		.get();
159 	assertEquals("after deletion, no impoortersatusListShouldBe stored", 0,
160 		importerStatusDtoListExpected.size());
161 	assertTrue(
162 		"the delete method shoul return true even if no list were saved",
163 		importerstatusDao.delete());
164 	GeolocTestHelper.DeleteNonEmptyDirectory(tempDir);
165     }
166 
167 }