Skip to content

Commit 59c32d3

Browse files
committed
Add tests that run the examples
1 parent dc4f07e commit 59c32d3

14 files changed

Lines changed: 528 additions & 148 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.abstractfactory;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
27+
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
public class AbstractFactoryTest {
32+
33+
private App app = new App();
34+
private KingdomFactory elfFactory;
35+
private KingdomFactory orcFactory;
36+
37+
@Before
38+
public void setUp() {
39+
elfFactory = app.getElfKingdomFactory();
40+
orcFactory = app.getOrcKingdomFactory();
41+
}
42+
43+
@Test
44+
public void king() {
45+
final King elfKing = app.getKing(elfFactory);
46+
assertTrue(elfKing instanceof ElfKing);
47+
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
48+
final King orcKing = app.getKing(orcFactory);
49+
assertTrue(orcKing instanceof OrcKing);
50+
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
51+
}
52+
53+
@Test
54+
public void castle() {
55+
final Castle elfCastle = app.getCastle(elfFactory);
56+
assertTrue(elfCastle instanceof ElfCastle);
57+
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
58+
final Castle orcCastle = app.getCastle(orcFactory);
59+
assertTrue(orcCastle instanceof OrcCastle);
60+
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
61+
}
62+
63+
@Test
64+
public void army() {
65+
final Army elfArmy = app.getArmy(elfFactory);
66+
assertTrue(elfArmy instanceof ElfArmy);
67+
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
68+
final Army orcArmy = app.getArmy(orcFactory);
69+
assertTrue(orcArmy instanceof OrcArmy);
70+
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
71+
}
72+
73+
@Test
74+
public void createElfKingdom() {
75+
app.createKingdom(elfFactory);
76+
final King king = app.getKing();
77+
final Castle castle = app.getCastle();
78+
final Army army = app.getArmy();
79+
assertTrue(king instanceof ElfKing);
80+
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
81+
assertTrue(castle instanceof ElfCastle);
82+
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
83+
assertTrue(army instanceof ElfArmy);
84+
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
85+
}
86+
87+
@Test
88+
public void createOrcKingdom() {
89+
app.createKingdom(orcFactory);
90+
final King king = app.getKing();
91+
final Castle castle = app.getCastle();
92+
final Army army = app.getArmy();
93+
assertTrue(king instanceof OrcKing);
94+
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
95+
assertTrue(castle instanceof OrcCastle);
96+
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
97+
assertTrue(army instanceof OrcArmy);
98+
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
99+
}
100+
}

abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -22,79 +22,17 @@
2222
*/
2323
package com.iluwatar.abstractfactory;
2424

25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertTrue;
27-
28-
import org.junit.Before;
2925
import org.junit.Test;
3026

31-
public class AppTest {
32-
33-
private App app = new App();
34-
private KingdomFactory elfFactory;
35-
private KingdomFactory orcFactory;
36-
37-
@Before
38-
public void setUp() {
39-
elfFactory = app.getElfKingdomFactory();
40-
orcFactory = app.getOrcKingdomFactory();
41-
}
42-
43-
@Test
44-
public void king() {
45-
final King elfKing = app.getKing(elfFactory);
46-
assertTrue(elfKing instanceof ElfKing);
47-
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
48-
final King orcKing = app.getKing(orcFactory);
49-
assertTrue(orcKing instanceof OrcKing);
50-
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
51-
}
52-
53-
@Test
54-
public void castle() {
55-
final Castle elfCastle = app.getCastle(elfFactory);
56-
assertTrue(elfCastle instanceof ElfCastle);
57-
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
58-
final Castle orcCastle = app.getCastle(orcFactory);
59-
assertTrue(orcCastle instanceof OrcCastle);
60-
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
61-
}
62-
63-
@Test
64-
public void army() {
65-
final Army elfArmy = app.getArmy(elfFactory);
66-
assertTrue(elfArmy instanceof ElfArmy);
67-
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
68-
final Army orcArmy = app.getArmy(orcFactory);
69-
assertTrue(orcArmy instanceof OrcArmy);
70-
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
71-
}
72-
73-
@Test
74-
public void createElfKingdom() {
75-
app.createKingdom(elfFactory);
76-
final King king = app.getKing();
77-
final Castle castle = app.getCastle();
78-
final Army army = app.getArmy();
79-
assertTrue(king instanceof ElfKing);
80-
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
81-
assertTrue(castle instanceof ElfCastle);
82-
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
83-
assertTrue(army instanceof ElfArmy);
84-
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
85-
}
27+
import java.io.IOException;
8628

29+
/**
30+
* Tests that Abstract Factory example runs without errors.
31+
*/
32+
public class AppTest {
8733
@Test
88-
public void createOrcKingdom() {
89-
app.createKingdom(orcFactory);
90-
final King king = app.getKing();
91-
final Castle castle = app.getCastle();
92-
final Army army = app.getArmy();
93-
assertTrue(king instanceof OrcKing);
94-
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
95-
assertTrue(castle instanceof OrcCastle);
96-
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
97-
assertTrue(army instanceof OrcArmy);
98-
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
34+
public void test() throws IOException {
35+
String[] args = {};
36+
App.main(args);
9937
}
10038
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.adapter;
24+
25+
import org.junit.Test;
26+
27+
import java.io.IOException;
28+
29+
/**
30+
* Tests that Adapter example runs without errors.
31+
*/
32+
public class AppTest {
33+
@Test
34+
public void test() throws IOException {
35+
String[] args = {};
36+
App.main(args);
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.business.delegate;
24+
25+
import org.junit.Test;
26+
27+
import java.io.IOException;
28+
29+
/**
30+
* Tests that Business Delegate example runs without errors.
31+
*/
32+
public class AppTest {
33+
@Test
34+
public void test() throws IOException {
35+
String[] args = {};
36+
App.main(args);
37+
}
38+
}

caching/src/test/java/com/iluwatar/caching/AppTest.java

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,17 @@
2222
*/
2323
package com.iluwatar.caching;
2424

25-
import org.junit.Before;
2625
import org.junit.Test;
2726

27+
import java.io.IOException;
28+
2829
/**
29-
*
30-
* Application test
31-
*
30+
* Tests that Caching example runs without errors.
3231
*/
3332
public class AppTest {
34-
App app;
35-
36-
/**
37-
* Setup of application test includes: initializing DB connection and cache size/capacity.
38-
*/
39-
@Before
40-
public void setUp() {
41-
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
42-
// to avoid Maven compilation errors. Set flag to true to run the
43-
// tests with MongoDB (provided that MongoDB is installed and socket
44-
// connection is open).
45-
AppManager.initCacheCapacity(3);
46-
app = new App();
47-
}
48-
49-
@Test
50-
public void testReadAndWriteThroughStrategy() {
51-
app.useReadAndWriteThroughStrategy();
52-
}
53-
54-
@Test
55-
public void testReadThroughAndWriteAroundStrategy() {
56-
app.useReadThroughAndWriteAroundStrategy();
57-
}
58-
5933
@Test
60-
public void testReadThroughAndWriteBehindStrategy() {
61-
app.useReadThroughAndWriteBehindStrategy();
34+
public void test() throws IOException {
35+
String[] args = {};
36+
App.main(args);
6237
}
6338
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.caching;
24+
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
/**
29+
*
30+
* Application test
31+
*
32+
*/
33+
public class CachingTest {
34+
App app;
35+
36+
/**
37+
* Setup of application test includes: initializing DB connection and cache size/capacity.
38+
*/
39+
@Before
40+
public void setUp() {
41+
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
42+
// to avoid Maven compilation errors. Set flag to true to run the
43+
// tests with MongoDB (provided that MongoDB is installed and socket
44+
// connection is open).
45+
AppManager.initCacheCapacity(3);
46+
app = new App();
47+
}
48+
49+
@Test
50+
public void testReadAndWriteThroughStrategy() {
51+
app.useReadAndWriteThroughStrategy();
52+
}
53+
54+
@Test
55+
public void testReadThroughAndWriteAroundStrategy() {
56+
app.useReadThroughAndWriteAroundStrategy();
57+
}
58+
59+
@Test
60+
public void testReadThroughAndWriteBehindStrategy() {
61+
app.useReadThroughAndWriteBehindStrategy();
62+
}
63+
}

0 commit comments

Comments
 (0)