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.FileOutputStream;
29  import java.io.BufferedReader;
30  import java.io.InputStream;
31  import java.util.Date;
32  /**
33   * JUnit test case for testing the simple file system repository implementation.
34   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
35   * @version $Revision: 1.2 $
36   */
37  public class SimpleFileSystemRepositoryTest extends TestCase{
38  
39     // Attributes ---------------------------------------------------------------
40  
41     /** The root directory for FS repository. */
42     private File rootDir = null;
43     /** The FS repository to test. */
44     private SimpleFileSystemRepository repository = null;
45  
46     /** Our test artifact. */
47     private Deliverable artifact = null;
48  
49  
50     // Constructors -------------------------------------------------------------
51  
52     /** Default constructor. Build a test case using a name. */
53     public SimpleFileSystemRepositoryTest(String testName){
54        super(testName);
55     }
56  
57  
58     // Override of TestCase -----------------------------------------------------
59  
60     /**
61      * Make this class a TestSuite.
62      * @return A TestSuite containing this TestCase.
63      */
64     public static Test suite(){
65        TestSuite suite = new TestSuite(SimpleFileSystemRepositoryTest.class);
66        return suite;
67     }
68     
69     
70     // Public -------------------------------------------------------------------
71  
72     /** Setup the test fixtures */
73     public void setUp() throws Exception{
74        super.setUp();
75        // Get user personnal directory.
76        String userDir = System.getProperty("user.dir");
77        rootDir = new File(userDir, "join-repository-test");
78        rootDir.mkdir();
79  
80        // Create a simple fs repository in user dir.
81        repository = new SimpleFileSystemRepository();
82        repository.setRootDirectory(userDir + "/" + rootDir.getName());
83  
84        // Create an artifact (ie : a deliverable).
85        Release release = new Release("1.0", 1, 0, new Date());
86        DeliverableType type = new DeliverableType("mytype", "My Type Label", "mytype-r{0}-v{1}", false, false);
87        artifact = new Deliverable("01", "comments", "supplierId", release, type);
88     }
89  
90     /** Test the storage of an artifact within repository */
91     public void testStoreArtifact(){
92        // Create a simple file for artifact content.
93        File artifactFile = new File(repository.getRootDirectory(), "artifact.txt");
94        try {writeToFile(artifactFile, "Test content");}
95        catch (Exception e){
96           fail("Exception while creating the artifact file");
97        }
98        // Store artifact within repository using different structures.
99        repository.setStructureDefinition("${release}/${versionInfo}");
100       try {repository.storeArtifact(artifact, artifactFile);}
101       catch (Exception e){
102          fail("Exception while storing artifact within repository");
103       }
104       File targetFile = new File(repository.getRootDirectory() + "/1.0/01/mytype-r1.0-v01.txt");
105       assertTrue("Artifact is in repository", targetFile.isFile());
106       assertTrue("Artifact has correct content", compareFileContent(artifactFile, targetFile));
107    }
108 
109    /** Test the retrieval of an artifact within repository */
110    public void testGetArtifact(){
111       // First, ensure an artifact has been stored.
112       testStoreArtifact();
113 
114       // Then, read and check incoming input stream.
115       InputStream is = null;
116       FileOutputStream fos = null;
117       try{
118          // Getting input and opening output.
119          is = repository.getArtifact(artifact);
120          fos = new FileOutputStream(new File(rootDir, "output.txt"));
121          // Read is and write on fos unsing a buffer of bytes.
122          int bytes = 0;
123          byte[] buffer = new byte[8192];
124          while ((bytes = is.read(buffer, 0, 8192)) != -1)
125             fos.write(buffer, 0, bytes);
126       }
127       catch (Exception e){
128          fail("Exception while getting artifact from repository");
129       }
130       finally{
131          try {is.close();}
132          catch (Exception e) {/* Do nothing here... */}
133          try {fos.close();}
134          catch (Exception e) {/* Do nothing here... */}
135       }
136       File sourceFile = new File(repository.getRootDirectory() + "/1.0/01/mytype-r1.0-v01.txt");
137       File outputFile = new File(rootDir, "output.txt");
138       assertTrue("Artifact is extracted from repository", outputFile.isFile());
139       assertTrue("Extracted content is equals to content in repository", compareFileContent(sourceFile, outputFile));
140 
141       // Modify artifact and check exceptions.
142       artifact.setKey("mytype-r1.0-v02");
143       try {repository.getArtifact(artifact);}
144       catch (RepositoryException re){
145          assertTrue("ConnectionException 'cause no artifact in repository", re instanceof ConnectionException);
146       }
147    }
148 
149    /** Tear down resources (ie: delete created directory) */
150    public void tearDown() throws Exception{
151       if (rootDir != null && rootDir.isDirectory()){
152          deleteDir(rootDir);
153          rootDir.delete();
154       }
155    }
156 
157 
158    // Private ------------------------------------------------------------------
159 
160    /** Delete a directory content. */
161    private void deleteDir(File directory){
162       if (directory != null && directory.isDirectory()){
163          File[] files = directory.listFiles();
164          for (int i=0; i < files.length; i++){
165             File file = files[i];
166             if (file.isDirectory())
167                deleteDir(file);
168             file.delete();
169          }
170       }
171    }
172 
173    /** Write content for testing. */
174    private void writeToFile(File targetFile, String content) throws Exception{
175       FileWriter writer = new FileWriter(targetFile);
176       writer.write(content);
177       writer.close();
178    }
179 
180    /** Compare file contents. */
181    private boolean compareFileContent(File origine, File destination){
182       boolean result = true;
183       BufferedReader oReader = null;
184       BufferedReader dReader = null;
185       try{
186          String oLine = null;
187          oReader = new BufferedReader(new FileReader(origine));
188          dReader = new BufferedReader(new FileReader(destination));
189          while (result && (oLine = oReader.readLine()) != null){
190             String dLine = dReader.readLine();
191             if (!oLine.equals(dLine))
192                result = false;
193          }
194       }
195       catch (Exception e) {result = false;}
196       finally{
197          try {oReader.close();}
198          catch (Exception e) {/* Do nothing here... */}
199          try {dReader.close();}
200          catch (Exception e) {/* Do nothing here... */}
201       }
202       return result;
203    }
204 }