|
| 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