Java Code Examples for com.sun.codemodel.JFieldVar#annotate()
The following examples show how to use
com.sun.codemodel.JFieldVar#annotate() .
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: ClassFieldDeclarationRule.java From springmvc-raml-plugin with Apache License 2.0 | 6 votes |
@Override public JFieldVar apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) { JFieldVar field = generatableType.field(JMod.PRIVATE, this.fieldClazz, this.fieldName); // add @Autowired field annoation if (autowire) { field.annotate(Autowired.class); } // add @Qualifier("qualifierBeanName") if (qualifierAnnotation && !StringUtils.isEmpty(qualifier)) { field.annotate(Qualifier.class).param("value", qualifier); } // add @Value(value="") annotation to the field if (valueAnnotation) { field.annotate(Value.class).param("value", valueAnnotationValue); } return field; }
Example 2
Source File: AnnotationInspectorTest.java From jpa-unit with Apache License 2.0 | 6 votes |
@BeforeClass public static void generateModel() throws Exception { final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); JAnnotationUse jAnnotationUse = jClass.annotate(InitialDataSets.class); jAnnotationUse.param("value", "Script.file"); jClass.annotate(Cleanup.class); final JFieldVar jField = jClass.field(JMod.PRIVATE, String.class, "testField"); jField.annotate(PersistenceContext.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jAnnotationUse = jMethod.annotate(InitialDataSets.class); jAnnotationUse.param("value", "InitialDataSets.file"); jAnnotationUse = jMethod.annotate(ApplyScriptsAfter.class); jAnnotationUse.param("value", "ApplyScriptsAfter.file"); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); cut = loadClass(testFolder.getRoot(), jClass.name()); }
Example 3
Source File: JpaUnitRunnerTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testJpaUnitRunnerAndJpaUnitRuleFieldExcludeEachOther() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class); jAnnotationUse.param("value", JpaUnitRunner.class); final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf"); final JAnnotationUse jAnnotation = emField.annotate(PersistenceUnit.class); jAnnotation.param("unitName", "test-unit-1"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); try { // WHEN new JpaUnitRunner(cut); fail("InitializationError expected"); } catch (final InitializationError e) { // expected assertThat(e.getCauses().get(0).getMessage(), containsString("exclude each other")); } }
Example 4
Source File: JpaUnitRunnerTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithPersistenceContextWithKonfiguredUnitNameSpecified() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class); jAnnotationUse.param("value", JpaUnitRunner.class); final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em"); final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class); jAnnotation.param("unitName", "test-unit-1"); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); final JpaUnitRunner runner = new JpaUnitRunner(cut); final RunListener listener = mock(RunListener.class); final RunNotifier notifier = new RunNotifier(); notifier.addListener(listener); // WHEN runner.run(notifier); // THEN final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class); verify(listener).testStarted(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); verify(listener).testFinished(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); }
Example 5
Source File: JaxbValidationsPlugins.java From krasa-jaxb-tools with Apache License 2.0 | 5 votes |
private void processNotNull(ClassOutline co, JFieldVar field) { if (notNullAnnotations) { log("@NotNull: " + field.name() + " added to class " + co.implClass.name()); JAnnotationUse annotation = field.annotate(NotNull.class); if (notNullPrefixClassName) { annotation.param("message", String.format("%s.%s {%s.message}", co.implClass.name(), field.name(), NotNull.class.getName())); } else if (notNullPrefixFieldName) { annotation.param("message", String.format("%s {%s.message}", field.name(), NotNull.class.getName())); } else if (notNullCustomMessages) { annotation.param("message", String.format("{%s.message}", NotNull.class.getName())); } else if (notNullCustomMessage != null) { annotation.param("message", notNullCustomMessage.replace("{ClassName}", co.implClass.name()).replace("{FieldName}", field.name())); } } }
Example 6
Source File: ImmutableJaxbGenerator.java From rice with Educational Community License v2.0 | 5 votes |
private void renderField(JDefinedClass classModel, FieldModel fieldModel) { JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, fieldModel.fieldType, fieldModel.fieldName); JAnnotationUse annotation = field.annotate(XmlElement.class); if (Util.isCommonElement(fieldModel.fieldName)) { JClass coreConstants = codeModel.ref(CoreConstants.class); JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements"); annotation.param("name", commonElementsRef.ref(Util.toConstantsVariable(fieldModel.fieldName))); } else { JClass elementsClass = codeModel.ref(Util.ELEMENTS_CLASS_NAME); JFieldRef fieldXmlNameRef = elementsClass.staticRef(Util.toConstantsVariable(fieldModel.fieldName)); annotation.param("name", fieldXmlNameRef); } annotation.param("required", false); }
Example 7
Source File: ImmutableJaxbGenerator.java From rice with Educational Community License v2.0 | 5 votes |
private void renderFutureElementsField(JDefinedClass classModel) throws Exception { JType collectionType = codeModel.parseType("java.util.Collection<org.w3c.dom.Element>"); JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, collectionType, "_futureElements"); field.init(JExpr._null()); JAnnotationUse annotation = field.annotate(SuppressWarnings.class); annotation.param("value", "unused"); field.annotate(XmlAnyElement.class); }
Example 8
Source File: JpaUnitRunnerTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithPersistenceContextAndPersistenceUnitFields() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class); jAnnotationUse.param("value", JpaUnitRunner.class); final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em"); emf1Field.annotate(PersistenceContext.class); final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf"); emf2Field.annotate(PersistenceUnit.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); final RunListener listener = mock(RunListener.class); final RunNotifier notifier = new RunNotifier(); notifier.addListener(listener); final JpaUnitRunner runner = new JpaUnitRunner(cut); // WHEN runner.run(notifier); // THEN final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class); verify(listener).testFailure(failureCaptor.capture()); final Failure failure = failureCaptor.getValue(); assertThat(failure.getException().getClass(), equalTo(IllegalArgumentException.class)); assertThat(failure.getException().getMessage(), containsString("either @PersistenceUnit or @PersistenceContext")); }
Example 9
Source File: JpaUnitRunnerTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithMultiplePersistenceUnitFields() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class); jAnnotationUse.param("value", JpaUnitRunner.class); final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf1"); emf1Field.annotate(PersistenceUnit.class); final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf2"); emf2Field.annotate(PersistenceUnit.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); final RunListener listener = mock(RunListener.class); final RunNotifier notifier = new RunNotifier(); notifier.addListener(listener); final JpaUnitRunner runner = new JpaUnitRunner(cut); // WHEN runner.run(notifier); // THEN final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class); verify(listener).testFailure(failureCaptor.capture()); final Failure failure = failureCaptor.getValue(); assertThat(failure.getException().getClass(), equalTo(IllegalArgumentException.class)); assertThat(failure.getException().getMessage(), containsString("Only single field is allowed")); }
Example 10
Source File: JpaUnitRunnerTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithMultiplePersistenceContextFields() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class); jAnnotationUse.param("value", JpaUnitRunner.class); final JFieldVar em1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em1"); em1Field.annotate(PersistenceContext.class); final JFieldVar em2Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em2"); em2Field.annotate(PersistenceContext.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); final RunListener listener = mock(RunListener.class); final RunNotifier notifier = new RunNotifier(); notifier.addListener(listener); final JpaUnitRunner runner = new JpaUnitRunner(cut); // WHEN runner.run(notifier); // THEN final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class); verify(listener).testFailure(failureCaptor.capture()); final Failure failure = failureCaptor.getValue(); assertThat(failure.getException().getClass(), equalTo(IllegalArgumentException.class)); assertThat(failure.getException().getMessage(), containsString("Only single field is allowed")); }
Example 11
Source File: ImplementationBuilder.java From aem-component-generator with Apache License 2.0 | 5 votes |
private void addExportedTypeMethod(JDefinedClass jc) { if (this.isAllowExporting) { JFieldVar jFieldVar = jc.field(PRIVATE, codeModel.ref(Resource.class), "resource"); jFieldVar.annotate(codeModel.ref(SlingObject.class)); JMethod method = jc.method(JMod.PUBLIC, codeModel.ref(String.class), "getExportedType"); method.annotate(codeModel.ref(Override.class)); method.body()._return(jFieldVar.invoke("getResourceType")); } }
Example 12
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithPersistenceContextWithKonfiguredUnitNameSpecified() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em"); final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class); jAnnotation.param("unitName", "test-unit-1"); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); final BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(cut); final RunListener listener = mock(RunListener.class); final RunNotifier notifier = new RunNotifier(); notifier.addListener(listener); // WHEN runner.run(notifier); // THEN final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class); verify(listener).testStarted(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); verify(listener).testFinished(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); }
Example 13
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithPersistenceUnitWithoutUnitNameSpecified() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf"); emField.annotate(PersistenceUnit.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); try { // WHEN new JpaUnitRule(cut); fail("JpaUnitException expected"); } catch (final JpaUnitException e) { // THEN assertThat(e.getMessage(), containsString("No Persistence")); } }
Example 14
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithPersistenceContextFieldOfWrongType() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "em"); emField.annotate(PersistenceContext.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); try { // WHEN new JpaUnitRule(cut); fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException e) { // THEN assertThat(e.getMessage(), containsString("annotated with @PersistenceContext is not of type EntityManager")); } }
Example 15
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithPersistenceContextAndPersistenceUnitFields() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em"); emf1Field.annotate(PersistenceContext.class); final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf"); emf2Field.annotate(PersistenceUnit.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); try { // WHEN new JpaUnitRule(cut); fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException e) { // THEN assertThat(e.getMessage(), containsString("either @PersistenceUnit or @PersistenceContext")); } }
Example 16
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithMultiplePersistenceUnitFields() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf1"); emf1Field.annotate(PersistenceUnit.class); final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf2"); emf2Field.annotate(PersistenceUnit.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); try { // WHEN new JpaUnitRule(cut); fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException e) { // THEN assertThat(e.getMessage(), containsString("Only single field is allowed")); } }
Example 17
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithMultiplePersistenceContextFields() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JFieldVar em1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em1"); em1Field.annotate(PersistenceContext.class); final JFieldVar em2Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em2"); em2Field.annotate(PersistenceContext.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); try { // WHEN new JpaUnitRule(cut); fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException e) { // THEN assertThat(e.getMessage(), containsString("Only single field is allowed")); } }
Example 18
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 5 votes |
@Test public void testClassWithoutPersistenceContextField() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); try { // WHEN new JpaUnitRule(cut); fail("IllegalArgumentException expected"); } catch (final IllegalArgumentException e) { // THEN assertThat(e.getMessage(), containsString("EntityManagerFactory or EntityManager field annotated")); } }
Example 19
Source File: JpaUnitRuleTest.java From jpa-unit with Apache License 2.0 | 4 votes |
@Test public void testClassWithPersistenceContextWithWithOverwrittenConfiguration() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule"); ruleField.annotate(Rule.class); final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()")); ruleField.init(instance); final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em"); final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class); jAnnotation.param("unitName", "test-unit-1"); final JAnnotationArrayMember propArray = jAnnotation.paramArray("properties"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.url").param("value", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.driver").param("value", "org.h2.Driver"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.password").param("value", "test"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.user").param("value", "test"); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); final BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(cut); final RunListener listener = mock(RunListener.class); final RunNotifier notifier = new RunNotifier(); notifier.addListener(listener); // WHEN runner.run(notifier); // THEN final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class); verify(listener).testStarted(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); verify(listener).testFinished(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); }
Example 20
Source File: JpaUnitRunnerTest.java From jpa-unit with Apache License 2.0 | 4 votes |
@Test public void testClassWithPersistenceContextWithWithOverwrittenConfiguration() throws Exception { // GIVEN final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class); jAnnotationUse.param("value", JpaUnitRunner.class); final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em"); final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class); jAnnotation.param("unitName", "test-unit-1"); final JAnnotationArrayMember propArray = jAnnotation.paramArray("properties"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.url").param("value", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.driver").param("value", "org.h2.Driver"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.password").param("value", "test"); propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.user").param("value", "test"); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jMethod.annotate(Test.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name()); final JpaUnitRunner runner = new JpaUnitRunner(cut); final RunListener listener = mock(RunListener.class); final RunNotifier notifier = new RunNotifier(); notifier.addListener(listener); // WHEN runner.run(notifier); // THEN final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class); verify(listener).testStarted(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); verify(listener).testFinished(descriptionCaptor.capture()); assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest")); assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod")); }