24
24
import java .util .TreeSet ;
25
25
import java .util .concurrent .atomic .AtomicReference ;
26
26
27
+ import org .apache .commons .io .FileUtils ;
28
+ import static org .jboss .as .test .shared .FileUtils .computeHash ;
29
+ import static org .jboss .as .test .shared .FileUtils .unzipFile ;
30
+
27
31
import org .jboss .logging .Logger ;
28
- import org .junit .AssumptionViolatedException ;
29
- import org .junit .BeforeClass ;
30
32
import org .junit .Test ;
31
33
32
34
/**
@@ -59,17 +61,13 @@ private static Path resolveJBossHome() {
59
61
}
60
62
}
61
63
62
- @ BeforeClass
63
- public static void assumeNotS390 () {
64
- if ("s390x" .equalsIgnoreCase (System .getProperty ("os.arch" ))) {
65
- throw new AssumptionViolatedException ("WFCORE-7269" );
66
- }
64
+ private static File getDistFile (Path channelPath , boolean exists , boolean directory , List <String > errors ) {
65
+ return getDistFile (CHANNEL_INSTALLATION , channelPath , DIST_INSTALLATION , exists , directory , errors );
67
66
}
68
67
69
- private static File getDistFile (Path channelPath , boolean exists , boolean directory , List <String > errors ) {
70
- Path relative = CHANNEL_INSTALLATION .relativize (channelPath );
71
- System .out .println ("Getting dist file for relative path " + relative );
72
- Path path = DIST_INSTALLATION .resolve (relative );
68
+ private static File getDistFile (Path currentRoot , Path currentPath , Path distRoot , boolean exists , boolean directory , List <String > errors ) {
69
+ Path relative = currentRoot .relativize (currentPath );
70
+ Path path = distRoot .resolve (relative );
73
71
File test = path .toFile ();
74
72
File result = null ;
75
73
if (exists ) {
@@ -142,9 +140,14 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
142
140
} else {
143
141
File distFile = getDistFile (path , true , false , errors );
144
142
if (distFile != null ) {
145
- if (path .toFile ().length () != distFile .length ()) {
146
- errors .add (String .format ("dist file for %s has an unexpected length: %d not %d" ,
143
+ if (path .toFile ().length () != distFile .length ()) {
144
+ log .trace ("File size is different" );
145
+ // This can happen on some platforms for fat jar generated at provisioning time.
146
+ // Check that the actual jar content is identical.
147
+ if (!sameJarContent (path , distFile .toPath (), errors )) {
148
+ errors .add (String .format ("dist file for %s has an unexpected length: %d not %d" ,
147
149
path , distFile .length (), path .toFile ().length ()));
150
+ }
148
151
}
149
152
}
150
153
}
@@ -169,4 +172,89 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
169
172
fail (errors .toString ());
170
173
}
171
174
}
175
+
176
+ private static boolean sameJarContent (Path current , Path dist , List <String > errors ) {
177
+ if (!dist .getFileName ().toString ().endsWith (".jar" )) {
178
+ return false ;
179
+ }
180
+ log .trace ("Checking jar " + current );
181
+ Path tempFolder = null ;
182
+ try {
183
+ tempFolder = Files .createTempDirectory ("checkconsistency" );
184
+ Path distUnzipped = tempFolder .resolve ("dist" );
185
+ Path currentUnzipped = tempFolder .resolve ("current" );
186
+ unzipFile (dist , distUnzipped );
187
+ unzipFile (current , currentUnzipped );
188
+ checkContent (currentUnzipped , distUnzipped , errors );
189
+ if (errors .isEmpty ()) return true ;
190
+ } catch (IOException ex ) {
191
+ errors .add ("Exception occurred when checking " + dist + " file content. " + ex );
192
+ } finally {
193
+ try {
194
+ if (tempFolder != null ) {
195
+ FileUtils .deleteDirectory (tempFolder .toFile ());
196
+ }
197
+ } catch (IOException ex ) {
198
+ throw new RuntimeException (ex );
199
+ }
200
+ }
201
+ return false ;
202
+ }
203
+
204
+ private static void checkContent (Path currentRoot , Path dist , List <String > errors ) throws IOException {
205
+ Files .walkFileTree (currentRoot , new FileVisitor <>() {
206
+ @ Override
207
+ public FileVisitResult preVisitDirectory (Path dir , BasicFileAttributes attrs ) {
208
+ log .trace ("Testing " + dir );
209
+ if (dir .equals (currentRoot )) {
210
+ File distRoot = getDistFile (currentRoot , dir , dist , true , true , errors );
211
+ Set <String > channelChildren = new TreeSet <>(Arrays .asList (Objects .requireNonNull (dir .toFile ().list ())));
212
+ Set <String > distChildren = new TreeSet <>(Arrays .asList (Objects .requireNonNull (distRoot .list ())));
213
+ assertEquals (dir .toString (), channelChildren , distChildren );
214
+ return FileVisitResult .CONTINUE ;
215
+ } else {
216
+ File distDir = getDistFile (currentRoot , dir , dist , true , true , errors );
217
+ if (distDir != null ) {
218
+ assertTrue (dir .toString (), Objects .deepEquals (dir .toFile ().list (), distDir .list ()));
219
+ return FileVisitResult .CONTINUE ;
220
+ } else {
221
+ return FileVisitResult .SKIP_SUBTREE ;
222
+ }
223
+ }
224
+ }
225
+
226
+ @ Override
227
+ public FileVisitResult visitFile (Path path , BasicFileAttributes attrs ) {
228
+ System .out .println ("Checking " + path );
229
+ File distFile = getDistFile (currentRoot , path , dist , true , false , errors );
230
+ if (distFile == null ) {
231
+ errors .add (String .format ("current file %s has not been found in %s" ,
232
+ path , dist ));
233
+ } else {
234
+ try {
235
+ String hash1 = computeHash (path );
236
+ String hash2 = computeHash (distFile .toPath ());
237
+ if (!hash1 .equals (hash2 )) {
238
+ errors .add (String .format ("dist file for %s has an unexpected content" ,
239
+ path ));
240
+ }
241
+ } catch (Exception ex ) {
242
+ errors .add (String .format ("dist file for %s has an unexpected length: %d not %d" ,
243
+ path , distFile .length (), path .toFile ().length ()));
244
+ }
245
+ }
246
+ return FileVisitResult .CONTINUE ;
247
+ }
248
+
249
+ @ Override
250
+ public FileVisitResult visitFileFailed (Path file , IOException exc ) throws IOException {
251
+ throw exc ;
252
+ }
253
+
254
+ @ Override
255
+ public FileVisitResult postVisitDirectory (Path dir , IOException exc ) {
256
+ return FileVisitResult .CONTINUE ;
257
+ }
258
+ });
259
+ }
172
260
}
0 commit comments