|
2 | 2 |
|
3 | 3 | import com.github.nylle.javafixture.annotations.testcases.TestCase; |
4 | 4 | import com.github.nylle.javafixture.annotations.testcases.TestWithCases; |
| 5 | +import com.github.nylle.javafixture.specimen.AbstractSpecimen; |
5 | 6 | import com.github.nylle.javafixture.specimen.ArraySpecimen; |
6 | 7 | import com.github.nylle.javafixture.specimen.CollectionSpecimen; |
7 | 8 | import com.github.nylle.javafixture.specimen.EnumSpecimen; |
|
16 | 17 | import com.github.nylle.javafixture.testobjects.TestEnum; |
17 | 18 | import com.github.nylle.javafixture.testobjects.TestObjectGeneric; |
18 | 19 | import com.github.nylle.javafixture.testobjects.TestPrimitive; |
| 20 | +import com.github.nylle.javafixture.testobjects.abstractclasses.AbstractClassWithAbstractImplementation; |
| 21 | +import com.github.nylle.javafixture.testobjects.abstractclasses.AbstractClassWithGenericImplementation; |
| 22 | +import com.github.nylle.javafixture.testobjects.abstractclasses.AbstractClassWithImplementation; |
| 23 | +import com.github.nylle.javafixture.testobjects.abstractclasses.AbstractClassWithoutImplementation; |
| 24 | +import com.github.nylle.javafixture.testobjects.abstractclasses.GenericAbstractClassTUWithGenericImplementationT; |
| 25 | +import com.github.nylle.javafixture.testobjects.abstractclasses.GenericAbstractClassTUWithGenericImplementationTU; |
| 26 | +import com.github.nylle.javafixture.testobjects.abstractclasses.GenericAbstractClassTUWithGenericImplementationU; |
| 27 | +import com.github.nylle.javafixture.testobjects.abstractclasses.GenericAbstractClassWithImplementation; |
19 | 28 | import com.github.nylle.javafixture.testobjects.example.IContract; |
20 | 29 | import com.github.nylle.javafixture.testobjects.interfaces.GenericInterfaceTUWithGenericImplementationT; |
21 | 30 | import com.github.nylle.javafixture.testobjects.interfaces.GenericInterfaceTUWithGenericImplementationTU; |
@@ -179,4 +188,92 @@ void ifGenericImplementationOnlyUsesSecondTypeArgumentOfGenericInterface() { |
179 | 188 | } |
180 | 189 | } |
181 | 190 | } |
| 191 | + |
| 192 | + @Nested |
| 193 | + @DisplayName("For abstract classes") |
| 194 | + class AbstractClasses { |
| 195 | + |
| 196 | + Context context = new Context(Configuration.configure().experimentalInterfaces(true)); |
| 197 | + |
| 198 | + @TestWithCases |
| 199 | + @TestCase(bool1 = true, class2 = ObjectSpecimen.class) |
| 200 | + @TestCase(bool1 = false, class2 = AbstractSpecimen.class) |
| 201 | + @DisplayName("only scans for sub-classes if experimentalInterfaces is") |
| 202 | + void abstractImplementationsAreOnlySupportedIfExperimentalInterfacesAreEnabled(boolean experimental, Class<?> expected) { |
| 203 | + var context = new Context(Configuration.configure().experimentalInterfaces(experimental)); |
| 204 | + |
| 205 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(AbstractClassWithImplementation.class))).isExactlyInstanceOf(expected); |
| 206 | + } |
| 207 | + |
| 208 | + @Nested |
| 209 | + @DisplayName("creates AbstractSpecimen if") |
| 210 | + class CreatesAbstractSpecimen { |
| 211 | + |
| 212 | + @Test |
| 213 | + @DisplayName("no subclasses found") |
| 214 | + void createsAbstractSpecimenIfNoSubclassFound() { |
| 215 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(AbstractClassWithoutImplementation.class))) |
| 216 | + .isExactlyInstanceOf(AbstractSpecimen.class); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + @DisplayName("only abstract subclasses found") |
| 221 | + void createsAbstractSpecimenIfOnlyAbstractSubclassesFound() { |
| 222 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(AbstractClassWithAbstractImplementation.class))) |
| 223 | + .isExactlyInstanceOf(AbstractSpecimen.class); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + @DisplayName("subclass is generic and superclass is not") |
| 228 | + void createsAbstractSpecimenIfSubclassIsGenericAndSuperclassIsNot() { |
| 229 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(AbstractClassWithGenericImplementation.class))) |
| 230 | + .isExactlyInstanceOf(AbstractSpecimen.class); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + @Nested |
| 235 | + @DisplayName("creates ObjectSpecimen if") |
| 236 | + class CreatesObjectSpecimen { |
| 237 | + |
| 238 | + @Test |
| 239 | + @DisplayName("subclass is not generic and superclass is not generic") |
| 240 | + void createsObjectSpecimenIfBothSubclassAndSuperclassAreNotGeneric() { |
| 241 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(AbstractClassWithImplementation.class))) |
| 242 | + .isExactlyInstanceOf(ObjectSpecimen.class); |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + @DisplayName("subclass is not generic and superclass is generic") |
| 247 | + void createsObjectSpecimenIfSubclassIsNotGenericAndSuperclassIs() { |
| 248 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericAbstractClassWithImplementation<Integer, String>>() {})) |
| 249 | + .isExactlyInstanceOf(ObjectSpecimen.class); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + @Nested |
| 254 | + @DisplayName("creates GenericSpecimen if") |
| 255 | + class CreatesGenericSpecimen { |
| 256 | + |
| 257 | + @Test |
| 258 | + @DisplayName("subclass is generic and superclass is generic") |
| 259 | + void createsGenericSpecimenIfSubclassAndSuperclassAreGeneric() { |
| 260 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericAbstractClassTUWithGenericImplementationTU<String, Integer>>() {})) |
| 261 | + .isExactlyInstanceOf(GenericSpecimen.class); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + @DisplayName("generic subclass only uses first type-argument of generic superclass") |
| 266 | + void createsGenericSpecimenIfGenericSubclassOnlyUsesFirstTypeArgumentOfGenericSuperclass() { |
| 267 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericAbstractClassTUWithGenericImplementationT<String, Integer>>() {})) |
| 268 | + .isExactlyInstanceOf(GenericSpecimen.class); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + @DisplayName("generic subclass only uses second type-argument of generic superclass") |
| 273 | + void createsGenericSpecimenIfGenericSubclassOnlyUsesSecondTypeArgumentOfGenericSuperclass() { |
| 274 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericAbstractClassTUWithGenericImplementationU<String, Integer>>() {})) |
| 275 | + .isExactlyInstanceOf(GenericSpecimen.class); |
| 276 | + } |
| 277 | + } |
| 278 | + } |
182 | 279 | } |
0 commit comments