Skip to content

Commit a284329

Browse files
committed
JUnit tests
1 parent e821abd commit a284329

File tree

9 files changed

+236
-11
lines changed

9 files changed

+236
-11
lines changed

mutex/src/main/java/com/iluwatar/mutex/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class App {
4040
*/
4141
public static void main(String[] args) {
4242
Mutex mutex = new Mutex();
43-
Jar jar = new Jar(mutex);
43+
Jar jar = new Jar(1000, mutex);
4444
Thief peter = new Thief("Peter", jar);
4545
Thief john = new Thief("John", jar);
4646
peter.start();

mutex/src/main/java/com/iluwatar/mutex/Jar.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ public class Jar {
3737
/**
3838
* The resource within the jar.
3939
*/
40-
private int beans = 1000;
40+
private int beans;
4141

42-
public Jar(Lock lock) {
42+
public Jar(int beans, Lock lock) {
43+
this.beans = beans;
4344
this.lock = lock;
4445
}
4546

4647
/**
4748
* Method for a thief to take a bean.
4849
*/
49-
public boolean takeBean(Thief thief) {
50+
public boolean takeBean() {
5051
boolean success = false;
5152
try {
5253
lock.acquire();

mutex/src/main/java/com/iluwatar/mutex/Mutex.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public class Mutex implements Lock {
3232
*/
3333
private Object owner;
3434

35+
/**
36+
* Returns the current owner of the Mutex, or null if available
37+
*/
38+
public Object getOwner() {
39+
return owner;
40+
}
41+
3542
/**
3643
* Method called by a thread to acquire the lock. If the lock has already
3744
* been acquired this will wait until the lock has been released to
@@ -51,8 +58,10 @@ public synchronized void acquire() throws InterruptedException {
5158
*/
5259
@Override
5360
public synchronized void release() {
54-
owner = null;
55-
notify();
61+
if (Thread.currentThread() == owner) {
62+
owner = null;
63+
notify();
64+
}
5665
}
5766

5867
}

mutex/src/main/java/com/iluwatar/mutex/Thief.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Thief(String name, Jar jar) {
5151
public void run() {
5252
int beans = 0;
5353

54-
while (jar.takeBean(this)) {
54+
while (jar.takeBean()) {
5555
beans = beans + 1;
5656
System.out.println(name + " took a bean.");
5757
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.mutex;
24+
25+
import org.junit.Test;
26+
import static org.junit.Assert.*;
27+
28+
/**
29+
* Test case for taking beans from a Jar
30+
*/
31+
public class JarTest {
32+
33+
@Test
34+
public void testTakeBeans() {
35+
Mutex mutex = new Mutex();
36+
Jar jar = new Jar(10, mutex);
37+
for (int i = 0; i < 10; i++) {
38+
assertTrue(jar.takeBean());
39+
}
40+
assertFalse(jar.takeBean());
41+
}
42+
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.mutex;
24+
25+
import org.junit.Test;
26+
import static org.junit.Assert.*;
27+
28+
/**
29+
* Test case for acquiring and releasing a Mutex
30+
*/
31+
public class MutexTest {
32+
33+
@Test
34+
public void acquireReleaseTest() {
35+
Mutex mutex = new Mutex();
36+
assertNull(mutex.getOwner());
37+
try {
38+
mutex.acquire();
39+
assertEquals(mutex.getOwner(), Thread.currentThread());
40+
} catch (InterruptedException e) {
41+
fail(e.toString());
42+
}
43+
mutex.release();
44+
assertNull(mutex.getOwner());
45+
}
46+
47+
}

semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,29 @@
2727
*/
2828
public class Semaphore implements Lock {
2929

30+
private final int licenses;
3031
/**
3132
* The number of concurrent resource accesses which are allowed.
3233
*/
3334
private int counter;
3435

35-
public Semaphore(int counter) {
36-
this.counter = counter;
36+
public Semaphore(int licenses) {
37+
this.licenses = licenses;
38+
this.counter = licenses;
39+
}
40+
41+
/**
42+
* Returns the number of licenses managed by the Semaphore
43+
*/
44+
public int getNumLicenses() {
45+
return licenses;
46+
}
47+
48+
/**
49+
* Returns the number of available licenses
50+
*/
51+
public int getAvailableLicenses() {
52+
return counter;
3753
}
3854

3955
/**
@@ -52,8 +68,10 @@ public synchronized void acquire() throws InterruptedException {
5268
* Method called by a thread to release the lock.
5369
*/
5470
public synchronized void release() {
55-
counter = counter + 1;
56-
notify();
71+
if (counter < licenses) {
72+
counter = counter + 1;
73+
notify();
74+
}
5775
}
5876

5977
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.semaphore;
24+
25+
import org.junit.Test;
26+
import static org.junit.Assert.*;
27+
28+
/**
29+
* Test taking from and putting Fruit into a FruitBowl
30+
*/
31+
public class FruitBowlTest {
32+
33+
@Test
34+
public void fruitBowlTest() {
35+
FruitBowl fbowl = new FruitBowl();
36+
37+
assertEquals(fbowl.countFruit(), 0);
38+
39+
for (int i = 1; i <= 10; i++) {
40+
fbowl.put(new Fruit(Fruit.FruitType.LEMON));
41+
assertEquals(fbowl.countFruit(), i);
42+
}
43+
44+
for (int i = 9; i >= 0; i--) {
45+
assertNotNull(fbowl.take());
46+
assertEquals(fbowl.countFruit(), i);
47+
}
48+
49+
assertNull(fbowl.take());
50+
}
51+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.semaphore;
24+
25+
import org.junit.Test;
26+
import static org.junit.Assert.*;
27+
28+
/**
29+
* Test case for acquiring and releasing a Semaphore
30+
*/
31+
public class SemaphoreTest {
32+
33+
@Test
34+
public void acquireReleaseTest() {
35+
Semaphore sphore = new Semaphore(3);
36+
37+
assertEquals(sphore.getAvailableLicenses(), 3);
38+
39+
for (int i = 2; i >= 0; i--) {
40+
try {
41+
sphore.acquire();
42+
assertEquals(sphore.getAvailableLicenses(), i);
43+
} catch (InterruptedException e) {
44+
fail(e.toString());
45+
}
46+
}
47+
48+
for (int i = 1; i <= 3; i++) {
49+
sphore.release();
50+
assertEquals(sphore.getAvailableLicenses(), i);
51+
}
52+
53+
sphore.release();
54+
assertEquals(sphore.getAvailableLicenses(), 3);
55+
}
56+
}

0 commit comments

Comments
 (0)