2020import org .junit .Assert ;
2121import org .junit .Assume ;
2222import org .junit .Before ;
23- import org .junit .Ignore ;
23+ import org .junit .BeforeClass ;
2424import org .junit .Test ;
2525import org .junit .runner .RunWith ;
2626import org .junit .runners .Parameterized ;
3737import java .io .OutputStream ;
3838import java .nio .ByteBuffer ;
3939import java .nio .ByteOrder ;
40- import java .nio .IntBuffer ;
4140import java .nio .channels .FileChannel ;
4241import java .nio .file .Files ;
4342import java .util .ArrayList ;
@@ -99,18 +98,13 @@ public LZ4FrameIOStreamTest(int testSize) {
9998
10099 @ Before
101100 public void setUp () throws IOException {
102- final int fill = 0xDEADBEEF ;
103101 tmpFile = Files .createTempFile ("lz4ioTest" , ".dat" ).toFile ();
104102 final Random rnd = new Random (5378L );
105103 int sizeRemaining = testSize ;
106104 try (OutputStream os = Files .newOutputStream (tmpFile .toPath ())) {
107105 while (sizeRemaining > 0 ) {
108106 final byte [] buff = new byte [Math .min (sizeRemaining , 1 << 10 )];
109- final IntBuffer intBuffer = ByteBuffer .wrap (buff ).order (ByteOrder .LITTLE_ENDIAN ).asIntBuffer ();
110107 rnd .nextBytes (buff );
111- while (intBuffer .hasRemaining ()) {
112- intBuffer .put (fill );
113- }
114108 os .write (buff );
115109 sizeRemaining -= buff .length ;
116110 }
@@ -125,6 +119,28 @@ public void tearDown() {
125119 }
126120 }
127121
122+ /**
123+ * Whether the native LZ4 CLI is available; can be used for comparing this library with the expected native LZ4 behavior
124+ */
125+ private static boolean hasLz4CLI = false ;
126+
127+ @ BeforeClass
128+ public static void checkLz4CLI () {
129+ try {
130+ ProcessBuilder checkBuilder = new ProcessBuilder ().command ("lz4" , "-V" ).redirectErrorStream (true );
131+ Process checkProcess = checkBuilder .start ();
132+ hasLz4CLI = checkProcess .waitFor () == 0 ;
133+ } catch (IOException | InterruptedException e ) {
134+ // lz4 CLI not available or failed to execute; treat as unavailable to allow test skip
135+ hasLz4CLI = false ;
136+ }
137+
138+ // Check if this is running in CI (env CI=true), see https://docs.github.com/en/actions/reference/workflows-and-actions/variables#default-environment-variables
139+ if (!hasLz4CLI && "true" .equals (System .getenv ("CI" ))) {
140+ Assert .fail ("LZ4 CLI is not available, but should be for CI run" );
141+ }
142+ }
143+
128144 private void fillBuffer (final byte [] buffer , final InputStream is ) throws IOException {
129145 int offset = 0 ;
130146 while (offset < buffer .length ) {
@@ -197,9 +213,10 @@ public void testOutputSimple() throws IOException {
197213 copy (is , os );
198214 }
199215 }
200- final FileChannel channel = FileChannel .open (lz4File .toPath ());
201216 final ByteBuffer buffer = ByteBuffer .allocate (1024 ).order (ByteOrder .LITTLE_ENDIAN );
202- channel .read (buffer );
217+ try (FileChannel channel = FileChannel .open (lz4File .toPath ())) {
218+ channel .read (buffer );
219+ }
203220 buffer .rewind ();
204221 Assert .assertEquals (LZ4FrameOutputStream .MAGIC , buffer .getInt ());
205222 final BitSet b = BitSet .valueOf (new byte []{buffer .get ()});
@@ -296,12 +313,7 @@ public void testSkippableOnly() throws IOException {
296313 }
297314 // Extra one byte at the tail
298315 try (InputStream is = new LZ4FrameInputStream (new SequenceInputStream (new FileInputStream (lz4File ), new ByteArrayInputStream (new byte [1 ])))) {
299- is .read ();
300- Assert .assertFalse (true );
301- } catch (IOException ex ) {
302- // OK
303- } catch (Exception ex ) {
304- Assert .assertFalse (true );
316+ Assert .assertThrows (IOException .class , is ::read );
305317 }
306318 } finally {
307319 lz4File .delete ();
@@ -399,12 +411,7 @@ public void testInputOutputMultipleFrames() throws IOException {
399411 }
400412 }
401413 try (LZ4FrameInputStream is = new LZ4FrameInputStream (new FileInputStream (lz4File ))) {
402- try {
403- is .getExpectedContentSize ();
404- Assert .assertFalse (true );
405- } catch (UnsupportedOperationException e ) {
406- // OK
407- }
414+ Assert .assertThrows (UnsupportedOperationException .class , is ::getExpectedContentSize );
408415 Assert .assertFalse (is .isExpectedContentSizeDefined ());
409416 validateStreamEquals (is , tmpFile );
410417 validateStreamEquals (is , tmpFile );
@@ -427,7 +434,7 @@ public void testInputOutputMultipleFrames() throws IOException {
427434
428435 @ Test
429436 public void testNativeCompressIfAvailable () throws IOException , InterruptedException {
430- Assume .assumeTrue (hasNativeLz4CLI () );
437+ Assume .assumeTrue (hasLz4CLI );
431438 nativeCompress ();
432439 nativeCompress ("--no-frame-crc" );
433440 }
@@ -444,7 +451,7 @@ private void nativeCompress(String... args) throws IOException, InterruptedExcep
444451 }
445452 cmd .add (tmpFile .getAbsolutePath ());
446453 cmd .add (lz4File .getAbsolutePath ());
447- builder .command (cmd .toArray (new String [cmd . size () ]));
454+ builder .command (cmd .toArray (new String [0 ]));
448455 builder .inheritIO ();
449456 Process process = builder .start ();
450457 int retval = process .waitFor ();
@@ -479,20 +486,9 @@ public void testUncompressableEnd() throws IOException {
479486 }
480487 }
481488
482- private static boolean hasNativeLz4CLI () throws IOException , InterruptedException {
483- try {
484- ProcessBuilder checkBuilder = new ProcessBuilder ().command ("lz4" , "-V" ).redirectErrorStream (true );
485- Process checkProcess = checkBuilder .start ();
486- return checkProcess .waitFor () == 0 ;
487- } catch (IOException | InterruptedException e ) {
488- // lz4 CLI not available or failed to execute; treat as unavailable to allow test skip
489- return false ;
490- }
491- }
492-
493489 @ Test
494- public void testNativeDecompresIfAvailable () throws IOException , InterruptedException {
495- Assume .assumeTrue (hasNativeLz4CLI () );
490+ public void testNativeDecompressIfAvailable () throws IOException , InterruptedException {
491+ Assume .assumeTrue (hasLz4CLI );
496492 final File lz4File = Files .createTempFile ("lz4test" , ".lz4" ).toFile ();
497493 final File unCompressedFile = Files .createTempFile ("lz4raw" , ".dat" ).toFile ();
498494 unCompressedFile .delete ();
@@ -527,26 +523,16 @@ public void testNativeDecompresIfAvailable() throws IOException, InterruptedExce
527523 }
528524
529525 @ Test
530- public void testEmptyLZ4Input () {
526+ public void testEmptyLZ4Input () throws IOException {
531527 try (InputStream is = new LZ4FrameInputStream (new ByteArrayInputStream (new byte [0 ]))) {
532- is .read ();
533- Assert .assertFalse (true );
534- } catch (IOException ex ) {
535- // OK
536- } catch (Exception ex ) {
537- Assert .assertFalse (true );
528+ Assert .assertThrows (IOException .class , is ::read );
538529 }
539530 }
540531
541532 @ Test
542533 public void testPrematureMagicNb () throws IOException {
543534 try (InputStream is = new LZ4FrameInputStream (new ByteArrayInputStream (new byte [1 ]))) {
544- is .read ();
545- Assert .assertFalse (true );
546- } catch (IOException ex ) {
547- // OK
548- } catch (Exception ex ) {
549- Assert .assertFalse (true );
535+ Assert .assertThrows (IOException .class , is ::read );
550536 }
551537
552538 final File lz4File = Files .createTempFile ("lz4test" , ".lz4" ).toFile ();
@@ -559,12 +545,7 @@ public void testPrematureMagicNb() throws IOException {
559545 // Extra one byte at the tail
560546 try (InputStream is = new LZ4FrameInputStream (new SequenceInputStream (new FileInputStream (lz4File ), new ByteArrayInputStream (new byte [1 ])))) {
561547 validateStreamEquals (is , tmpFile );
562- is .read ();
563- Assert .assertFalse (true );
564- } catch (IOException ex ) {
565- // OK
566- } catch (Exception ex ) {
567- Assert .assertFalse (true );
548+ Assert .assertThrows (IOException .class , is ::read );
568549 }
569550 } finally {
570551 lz4File .delete ();
0 commit comments