View Javadoc
1   package org.apache.maven.plugins.repository.testutil;
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  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.Set;
27  import java.util.zip.ZipFile;
28  
29  import junit.framework.Assert;
30  
31  public final class Assertions
32  {
33      
34      public static final Set<String> EMPTY_ENTRY_NAMES = Collections.emptySet();
35  
36      public static void assertZipContents( Iterable<String> requiredNames, Iterable<String> bannedNames, File bundleSource )
37          throws IOException
38      {
39          ZipFile zf = new ZipFile( bundleSource );
40  
41          Set<String> missing = new HashSet<String>();
42          for ( String name : requiredNames )
43          {
44              if ( zf.getEntry( name ) == null )
45              {
46                  missing.add( name );
47              }
48          }
49  
50          Set<String> banned = new HashSet<String>();
51          for ( String name : bannedNames )
52          {
53              if ( zf.getEntry( name ) != null )
54              {
55                  banned.add( name );
56              }
57          }
58  
59          zf.close();
60          if ( !missing.isEmpty() || !banned.isEmpty() )
61          {
62              StringBuilder msg = new StringBuilder();
63              msg.append( "The following REQUIRED entries were missing from the bundle archive:\n" );
64  
65              if ( missing.isEmpty() )
66              {
67                  msg.append( "\nNone." );
68              }
69              else
70              {
71                  for ( String name : missing )
72                  {
73                      msg.append( "\n" ).append( name );
74                  }
75              }
76  
77              msg.append( "\n\nThe following BANNED entries were present from the bundle archive:\n" );
78  
79              if ( banned.isEmpty() )
80              {
81                  msg.append( "\nNone.\n" );
82              }
83              else
84              {
85                  for ( String name : banned )
86                  {
87                      msg.append( "\n" ).append( name );
88                  }
89              }
90  
91              Assert.fail( msg.toString() );
92          }
93      }
94  }