1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.services.repository;
16
17 import org.figure8.join.businessobjects.commons.Release;
18 import org.figure8.join.businessobjects.artifact.Deliverable;
19 import org.figure8.join.businessobjects.artifact.DeliverableType;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import java.io.File;
26 import java.io.FileWriter;
27 import java.io.FileReader;
28 import java.io.BufferedReader;
29 import java.util.Date;
30 /**
31 * JUnit test case for testing the repository implementation based on Jakarta VFS.
32 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
33 * @version $Revision$
34 */
35 public class VFSRepositoryTest extends TestCase{
36
37
38
39 /** The root directory for VFS repository. */
40 private File rootDir = null;
41 /** The VFS repository to test. */
42 private VFSRepository repository = null;
43
44 /** Our test artifact. */
45 private Deliverable artifact = null;
46
47
48
49
50 /** Default constructor. Build a test case using a name. */
51 public VFSRepositoryTest(String testName){
52 super(testName);
53 }
54
55
56
57
58 /**
59 * Make this class a TestSuite.
60 * @return A TestSuite containing this TestCase.
61 */
62 public static Test suite(){
63 TestSuite suite = new TestSuite(VFSRepositoryTest.class);
64 return suite;
65 }
66
67
68
69
70 /** Setup the test fixtures */
71 public void setUp() throws Exception{
72 super.setUp();
73
74 String userDir = System.getProperty("user.dir");
75 rootDir = new File(userDir, "join-repository-test");
76 rootDir.mkdir();
77
78
79 repository = new VFSRepository();
80
81
82 Release release = new Release("1.0", 1, 0, new Date());
83 DeliverableType type = new DeliverableType("mytype", "My Type Label", "mytype-r{0}-v{1}", false, false);
84 artifact = new Deliverable("01", "comments", "supplierId", release, type);
85 }
86
87 /** Test the storage of an artifact within file repository */
88 public void testStoreArtifactOnFile(){
89
90 File artifactFile = new File(rootDir, "vfsartifact.txt");
91 try {writeToFile(artifactFile, "Test content");}
92 catch (Exception e){
93 fail("Exception while creating the artifact file");
94 }
95
96 repository.setBaseUrl("file://" + rootDir.getPath());
97 repository.setStructureDefinition("${release}/${versionInfo}");
98 try {repository.storeArtifact(artifact, artifactFile);}
99 catch (Exception e){
100 fail("Exception while storing artifact within repository");
101 }
102 File targetFile = new File(rootDir + "/1.0/01/mytype-r1.0-v01.txt");
103 assertTrue("Artifact is in repository", targetFile.isFile());
104 assertTrue("Artifact has correct content", compareFileContent(artifactFile, targetFile));
105 }
106
107 /** Test the storage of an artifact within ftp repository */
108 public void testStoreArtifactOnFtp(){
109
110 File artifactFile = new File(rootDir, "vfsartifact.txt");
111 try {writeToFile(artifactFile, "Test content");}
112 catch (Exception e){
113 fail("Exception while creating the artifact file");
114 }
115
116 repository.setBaseUrl("ftp://laurent.broudoux:o5knybkg@ftpperso.free.fr");
117 repository.setStructureDefinition("${release}/${versionInfo}");
118 try {repository.storeArtifact(artifact, artifactFile);}
119 catch (Exception e){
120 fail("Exception while storing artifact within repository");
121 }
122 }
123
124 /** Tear down resources (ie: delete created directory) */
125 public void tearDown() throws Exception{
126 if (rootDir != null && rootDir.isDirectory()){
127 deleteDir(rootDir);
128 rootDir.delete();
129 }
130 }
131
132
133
134
135 /** Delete a directory content. */
136 private void deleteDir(File directory){
137 if (directory != null && directory.isDirectory()){
138 File[] files = directory.listFiles();
139 for (int i=0; i < files.length; i++){
140 File file = files[i];
141 if (file.isDirectory())
142 deleteDir(file);
143 file.delete();
144 }
145 }
146 }
147
148 /** Write content for testing. */
149 private void writeToFile(File targetFile, String content) throws Exception{
150 FileWriter writer = new FileWriter(targetFile);
151 writer.write(content);
152 writer.close();
153 }
154
155 /** Compare file contents. */
156 private boolean compareFileContent(File origine, File destination){
157 boolean result = true;
158 BufferedReader oReader = null;
159 BufferedReader dReader = null;
160 try{
161 String oLine = null;
162 oReader = new BufferedReader(new FileReader(origine));
163 dReader = new BufferedReader(new FileReader(destination));
164 while (result && (oLine = oReader.readLine()) != null){
165 String dLine = dReader.readLine();
166 if (!oLine.equals(dLine))
167 result = false;
168 }
169 }
170 catch (Exception e) {result = false;}
171 finally{
172 try {oReader.close();}
173 catch (Exception e) {
174 try {dReader.close();}
175 catch (Exception e) {
176 }
177 return result;
178 }
179 }