View Javadoc
1   /*
2    * Copyright 2007 The Codehaus Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.archiver.rar;
17  
18  import java.io.File;
19  import org.codehaus.plexus.archiver.Archiver;
20  import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
21  import org.codehaus.plexus.archiver.UnArchiver;
22  import org.codehaus.plexus.util.FileUtils;
23  
24  /**
25   * @author <a href="mailto:olamy@codehaus.org">olamy</a>
26   * @since 13 mars 07
27   */
28  public class RarArchiverTest
29      extends BasePlexusArchiverTest
30  {
31  
32      public File getTargetRarFolder()
33      {
34          return new File( getBasedir(), "/target/rartest/" );
35      }
36  
37      @Override
38      protected void setUp()
39          throws Exception
40      {
41          super.setUp();
42          // clean output directory and re create it
43          if ( getTargetRarFolder().exists() )
44          {
45              FileUtils.deleteDirectory( getTargetRarFolder() );
46          }
47      }
48  
49      public void testArchive()
50          throws Exception
51      {
52          Archiver archiver = (Archiver) lookup( Archiver.ROLE, "rar" );
53          archiver.setDestFile( new File( getTargetRarFolder(), "test.rar" ) );
54          //archiver.addDirectory( , "manifests" );
55          archiver.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), "manifests/manifest1.mf" );
56  
57          archiver.createArchive();
58          assertTrue( new File( getTargetRarFolder(), "test.rar" ).exists() );
59  
60          UnArchiver unArchiver = (UnArchiver) lookup( UnArchiver.ROLE, "rar" );
61          unArchiver.setSourceFile( new File( getTargetRarFolder(), "test.rar" ) );
62          unArchiver.setDestDirectory( getTargetRarFolder() );
63          unArchiver.extract();
64          File manifestsDir = new File( getTargetRarFolder(), "/manifests" );
65          assertTrue( manifestsDir.exists() );
66  
67          File manifestsFile = new File( getTargetRarFolder(), "/manifests/manifest1.mf" );
68          assertTrue( manifestsFile.exists() );
69      }
70  
71      public void testUnarchive()
72          throws Exception
73      {
74  
75          UnArchiver unArchiver = (UnArchiver) lookup( UnArchiver.ROLE, "rar" );
76          File rarFile = new File( getBasedir() + "/src/test/jars/test.rar" );
77          assertTrue( rarFile.exists() );
78          unArchiver.setSourceFile( rarFile );
79          unArchiver.setDestDirectory( getTargetRarFolder() );
80          getTargetRarFolder().mkdir();
81          unArchiver.extract();
82  
83          File dirExtract = new File( getTargetRarFolder(), "META-INF" );
84          assertTrue( dirExtract.exists() );
85          assertTrue( dirExtract.isDirectory() );
86  
87      }
88  
89      /**
90       * Tests the .rar archiver is forced set to true, and after that
91       * tests the behavior when the forced is set to false.
92       *
93       * @throws Exception
94       */
95      public void testRarIsForcedBehaviour() throws Exception
96      {
97          Archiver rarArvhiver = createArchiver( "rar" );
98  
99          assertTrue( rarArvhiver.isSupportingForced() );
100 
101         rarArvhiver.createArchive();
102 
103         final long creationTime = rarArvhiver.getDestFile().lastModified();
104 
105         rarArvhiver = createArchiver( "rar" );
106 
107         assertTrue( rarArvhiver.isSupportingForced() );
108         //Default should be true
109         rarArvhiver.setForced( true );
110 
111         waitUntilNewTimestamp( rarArvhiver.getDestFile(), creationTime );
112         rarArvhiver.createArchive();
113 
114         final long firstRunTime = rarArvhiver.getDestFile().lastModified();
115         assertFalse( creationTime == firstRunTime );
116 
117         //waitUntilNewTimestamp( rarArvhiver.getDestFile(), firstRunTime );
118         rarArvhiver = createArchiver( "rar" );
119 
120         rarArvhiver.setForced( false );
121         rarArvhiver.createArchive();
122 
123         final long secondRunTime = rarArvhiver.getDestFile().lastModified();
124 
125         assertEquals( secondRunTime, firstRunTime );
126     }
127 
128 }