View Javadoc
1   package guru.mikelue.jdut.annotation;
2   
3   import org.junit.jupiter.params.ParameterizedTest;
4   import org.junit.jupiter.params.provider.Arguments;
5   import org.junit.jupiter.params.provider.MethodSource;
6   
7   import static org.junit.jupiter.api.Assertions.*;
8   import static org.junit.jupiter.params.provider.Arguments.*;
9   
10  import guru.mikelue.jdut.test.AbstractDataSourceTestBase;
11  import guru.mikelue.jdut.vendor.DatabaseVendor;
12  import guru.mikelue.jdut.yaml.YamlConductorFactory;
13  
14  public class AnnotationUtilTest extends AbstractDataSourceTestBase {
15  	public AnnotationUtilTest() {}
16  
17  	/**
18  	 * Tests the matching for annotation {@link IfDatabaseVendor}.
19  	 */
20  	@ParameterizedTest
21  	@MethodSource
22  	public void matchDatabaseVendor(
23  		String sampleMethodName, DatabaseVendor checkedVendor,
24  		boolean expectedResult
25  	) throws NoSuchMethodException {
26  		IfDatabaseVendor sampleValueOfAnnotation = SampleForIfDatabaseVendor.class.getMethod(sampleMethodName)
27  			.getAnnotation(IfDatabaseVendor.class);
28  
29  		assertEquals(
30  			expectedResult,
31  			AnnotationUtil.matchDatabaseVendor(
32  				checkedVendor, sampleValueOfAnnotation
33  			)
34  		);
35  	}
36  	static Arguments[] matchDatabaseVendor()
37  	{
38  		return new Arguments[] {
39  			arguments("nullMethod", DatabaseVendor.H2, true),
40  			arguments("defaultMethod", DatabaseVendor.H2, true),
41  			arguments("matchOne", DatabaseVendor.H2, true),
42  			arguments("matchOne", DatabaseVendor.MySql, false),
43  			arguments("notMatchOne", DatabaseVendor.H2, false),
44  			arguments("notMatchOne", DatabaseVendor.MySql, true),
45  			arguments("contradict", DatabaseVendor.H2, false),
46  			arguments("contradict", DatabaseVendor.MySql, false),
47  			arguments("multiple", DatabaseVendor.Oracle, true),
48  			arguments("multiple", DatabaseVendor.H2, false),
49  			arguments("multiple", DatabaseVendor.PostgreSql, false),
50  		};
51  	}
52  
53  	/**
54  	 * Tests the building of duet conductor by convention.
55  	 */
56  	@ParameterizedTest
57  	@MethodSource
58  	public void buildConductorByConventionAndMethod(
59  		String sampleMethodName,
60  		boolean hasConductor
61  	) throws NoSuchMethodException {
62  		YamlConductorFactory sampleFactory = YamlConductorFactory.build(getDataSource());
63  
64  		assertEquals(
65  			hasConductor,
66  			AnnotationUtil.buildConductorByConvention(
67  				sampleFactory, AnnotationUtilTest.class.getMethod(sampleMethodName)
68  			).isPresent()
69  		);
70  	}
71  	static Arguments[] buildConductorByConventionAndMethod()
72  	{
73  		return new Arguments[] {
74  			arguments("withJdutResource", true),
75  			arguments("withoutJdutResource", false),
76  		};
77  	}
78  
79  	@JdutResource
80  	public void withJdutResource() {}
81  	public void withoutJdutResource() {}
82  
83  	/**
84  	 * Tests the building of duet conductor by convention.
85  	 */
86  	@ParameterizedTest
87  	@MethodSource
88  	public void buildConductorByConventionAndClass(
89  		Class<?> sampleClass,
90  		boolean hasConductor
91  	) {
92  		YamlConductorFactory sampleFactory = YamlConductorFactory.build(getDataSource());
93  
94  		assertEquals(
95  			hasConductor,
96  			AnnotationUtil.buildConductorByConvention(
97  				sampleFactory, sampleClass
98  			).isPresent()
99  		);
100 	}
101 	static Arguments[] buildConductorByConventionAndClass()
102 	{
103 		return new Arguments[] {
104 			arguments(WithAnnotation.class, true),
105 			arguments(WithoutAnnotation.class, false)
106 		};
107 	}
108 }
109 
110 @JdutResource
111 class WithAnnotation {}
112 class WithoutAnnotation {}
113 
114 interface SampleForIfDatabaseVendor {
115 	void nullMethod();
116 	@IfDatabaseVendor
117 	void defaultMethod();
118 	@IfDatabaseVendor(match=DatabaseVendor.H2)
119 	void matchOne();
120 	@IfDatabaseVendor(notMatch=DatabaseVendor.H2)
121 	void notMatchOne();
122 	@IfDatabaseVendor(match=DatabaseVendor.H2, notMatch=DatabaseVendor.H2)
123 	void contradict();
124 	@IfDatabaseVendor(match={DatabaseVendor.Oracle, DatabaseVendor.MsSql}, notMatch={DatabaseVendor.H2, DatabaseVendor.Derby})
125 	void multiple();
126 }