1   /********************************************************************************
2    *InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
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, MA  02110-1301, USA.
19   *******************************************************************************/
20  
21  package test.base.jdbs;
22  
23  import java.io.File;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.util.Date;
27  import java.util.Vector;
28  
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.transform.TransformerException;
31  import javax.xml.transform.TransformerFactoryConfigurationError;
32  
33  import junit.framework.TestCase;
34  
35  import org.apache.log4j.Logger;
36  
37  import base.jdbs.Backup;
38  import base.jdbs.BackupFactory;
39  import base.jdbs.SecurityLevel;
40  import base.util.FileUtil;
41  
42  public class BackupArtifactAssemblerTest extends TestCase {
43  
44  	private static final transient Logger logger = Logger
45  			.getLogger(BackupArtifactAssemblerTest.class.getName());
46  
47  	private final String BAKUP_NAME = "JUnit-TestBackup";
48  
49  	private final String BAKUP_DESCRIPTION = "This is a test backup created by the BackupArtifactAssemblerTest JUnit Test.";
50  
51  	private final Date BACKUP_EXPIRATION_DATE = new Date(System
52  			.currentTimeMillis()
53  			+ 1000 * 60 * 60 * 24 * 30);
54  
55  	/*
56  	 * Test method for
57  	 * 'base.jdbs.BackupArtifactAssembler.buildNewBackupArtifact(File,
58  	 * SymmetricKey, KeyPair, Backup)'
59  	 */
60  	public void testBuildNewBackupArtifact() throws IOException,
61  			ParserConfigurationException, TransformerFactoryConfigurationError,
62  			TransformerException {
63  		long start = System.currentTimeMillis();
64  		if (AllTests.RUN_SILENTLY) {
65  			// Creating some content for the backup
66  			File directory = new File("BackupTestDirectory");
67  			directory.mkdir();
68  			directory.deleteOnExit();
69  			File[] filesToBeBackupped = new File[50];
70  
71  			for (int i = 0; i < filesToBeBackupped.length; i++) {
72  				filesToBeBackupped[i] = new File(directory, "file" + i + ".txt");
73  				filesToBeBackupped[i].createNewFile();
74  				filesToBeBackupped[i].deleteOnExit();
75  				// Writing some file content...
76  				FileWriter fileWriter = new FileWriter(filesToBeBackupped[i]);
77  				for (int k = 0; k < 50; k++) {
78  					fileWriter.append("Line " + k
79  							+ ": THIS IS A TEST LINE CONTENT!");
80  				}
81  				fileWriter.close();
82  			}
83  
84  			// This is backup assembly test based on PRIVATE backup's content.
85  			Backup backup = BackupFactory.newBackup(BAKUP_NAME,
86  					BAKUP_DESCRIPTION, BACKUP_EXPIRATION_DATE,
87  					SecurityLevel.PRIVATE);
88  			backup.addAllFile(FileUtil.allFileContent(directory));
89  
90  			assertTrue(AllTests.getAsymmetricKeyRing().getAllKeyPair().length == 1);
91  
92  			base.jdbs.BackupArtifactAssembler.buildNewBackupArtifact(AllTests
93  					.getRepository().getLocation(), AllTests.getSymmetricKey(),
94  					AllTests.getAsymmetricKeyRing().getKeyPair(1), backup);
95  
96  		} else {
97  			// This is the interactive TEST. Asks for file selection trough a
98  			// swing UI.
99  			// This is backup assembly test based on PRIVATE backup's content.
100 			Backup backup = BackupFactory.newBackup(BAKUP_NAME,
101 					BAKUP_DESCRIPTION, BACKUP_EXPIRATION_DATE,
102 					SecurityLevel.PRIVATE);
103 			backup.addAllFile(testMultipleFileSelection());
104 			base.jdbs.BackupArtifactAssembler.buildNewBackupArtifact(AllTests
105 					.getRepository().getLocation(), AllTests.getSymmetricKey(),
106 					AllTests.getAsymmetricKeyRing().getKeyPair(1), backup);
107 		}
108 		long end = System.currentTimeMillis();
109 		logger.debug("The backup artifact assemblation has taken: "
110 				+ (end - start) + " ms");
111 	}
112 
113 	private File[] testMultipleFileSelection() {
114 		Vector<File> fileSelection = new Vector<File>();
115 		javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
116 		chooser
117 				.setDialogTitle("Select the files and the folders that must be backupped...");
118 		chooser
119 				.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
120 		chooser.setApproveButtonText("Select");
121 		chooser.setMultiSelectionEnabled(true);
122 
123 		if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) {
124 			File[] tempFileSelection = chooser.getSelectedFiles();
125 			for (int i = 0; i < tempFileSelection.length; i++) {
126 				File[] allFileAndFolder = FileUtil
127 						.allFileContent(tempFileSelection[i]);
128 				for (int k = 0; k < allFileAndFolder.length; k++)
129 					fileSelection.add(allFileAndFolder[k]);
130 			}
131 		}
132 		return fileSelection.toArray(new File[0]);
133 	}
134 
135 }