1 package org.apache.its.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static junit.framework.Assert.assertTrue;
23 import static junit.framework.Assert.fail;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.net.URL;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.LinkedHashSet;
33 import java.util.LinkedList;
34 import java.util.Set;
35 import java.util.zip.ZipEntry;
36 import java.util.zip.ZipException;
37 import java.util.zip.ZipFile;
38
39 import org.codehaus.plexus.archiver.tar.GZipTarFile;
40 import org.codehaus.plexus.archiver.tar.TarEntry;
41 import org.codehaus.plexus.archiver.tar.TarFile;
42
43 public class TestUtils
44 {
45
46 public static String archivePathFromChild( String artifactId, String version, String childName, String childPath )
47 {
48 if ( !childPath.startsWith( "/" ) )
49 {
50 childPath = "/" + childPath;
51 }
52
53 return ( artifactId + "-" + version + "/" + artifactId + "-" + childName + childPath );
54 }
55
56 public static String archivePathFromProject( String artifactId, String version, String path )
57 {
58 if ( !path.startsWith( "/" ) )
59 {
60 path = "/" + path;
61 }
62
63 return ( artifactId + "-" + version + path );
64 }
65
66
67 public static void assertTarContents( Set<String> required, Set<String> banned, File assembly )
68 throws IOException
69 {
70 assertTrue( "Assembly archive missing: " + assembly, assembly.isFile() );
71
72 GZipTarFile tarFile = null;
73 try
74 {
75 tarFile = new GZipTarFile( assembly );
76
77 LinkedHashSet<String> pathSet = new LinkedHashSet<String>();
78
79 for ( @SuppressWarnings( "unchecked" )
80 Enumeration<TarEntry> enumeration = tarFile.getEntries(); enumeration.hasMoreElements(); )
81 {
82 pathSet.add( enumeration.nextElement().getName() );
83 }
84 assertArchiveContents( required, banned, assembly.getAbsolutePath(), pathSet );
85 }
86 finally
87 {
88 if ( tarFile != null )
89 {
90 tarFile.close();
91 }
92 }
93 }
94
95 public static void assertZipContents( Set<String> required, Set<String> banned, File assembly )
96 throws ZipException, IOException
97 {
98 assertTrue( "Assembly archive missing: " + assembly, assembly.isFile() );
99
100 ZipFile zf = null;
101 try
102 {
103 zf = new ZipFile( assembly );
104
105 LinkedHashSet<String> pathSet = new LinkedHashSet<String>();
106
107 for ( Enumeration<? extends ZipEntry> enumeration = zf.entries(); enumeration.hasMoreElements(); )
108 {
109 pathSet.add( enumeration.nextElement().getName() );
110 }
111 assertArchiveContents( required, banned, assembly.getAbsolutePath(), pathSet );
112 }
113 finally
114 {
115 if ( zf != null )
116 {
117 zf.close();
118 }
119 }
120 }
121
122 private static void assertArchiveContents(Set<String> required, Set<String> banned, String assemblyName, Set<String> contents )
123 {
124
125 Set<String> missing = new HashSet<String>();
126 for ( String name : required )
127 {
128 if ( !contents.contains( name ) )
129 {
130 missing.add( name );
131 }
132 }
133
134 Set<String> banViolations = new HashSet<String>();
135 for ( String name : banned )
136 {
137 if ( contents.contains( name ) )
138 {
139 banViolations.add( name );
140 }
141 }
142
143 if ( !missing.isEmpty() || !banViolations.isEmpty() )
144 {
145 StringBuffer msg = new StringBuffer();
146 msg.append( "The following errors were found in:\n\n" );
147 msg.append( assemblyName );
148 msg.append( "\n");
149 msg.append( "\nThe following REQUIRED entries were missing from the bundle archive:\n" );
150
151 if ( missing.isEmpty() )
152 {
153 msg.append( "\nNone." );
154 }
155 else
156 {
157 for ( String name : missing )
158 {
159 msg.append( "\n" ).append( name );
160 }
161 }
162
163 msg.append( "\n\nThe following BANNED entries were present from the bundle archive:\n" );
164
165 if ( banViolations.isEmpty() )
166 {
167 msg.append( "\nNone.\n" );
168 }
169 else
170 {
171 for ( String name : banViolations )
172 {
173 msg.append( "\n" ).append( name );
174 }
175 }
176
177 msg.append( "\n" ).append( "Archive contents:\n" );
178 for ( String path : contents )
179 {
180 msg.append( "\n" ).append( path );
181 }
182
183 fail( msg.toString() );
184 }
185
186
187 }
188
189 public static File getTestDir( String name )
190 throws IOException, URISyntaxException
191 {
192 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
193 URL resource = cloader.getResource( name );
194
195 if ( resource == null )
196 {
197 throw new IOException( "Cannot find test directory: " + name );
198 }
199
200 return new File( new URI( resource.toExternalForm() ).normalize().getPath() );
201 }
202
203 }