View Javadoc
1   package org.apache.maven.plugin.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 java.io.File;
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.maven.model.Model;
28  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
29  import org.apache.maven.plugin.assembly.archive.DefaultAssemblyArchiverTest;
30  import org.apache.maven.plugin.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask;
31  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
32  import org.apache.maven.plugin.assembly.model.Assembly;
33  import org.apache.maven.plugin.assembly.model.FileSet;
34  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.logging.Logger;
37  import org.easymock.EasyMock;
38  import org.easymock.classextension.EasyMockSupport;
39  
40  import static org.easymock.EasyMock.anyObject;
41  import static org.easymock.EasyMock.expect;
42  
43  public class FileSetAssemblyPhaseTest
44      extends TestCase
45  {
46  
47      final EasyMockSupport mm = new EasyMockSupport();
48  
49      private final TestFileManager fileManager = new TestFileManager( "file-set-assembly.test.", "" );
50  
51      @Override
52      public void tearDown()
53          throws IOException
54      {
55          fileManager.cleanUp();
56      }
57  
58      public void testShouldNotFailWhenNoFileSetsSpecified()
59          throws ArchiveCreationException, AssemblyFormattingException
60      {
61          final Assembly assembly = new Assembly();
62  
63          assembly.setId( "test" );
64  
65          final MockAndControlForLogger macLogger = new MockAndControlForLogger();
66          final MockAndControlForAddFileSetsTask macTask =
67              new MockAndControlForAddFileSetsTask( mm, fileManager );
68  
69          mm.replayAll();
70  
71          createPhase( macLogger ).execute( assembly, macTask.archiver, macTask.configSource );
72  
73          mm.verifyAll();
74      }
75  
76      public void testShouldAddOneFileSet()
77          throws ArchiveCreationException, AssemblyFormattingException
78      {
79          final Assembly assembly = new Assembly();
80  
81          assembly.setId( "test" );
82          assembly.setIncludeBaseDirectory( false );
83  
84          final FileSet fs = new FileSet();
85          fs.setOutputDirectory( "/out" );
86          fs.setDirectory( "/input" );
87          fs.setFileMode( "777" );
88          fs.setDirectoryMode( "777" );
89  
90          assembly.addFileSet( fs );
91  
92          final MockAndControlForLogger macLogger = new MockAndControlForLogger();
93          final MockAndControlForAddFileSetsTask macTask =
94              new MockAndControlForAddFileSetsTask( mm, fileManager );
95  
96          macTask.expectGetArchiveBaseDirectory();
97  
98          final MavenProject project = new MavenProject( new Model() );
99  
100         macLogger.expectError( true, true );
101 
102         final int dirMode = Integer.parseInt( "777", 8 );
103         final int fileMode = Integer.parseInt( "777", 8 );
104 
105         final int[] modes = { -1, -1, dirMode, fileMode };
106 
107         macTask.expectAdditionOfSingleFileSet( project, "final-name", false, modes, 1, true );
108 
109         DefaultAssemblyArchiverTest.setupInterpolators(  macTask.configSource  );
110 
111         mm.replayAll();
112 
113         createPhase( macLogger ).execute( assembly, macTask.archiver, macTask.configSource );
114 
115         mm.verifyAll();
116     }
117 
118     private FileSetAssemblyPhase createPhase( final MockAndControlForLogger macLogger )
119     {
120         final FileSetAssemblyPhase phase = new FileSetAssemblyPhase();
121 
122         phase.enableLogging( macLogger.logger );
123 
124         return phase;
125     }
126 
127     private final class MockAndControlForLogger
128     {
129         final Logger logger;
130 
131         MockAndControlForLogger()
132         {
133             logger = mm.createMock(Logger.class);
134         }
135 
136         public void expectDebug( final boolean debugCheck, final boolean debugEnabled )
137         {
138             if ( debugCheck )
139             {
140                 expect( logger.isDebugEnabled()).andReturn( debugEnabled ).anyTimes();
141             }
142 
143             logger.debug( (String) anyObject() );
144             EasyMock.expectLastCall().anyTimes();
145         }
146         public void expectWarn( final boolean debugCheck, final boolean debugEnabled )
147         {
148             if ( debugCheck )
149             {
150                 expect( logger.isDebugEnabled()).andReturn( debugEnabled ).anyTimes();
151             }
152 
153             logger.debug( (String) anyObject() );
154             EasyMock.expectLastCall().anyTimes();
155             logger.warn( (String) anyObject() );
156             EasyMock.expectLastCall().anyTimes();
157         }
158         public void expectError( final boolean debugCheck, final boolean debugEnabled )
159         {
160             if ( debugCheck )
161             {
162                 expect( logger.isDebugEnabled()).andReturn( debugEnabled ).anyTimes();
163             }
164 
165             logger.debug( (String) anyObject() );
166             EasyMock.expectLastCall().anyTimes();
167             logger.warn( (String) anyObject() );
168             EasyMock.expectLastCall().anyTimes();
169             logger.error( (String) anyObject() );
170             EasyMock.expectLastCall().anyTimes();
171         }
172 
173     }
174 
175 }