Skip to content

Commit aa6542e

Browse files
committed
增加 原型模式 团队工厂 测试
1 parent 41fe610 commit aa6542e

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.prototype;
25+
26+
import org.junit.Test;
27+
28+
import static org.junit.Assert.assertNull;
29+
import static org.mockito.Mockito.*;
30+
31+
/**
32+
* 测试团队工厂
33+
*/
34+
public class FactoryTest {
35+
36+
@Test
37+
public void testFactory() throws Exception {
38+
final Driver driver = mock(Driver.class);
39+
final Passenger passenger = mock(Passenger.class);
40+
final Vehicle vehicle = mock(Vehicle.class);
41+
42+
when(driver.clone()).thenThrow(CloneNotSupportedException.class);
43+
when(passenger.clone()).thenThrow(CloneNotSupportedException.class);
44+
when(vehicle.clone()).thenThrow(CloneNotSupportedException.class);
45+
46+
final TeamFactoryImpl factory = new TeamFactoryImpl(driver, passenger, vehicle);
47+
assertNull(factory.createDriver());
48+
assertNull(factory.createPassenger());
49+
assertNull(factory.createVehicle());
50+
51+
verify(driver).clone();
52+
verify(passenger).clone();
53+
verify(vehicle).clone();
54+
verifyNoMoreInteractions(driver, passenger, vehicle);
55+
}
56+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.prototype;
25+
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.Parameterized;
29+
30+
import java.util.Arrays;
31+
import java.util.Collection;
32+
33+
import static org.junit.Assert.*;
34+
35+
/**
36+
* 测试原型模式
37+
*/
38+
@RunWith(Parameterized.class)
39+
public class PrototypeTest<P extends Prototype> {
40+
41+
@Parameterized.Parameters
42+
public static Collection<Object[]> setupData() {
43+
return Arrays.asList(
44+
new Object[]{new EstateDriver(), "这是一名旅行车司机"},
45+
new Object[]{new EstateVehicle(), "这是一辆旅行车"},
46+
new Object[]{new EstatePassanger(), "这是一名旅行车乘客"},
47+
new Object[]{new OffRoadDriver(), "这是一名越野车司机"},
48+
new Object[]{new OffRoadVehicle(), "这是一辆越野车"},
49+
new Object[]{new OffRoadPassanger(), "这是一名越野车乘客"}
50+
);
51+
}
52+
53+
private final P prototype;
54+
55+
private final String expectedString;
56+
57+
public PrototypeTest(P prototype, String expectedString) {
58+
this.prototype = prototype;
59+
this.expectedString = expectedString;
60+
}
61+
62+
@Test
63+
public void testPrototype() throws Exception {
64+
assertEquals(this.prototype.toString(), this.expectedString);
65+
final Object clone = this.prototype.clone();
66+
assertNotNull(clone);
67+
assertSame(clone.getClass(), this.prototype.getClass());
68+
assertNotSame(this.prototype, clone);
69+
}
70+
}

0 commit comments

Comments
 (0)