Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java3.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Java 1.3 (May 2000)
Expand All @@ -36,19 +40,43 @@ public void testJavaNamingAndDirectoryInterfaceAkaJNDILookup() throws NamingExce

@Test
public void testJavaSoundAPI() throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException {
if (AudioSystem.getMixerInfo().length != 0)
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/test/resources/beep.wav"))) {
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
CountDownLatch latch = new CountDownLatch(1);
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
latch.countDown();
}
});
clip.start();
latch.await();
if (AudioSystem.getMixerInfo().length == 0) {
return;
}

try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/test/resources/beep.wav"))) {
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
CountDownLatch latch = new CountDownLatch(1);
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
latch.countDown();
}
});
clip.start();
latch.await();
}
}

@Test
public void testScheduledTask() throws InterruptedException {
var schedulerCounter = new AtomicInteger(0);
var latch = new CountDownLatch(2);

var scheduler = new TimerTask() {
@Override
public void run() {
schedulerCounter.getAndIncrement();
latch.countDown();
}
};
var timer = new Timer();
timer.scheduleAtFixedRate(scheduler, 0, 100);

boolean completed = latch.await(300, TimeUnit.MILLISECONDS);
Assertions.assertTrue(completed, "Scheduler did not run twice in time");
Assertions.assertEquals(2, schedulerCounter.get());
timer.cancel();
}
}
23 changes: 21 additions & 2 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

import static java.lang.Math.max;
Expand Down Expand Up @@ -113,6 +113,25 @@ public void testConcurrencyAPI() {
TestUtils.resetSystemOut();
}

@Test
public void testScheduledExecutor() throws InterruptedException {
var counter = new AtomicInteger(0);
var latch = new CountDownLatch(2);

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
counter.getAndIncrement();
latch.countDown();
}, 0, 100, TimeUnit.MILLISECONDS);

boolean completed = latch.await(300, TimeUnit.MILLISECONDS);
Assertions.assertTrue(completed, "Task did not execute twice in time");
Assertions.assertEquals(2, counter.get());

future.cancel(true);
executor.shutdownNow();
}

@Test
public void testStaticImports() {
Assertions.assertEquals(4, sqrt(16)); // Math.sqrt(16);
Expand Down