-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava3.java
More file actions
82 lines (71 loc) · 2.68 KB
/
Copy pathJava3.java
File metadata and controls
82 lines (71 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package pl.mperor.lab.java;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pl.mperor.lab.java.jndi.CustomInitialContextFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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)
*/
public class Java3 {
@Test
public void testJavaNamingAndDirectoryInterfaceAkaJNDILookup() throws NamingException {
System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, CustomInitialContextFactory.class.getName());
String bindingName = "hello";
String bindingResult = "Hello World!";
Context ctx = new InitialContext();
ctx.bind(bindingName, bindingResult);
String lookupResult = (String) ctx.lookup(bindingName);
Assertions.assertEquals(bindingResult, lookupResult);
ctx.unbind(bindingName);
Assertions.assertThrows(NamingException.class, () -> {
ctx.lookup(bindingName);
});
}
@Test
public void testJavaSoundAPI() throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException {
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();
}
}