View Javadoc
1   package org.apache.maven.plugins.assembly.archive.phase;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
25  import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest;
26  import org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask;
27  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
28  import org.apache.maven.plugins.assembly.model.Assembly;
29  import org.apache.maven.plugins.assembly.model.FileSet;
30  import org.apache.maven.plugins.assembly.testutils.TestFileManager;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.logging.Logger;
33  import org.easymock.EasyMock;
34  import org.easymock.classextension.EasyMockSupport;
35  
36  import java.io.IOException;
37  
38  import static org.easymock.EasyMock.anyObject;
39  import static org.easymock.EasyMock.expect;
40  
41  public class FileSetAssemblyPhaseTest
42      extends TestCase
43  {
44  
45      final EasyMockSupport mm = new EasyMockSupport();
46  
47      private final TestFileManager fileManager = new TestFileManager( "file-set-assembly.test.", "" );
48  
49      @Override
50      public void tearDown()
51          throws IOException
52      {
53          fileManager.cleanUp();
54      }
55  
56      public void testShouldNotFailWhenNoFileSetsSpecified()
57          throws ArchiveCreationException, AssemblyFormattingException
58      {
59          final Assembly assembly = new Assembly();
60  
61          assembly.setId( "test" );
62  
63          final MockAndControlForLogger macLogger = new MockAndControlForLogger();
64          final MockAndControlForAddFileSetsTask macTask = new MockAndControlForAddFileSetsTask( mm, fileManager );
65  
66          mm.replayAll();
67  
68          createPhase( macLogger ).execute( assembly, macTask.archiver, macTask.configSource );
69  
70          mm.verifyAll();
71      }
72  
73      public void testShouldAddOneFileSet()
74          throws ArchiveCreationException, AssemblyFormattingException
75      {
76          final Assembly assembly = new Assembly();
77  
78          assembly.setId( "test" );
79          assembly.setIncludeBaseDirectory( false );
80  
81          final FileSet fs = new FileSet();
82          fs.setOutputDirectory( "/out" );
83          fs.setDirectory( "/input" );
84          fs.setFileMode( "777" );
85          fs.setDirectoryMode( "777" );
86  
87          assembly.addFileSet( fs );
88  
89          final MockAndControlForLogger macLogger = new MockAndControlForLogger();
90          final MockAndControlForAddFileSetsTask macTask = new MockAndControlForAddFileSetsTask( mm, fileManager );
91  
92          macTask.expectGetArchiveBaseDirectory();
93  
94          final MavenProject project = new MavenProject( new Model() );
95  
96          macLogger.expectError( true, true );
97  
98          final int dirMode = Integer.parseInt( "777", 8 );
99          final int fileMode = Integer.parseInt( "777", 8 );
100 
101         final int[] modes = { -1, -1, dirMode, fileMode };
102 
103         macTask.expectAdditionOfSingleFileSet( project, "final-name", false, modes, 1, true );
104 
105         DefaultAssemblyArchiverTest.setupInterpolators( macTask.configSource );
106 
107         mm.replayAll();
108 
109         createPhase( macLogger ).execute( assembly, macTask.archiver, macTask.configSource );
110 
111         mm.verifyAll();
112     }
113 
114     private FileSetAssemblyPhase createPhase( final MockAndControlForLogger macLogger )
115     {
116         final FileSetAssemblyPhase phase = new FileSetAssemblyPhase();
117 
118         phase.enableLogging( macLogger.logger );
119 
120         return phase;
121     }
122 
123     private final class MockAndControlForLogger
124     {
125         final Logger logger;
126 
127         MockAndControlForLogger()
128         {
129             logger = mm.createMock( Logger.class );
130         }
131 
132         public void expectError( final boolean debugCheck, final boolean debugEnabled )
133         {
134             if ( debugCheck )
135             {
136                 expect( logger.isDebugEnabled() ).andReturn( debugEnabled ).anyTimes();
137             }
138 
139             logger.debug( (String) anyObject() );
140             EasyMock.expectLastCall().anyTimes();
141             logger.warn( (String) anyObject() );
142             EasyMock.expectLastCall().anyTimes();
143             logger.error( (String) anyObject() );
144             EasyMock.expectLastCall().anyTimes();
145         }
146 
147     }
148 
149 }