com.openpojo.reflection.PojoClass Java Examples
The following examples show how to use
com.openpojo.reflection.PojoClass.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void multipleAnnotationsShouldBeReturned() { final Class<?> aClassWithAnnotations = AClassWithAnnotations.class; final PojoClass pojoClass = getPojoClassImplForClass(aClassWithAnnotations); Affirm.affirmEquals(String.format("Annotations added/removed from Class=[%s]", aClassWithAnnotations), 2, pojoClass .getAnnotations().size()); final List<Class<?>> expectedAnnotations = new LinkedList<Class<?>>(); expectedAnnotations.add(SomeAnnotation.class); expectedAnnotations.add(AnotherAnnotation.class); for (final Annotation annotation : pojoClass.getAnnotations()) { Affirm.affirmTrue(String.format("Expected annotations [%s] not found, instead found [%s]", expectedAnnotations, annotation.annotationType()), expectedAnnotations.contains(annotation.annotationType())); } }
Example #2
Source File: DefaultValidatorTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void rulesAndTestersAreTriggeredWhenValidationIsRunRecursively() { TesterSpy testerSpy = new TesterSpy(); List<Tester> testers = new ArrayList<Tester>(); testers.add(testerSpy); RuleSpy ruleSpy = new RuleSpy(); List<Rule> rules = new ArrayList<Rule>(); rules.add(ruleSpy); FilterSpy filterSpy = new FilterSpy(); DefaultValidator defaultValidator = new DefaultValidator(rules, testers); String packageName = this.getClass().getPackage().getName() + ".sample"; List<PojoClass> validatedPojoClasses = defaultValidator.validateRecursively(packageName, filterSpy); Assert.assertEquals(2, validatedPojoClasses.size()); Assert.assertTrue(validatedPojoClasses.contains(PojoClassFactory.getPojoClass(DummyClass.class))); Assert.assertTrue(validatedPojoClasses.contains(PojoClassFactory.getPojoClass(AnotherDummyClass.class))); assertInvokedClasses(testerSpy.getInvocations(), DummyClass.class.getName(), AnotherDummyClass.class.getName()); assertInvokedClasses(ruleSpy.getInvocations(), DummyClass.class.getName(), AnotherDummyClass.class.getName()); assertInvokedClasses(filterSpy.getInvocations(), DummyClass.class.getName(), AnotherDummyClass.class.getName()); }
Example #3
Source File: NoFieldShadowingRuleTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void aChildWithFieldShadowingShouldFail() { PojoClass aChildWithFieldShadowing = PojoClassFactory.getPojoClass(NoShadowAChildWithFieldShadowing.class); try { validator.validate(aChildWithFieldShadowing); Assert.fail("Expected [NoShadowAChildWithFieldShadowing.class] to fail NoFieldShadowRule but didn't"); } catch (AssertionError ae) { Assert.assertEquals("Field=[PojoFieldImpl " + "[field=private java.lang.String com.openpojo.validation.rule.impl.sampleclasses.NoShadowAChildWithFieldShadowing.aField, " + "fieldGetter=null, " + "fieldSetter=null]] " + "shadows field with the same name in " + "parent class=[[PojoFieldImpl [" + "field=private java.lang.String com.openpojo.validation.rule.impl.sampleclasses.NoShadowAParentClassWithOneField.aField, " + "fieldGetter=null," + " fieldSetter=null]]]", ae.getMessage()); } }
Example #4
Source File: PojoParameterImplTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void isParameterized() { PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithParameterizedConstructors.class); Assert.assertTrue(pojoClass.isNestedClass()); for (PojoMethod constructor : pojoClass.getPojoConstructors()) { if (!constructor.isSynthetic()) { List<PojoParameter> pojoParameters = constructor.getPojoParameters(); Assert.assertThat(pojoParameters.size(), is(greaterThan(1))); for (int i = 1; i < pojoParameters.size(); i++) { PojoParameter parameter = pojoParameters.get(i); Assert.assertThat(parameter.isParameterized(), is(Matchers.equalTo(true))); } } } }
Example #5
Source File: DefaultRandomGenerator.java From openpojo with Apache License 2.0 | 6 votes |
public Object doGenerate(final Class<?> type) { final PojoClass typePojoClass = PojoClassFactory.getPojoClass(type); if (typePojoClass.isInterface()) { return interfaceRandomGenerator.doGenerate(type); } if (typePojoClass.isEnum()) { return enumRandomGenerator.doGenerate(type); } if (typePojoClass.isArray()) { return arrayRandomGenerator.doGenerate(type); } LoggerFactory.getLogger(DefaultRandomGenerator.class).debug("Creating basic instance for type=[{0}] using " + "InstanceFactory", type); return InstanceFactory.getLeastCompleteInstance(PojoClassFactory.getPojoClass(type)); }
Example #6
Source File: AbstractUnitTest.java From cia with Apache License 2.0 | 6 votes |
/** * Check all classes in package. * * @param string the string * @return true, if successful */ protected final boolean checkAllClassesInPackage(final String string) { final List<PojoClass> pojoClassesRecursively = PojoClassFactory.getPojoClassesRecursively(string, new FilterTestClasses()); final Validator validator = ValidatorBuilder.create().with(new SetterMustExistRule(), new GetterMustExistRule()) .with(new SetterTester(), new GetterTester()).with(new InvokeToStringTester()) .with(new InvokeHashcodeTester()).with(new DummyEqualsTester()).with(new WithTester()) .with(new ObjectFactoryTester()).with(new EqualsAndHashCodeMatchRule()).build(); validator.validate(pojoClassesRecursively); final List<PojoClass> enumClassesRecursively = PojoClassFactory.getPojoClassesRecursively(string, new FilterNonEnumClasses()); final Validator enumValidator = ValidatorBuilder.create().with(new EnumTester()).build(); enumValidator.validate(enumClassesRecursively); return true; }
Example #7
Source File: ParallecPojoClassTest.java From parallec with Apache License 2.0 | 6 votes |
/** * Unit Test the POJO classes. * * @throws ClassNotFoundException * the class not found exception * @throws InstantiationException * the instantiation exception * @throws IllegalAccessException * the illegal access exception */ @Test public void testPojoStructureAndBehavior() throws ClassNotFoundException, InstantiationException, IllegalAccessException { final PojoValidator pojoValidator = new PojoValidator(); for (final Class<? extends Rule> ruleClass : getValidationRules()) { final Rule rule = ruleClass.newInstance(); pojoValidator.addRule(rule); } // Load tester classes for (Class<? extends Tester> testerClass : getTesters()) { final Tester testerInstance = testerClass.newInstance(); pojoValidator.addTester(testerInstance); } for (final Class<?> c : getPOJOClasses()) { final PojoClass pojoClass = PojoClassFactory.getPojoClass(c); pojoValidator.runValidation(pojoClass); } }
Example #8
Source File: FilterNonConcreteTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void testInclude() { PojoClassFilter pojoClassFilter = new FilterNonConcrete(); PojoClass stubPojoClass = PojoStubFactory.getStubPojoClass(false); Affirm.affirmTrue(String.format("Filter[%s] was supposed to filter OUT non concrete class", pojoClassFilter), stubPojoClass .isConcrete() == pojoClassFilter.include(stubPojoClass)); stubPojoClass = PojoStubFactory.getStubPojoClass(true); Affirm.affirmTrue(String.format("Filter[%s] was supposed to filter IN concrete class", pojoClassFilter), stubPojoClass.isConcrete() == pojoClassFilter.include(stubPojoClass)); final StubPojoClassFilter stubPojoClassFilter = new StubPojoClassFilter(); pojoClassFilter = new FilterChain(new FilterNonConcrete(), stubPojoClassFilter); stubPojoClass = PojoStubFactory.getStubPojoClass(true); pojoClassFilter.include(stubPojoClass); Affirm.affirmTrue(String.format("Filter [%s] didn't invoke next in filter chain", pojoClassFilter), stubPojoClassFilter .includeCalled); }
Example #9
Source File: ValidationHelperTest.java From openpojo with Apache License 2.0 | 6 votes |
@Test public void shouldReportMissingASMProperly() { Validator validator = ValidatorBuilder.create() .with(new Tester() { public void run(PojoClass pojoClass) { throw ASMNotLoadedException.getInstance(); } }).build(); LogHelper.initialize(MockAppenderLog4J.class); validator.validate(PojoClassFactory.getPojoClass(this.getClass())); List<LogEvent> warnEvents = LogHelper.getWarnEvents(MockAppenderLog4J.class, DefaultValidator.class.getName()); Assert.assertEquals(1, warnEvents.size()); String expectedMessage = "ASM not loaded while attempting to execute behavioural tests on non-constructable class[" + this.getClass() + "], either filter abstract classes or add asm to your classpath."; Assert.assertEquals(expectedMessage, warnEvents.get(0).getMessage()); }
Example #10
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void isProtectedClass() { PojoClass pojoclass = getClass(SAMPLE_CLASSES_PKG + ".AccessibilityClass$ProtectedClass"); Affirm.affirmNotNull("class not found", pojoclass); Affirm.affirmTrue("isProtected() check on class=[" + pojoclass + "] returned false!!", pojoclass.isProtected()); Affirm.affirmFalse("isPrivate() check on class=[" + pojoclass + "] returned true!!", pojoclass.isPrivate()); Affirm.affirmFalse("isPackagePrivate() check on class=[" + pojoclass + "] returned true!!", pojoclass.isPackagePrivate()); Affirm.affirmFalse("isPublic() check on class=[" + pojoclass + "] returned true!!", pojoclass.isPublic()); }
Example #11
Source File: PojoAbstractMethodImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldfindOneAbstractMethod() { PojoClass pojoClass = PojoClassFactory.getPojoClass(AnAbstractClassWithOneAbstractMethod.class); for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) { if (!pojoMethod.isConstructor()) Assert.assertTrue(pojoMethod.isAbstract()); } Assert.assertEquals(1 + 1 /* constructor */, pojoClass.getPojoMethods().size()); }
Example #12
Source File: InstanceFactory.java From openpojo with Apache License 2.0 | 5 votes |
private static PojoMethod getConstructorByCriteria(final PojoClass pojoClass, final ArrayLengthBasedComparator comparator) { PojoMethod constructor = null; for (final PojoMethod pojoConstructor : pojoClass.getPojoConstructors()) { if (!pojoConstructor.isSynthetic() && !(pojoClass.isAbstract() && pojoConstructor.isPrivate())) if (constructor == null) constructor = pojoConstructor; else { if (comparator.compare(pojoConstructor.getParameterTypes(), constructor.getParameterTypes())) constructor = pojoConstructor; } } return constructor; }
Example #13
Source File: CoberturaPojoClassAdapter.java From openpojo with Apache License 2.0 | 5 votes |
public PojoClass adapt(PojoClass pojoClass) { final List<PojoField> cleansedPojoFields = new ArrayList<PojoField>(); for (final PojoField pojoField : pojoClass.getPojoFields()) { if (!pojoField.getName().startsWith(COBERTURA_INJECTED)) { cleansedPojoFields.add(pojoField); } } final List<PojoMethod> cleansedPojoMethods = new ArrayList<PojoMethod>(); for (final PojoMethod pojoMethod : pojoClass.getPojoMethods()) { if (!pojoMethod.getName().startsWith(COBERTURA_INJECTED)) { cleansedPojoMethods.add(pojoMethod); } } return new PojoClassImpl(pojoClass.getClazz(), cleansedPojoFields, cleansedPojoMethods); }
Example #14
Source File: PojoMethodImplGenericParametersTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldGetConstructorWithGenericParameter() { PojoClass pojoClass = PojoClassFactory.getPojoClass(AClassWithGenericParameterConstructor.class); List<PojoMethod> constructors = pojoClass.getPojoConstructors(); Affirm.affirmEquals(pojoClass.getName() + " should have only one generic parameterized constructor", 1, constructors.size()); shouldHaveOneParameterizedParameter(constructors, String.class); }
Example #15
Source File: TestClassMustEndWithRuleTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test(expected = AssertionError.class) public void aClassThatHasTestSuiteButDoesntEndWithTestCaseShouldFailValidation() { Class<?> testClass = ASMService.getInstance().createSubclassFor(this.getClass(), new DefaultSubClassDefinition(this.getClass(), "AClassTestCaseAndSomethingElse")); PojoClass aBadTestClassPojo = PojoClassFactory.getPojoClass(testClass); testClassMustEndWithRule.evaluate(aBadTestClassPojo); }
Example #16
Source File: DefaultPojoClassLookupService.java From openpojo with Apache License 2.0 | 5 votes |
public PojoClass getPojoClass(final Class<?> clazz) { PojoClass pojoClass = PojoCache.getPojoClass(clazz.getName()); if (pojoClass == null) { try { pojoClass = new PojoClassImpl(clazz, PojoFieldFactory.getPojoFields(clazz), PojoMethodFactory.getPojoMethods(clazz)); pojoClass = ServiceRegistrar.getInstance().getPojoCoverageFilterService().adapt(pojoClass); } catch (LinkageError le) { if (clazz.getName().endsWith(GENERATED_CLASS_POSTFIX)) throw le; LoggerFactory.getLogger(this.getClass()).warn("Failed to load class [{0}], exception [{1}]", clazz, le); } PojoCache.addPojoClass(clazz.getName(), pojoClass); } return pojoClass; }
Example #17
Source File: JacocoPojoClassAdapter.java From openpojo with Apache License 2.0 | 5 votes |
public PojoClass adapt(final PojoClass pojoClass) { final List<PojoField> cleansedPojoFields = new LinkedList<PojoField>(); for (final PojoField pojoField : pojoClass.getPojoFields()) { if (!pojoField.getName().equals(JACOCO_FIELD_NAME)) { cleansedPojoFields.add(pojoField); } } final List<PojoMethod> cleansedPojoMethods = new LinkedList<PojoMethod>(); for (final PojoMethod pojoMethod : pojoClass.getPojoMethods()) { if (!pojoMethod.getName().equals(JACOCO_METHOD_NAME)) { cleansedPojoMethods.add(pojoMethod); } } return new PojoClassImpl(pojoClass.getClazz(), cleansedPojoFields, cleansedPojoMethods); }
Example #18
Source File: GetterTesterAndSetterTesterTest.java From openpojo with Apache License 2.0 | 5 votes |
private List<PojoClass> getGoodPojoClasses() { return PojoClassFactory.getPojoClasses(TESTPACKAGE, new PojoClassFilter() { public boolean include(PojoClass pojoClass) { return pojoClass.getClazz().getSimpleName().startsWith("Good_"); } }); }
Example #19
Source File: IssueTest.java From openpojo with Apache License 2.0 | 5 votes |
/** * This is the main issue experienced. */ @Test public void shouldNotThrowNoClassDefFoundError() { final Package aPackage = org.testng.Assert.class.getPackage(); final String packageName = aPackage.getName(); List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively(packageName, null); Assert.assertTrue("Should have found some classes", pojoClasses.size() > 0); }
Example #20
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void testGetAnnotations() { final Class<?> aClassWithAnnotations = AClassWithAnnotations.class; final PojoClass pojoClass = getPojoClassImplForClass(aClassWithAnnotations); Affirm.affirmEquals(String.format("Annotations added/removed from Class=[%s]", aClassWithAnnotations), 2, pojoClass .getAnnotations().size()); }
Example #21
Source File: ByteCodeFactoryTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void givenAnAbstractClassWithConstructorShouldConstruct() { PojoClass pojoClass = PojoClassFactory.getPojoClass(AnAbstractClassWithConstructor.class); Class<?> subClass = getSubClass(pojoClass.getClazz()); assertIsConcreteAndConstructable(subClass); }
Example #22
Source File: IssueTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void ensureEnumStaysIntact() { PojoClass pojoClass = PojoClassFactory.getPojoClass(SomeEnumWithValuesMethod.class); boolean valuesMethodExists = false; for (PojoMethod pojoMethod : pojoClass.getPojoMethods()) { if (pojoMethod.getName().equals("values") && pojoMethod.getPojoParameters().size() > 0 && pojoMethod.isStatic()) valuesMethodExists = true; } Affirm.affirmTrue("values method must exist, Enum Class Changed?!", valuesMethodExists); }
Example #23
Source File: IssueTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldHideJacocoFieldAndMethod() throws NoSuchFieldException, NoSuchMethodException { Field field = this.getClass().getDeclaredField(JACOCO_FIELD_NAME); Assert.assertNotNull("Should not be null", field); Method method = this.getClass().getDeclaredMethod(JACOCO_METHOD_NAME); Assert.assertNotNull("Should not be null", method); PojoClassAdapter jacocoPojoClassAdapter = JacocoPojoClassAdapter.getInstance(); PojoClass cleansedPojoClass = jacocoPojoClassAdapter.adapt(PojoClassFactory.getPojoClass(this.getClass())); for (PojoField pojoField : cleansedPojoClass.getPojoFields()) { if (pojoField.getName().equals(JACOCO_FIELD_NAME)) { Affirm.fail(JACOCO_FIELD_NAME + " field is still visible!!"); } } for (PojoMethod pojoMethod : cleansedPojoClass.getPojoMethods()) { if (pojoMethod.getName().equals(JACOCO_METHOD_NAME)) { Affirm.fail(JACOCO_METHOD_NAME + " method is still visible!!"); } } Assert.assertNotNull(this.getClass().getDeclaredField("JACOCO_FIELD_NAME")); Assert.assertNotNull(this.getClass().getDeclaredField("JACOCO_METHOD_NAME")); Assert.assertNotNull(this.getClass().getDeclaredMethod("shouldHideJacocoFieldAndMethod")); }
Example #24
Source File: PojoPackageTestBase.java From spring-batch-lightmin with Apache License 2.0 | 5 votes |
@Test public void ensureExpectedPojoCount() { if (this.withExpectedCount) { final List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses(this.pojoPackage, new FilterPackageInfo()); Affirm.affirmEquals("Classes added / removed?", this.expectedClassCount, pojoClasses.size()); } else { log.debug("WithExpectedCount is disbabled"); } }
Example #25
Source File: EqualsAndHashCodeMatchRuleTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void shouldPassWhenEqualsAndHashCodeAreImplemented() { PojoClass aClassImplementingEqualsAndHashcode = PojoClassFactory.getPojoClass(AClassImplementingEqualsAndHashCode.class); List<PojoMethod> methods = aClassImplementingEqualsAndHashcode.getPojoMethods(); Assert.assertEquals(3, methods.size()); boolean constructorFound = false; boolean equalsFound = false; boolean hashCodeFound = false; for (PojoMethod method : methods) { if (method.isConstructor()) constructorFound = true; if (method.getName().equals("hashCode") && method.getPojoParameters().size() == 0 && method.getReturnType().equals(int.class)) hashCodeFound = true; if (method.getName().equals("equals") && method.getPojoParameters().size() == 1 && method.getReturnType().equals(boolean.class)) equalsFound = true; } Assert.assertTrue("Constructor not found", constructorFound); Assert.assertTrue("Equals not found", equalsFound); Assert.assertTrue("hashCode not found", hashCodeFound); rule.evaluate(aClassImplementingEqualsAndHashcode); }
Example #26
Source File: ModelTest.java From cloudbreak with Apache License 2.0 | 5 votes |
@Test public void testPojoStructureAndBehavior() { List<PojoClass> pojoClasses = PojoClassFactory.getPojoClassesRecursively(DOMAIN_PACKAGE, POJO_CLASS_FILTER); Validator validator = ValidatorBuilder.create() .with(new SetterMustExistRule(), new GetterMustExistRule()) .with(new SetterTester(), new GetterTester()) .with(new NoStaticExceptFinalRule()) .with(new NoNestedClassRule()) .build(); validator.validate(pojoClasses); }
Example #27
Source File: URIRandomGeneratorTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void constructorIsPrivate() { PojoClass uriPojoClass = PojoClassFactory.getPojoClass(URIRandomGenerator.class); for (PojoMethod constructor : uriPojoClass.getPojoConstructors()) { if (!constructor.isSynthetic()) assertTrue(constructor + " should be private", constructor.isPrivate()); } }
Example #28
Source File: PojoClassImplTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void isPrivateClass() { PojoClass pojoclass = getClass(SAMPLE_CLASSES_PKG + ".AccessibilityClass$PrivateClass"); Affirm.affirmNotNull("class not found", pojoclass); Affirm.affirmTrue("isPrivate() check on class=[" + pojoclass + "] returned true!!", pojoclass.isPrivate()); Affirm.affirmFalse("isPackagePrivate() check on class=[" + pojoclass + "] returned true!!", pojoclass.isPackagePrivate()); Affirm.affirmFalse("isProtected() check on class=[" + pojoclass + "] returned true!!", pojoclass.isProtected()); Affirm.affirmFalse("isPublic() check on class=[" + pojoclass + "] returned true!!", pojoclass.isPublic()); }
Example #29
Source File: ByteCodeFactoryTest.java From openpojo with Apache License 2.0 | 5 votes |
@Test public void givenAnAbstractClassWithAnAbstractMethodShouldReturnAnInstance() { PojoClass pojoClass = PojoClassFactory.getPojoClass(AnAbstractClassWithOneAbstraceMethod.class); Assert.assertEquals("Should have 1 constructor and 1 abstract method", 2, pojoClass.getPojoMethods().size()); Class<?> subclass = getSubClass(pojoClass.getClazz()); assertNotNull(subclass); assertIsSubclass(pojoClass.getClazz(), subclass); }
Example #30
Source File: PojoTest.java From moleculer-java with MIT License | 5 votes |
public boolean include(PojoClass pojoClass) { boolean enable = !pojoClass.getName().contains("Test") && !pojoClass.getName().contains("$"); if (enable) { // System.out.println(pojoClass.getName()); } return enable; }