Details

    • Sub-task
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 2.12.1.Final
    • 3.x Future
    • Java EE
    • None

    Description

      It would be good to be able to create a new JPA converter with just the following command :

      jpa-new-converter --name CreditCardConverter --fromAttribute CreditCard --toColumn String
      

      This will generate the following code :

      @Converter(autoApply = true)
      public class CreditCardConverter implements AttributeConverter<CreditCard, String> {
      
        @Override
        public String convertToDatabaseColumn(CreditCard entityAttribute) {
          return null;
        }
      
        @Override
        public CreditCard convertToEntityAttribute(String databaseColumn) {
          return null;
        }
      }
      

      This command is only available in JPA 2.1

      We need [] to parameterize the Converter API. So the following code doesn't work, but it's a good start :

      public class JPANewConverterCommand extends AbstractJPACommand<JavaClassSource>
      {
         @Inject
         @WithAttributes(label = "Custom Scope Annotation", type = InputType.JAVA_CLASS_PICKER, required = true)
         private UIInput<String> fromAttribute;
      
         @Inject
         @WithAttributes(label = "Custom Scope Annotation", type = InputType.JAVA_CLASS_PICKER, required = true)
         private UIInput<String> toColumn;
      
         @Override
         public Metadata getMetadata(UIContext context)
         {
            return Metadata.from(super.getMetadata(context), getClass())
                     .name("JPA: New Converter")
                     .description("Create a new JPA Converter");
         }
      
         @Override
         protected String getType()
         {
            return "Converter";
         }
      
         @Override
         protected Class<JavaClassSource> getSourceType()
         {
            return JavaClassSource.class;
         }
      
         @Override
         public void initializeUI(UIBuilder builder) throws Exception
         {
            super.initializeUI(builder);
            builder.add(fromAttribute).add(toColumn);
         }
      
         @Override
         public JavaClassSource decorateSource(UIExecutionContext context, Project project, JavaClassSource source)
                  throws Exception
         {
            // Class
            source.addInterface(AttributeConverter.class).addAnnotation(Converter.class).setLiteralValue("autoApply", "true");
      
            // Methods
            source.addMethod().setPublic().setName("convertToDatabaseColumn")
                     .setReturnType(toColumn.getValue())
                     .setParameters(fromAttribute.getValue() + " entityAttribute")
                     .setBody("return null;")
                     .addAnnotation(Override.class);
            source.addMethod().setPublic().setName("convertToEntityAttribute")
                     .setReturnType(fromAttribute.getValue())
                     .setParameters(toColumn.getValue() + " databaseColumn")
                     .setBody("return null;")
                     .addAnnotation(Override.class);
      
            return source;
         }
      }
      
      @RunWith(Arquillian.class)
      public class JPANewConverterCommandTest
      {
         @Deployment
         @AddonDependencies({
                  @AddonDependency(name = "org.jboss.forge.addon:ui-test-harness"),
                  @AddonDependency(name = "org.jboss.forge.addon:shell-test-harness"),
                  @AddonDependency(name = "org.jboss.forge.addon:javaee"),
                  @AddonDependency(name = "org.jboss.forge.addon:maven"),
                  @AddonDependency(name = "org.jboss.forge.furnace.container:cdi")
         })
         public static AddonArchive getDeployment()
         {
            return ShrinkWrap.create(AddonArchive.class).addBeansXML().addClass(ProjectHelper.class);
         }
      
         @Inject
         private UITestHarness uiTestHarness;
      
         @Inject
         private ShellTest shellTest;
      
         @Inject
         private ProjectHelper projectHelper;
      
         @Inject
         private FacetFactory facetFactory;
      
         private Project project;
      
         @Before
         public void setUp()
         {
            project = projectHelper.createJavaLibraryProject();
            projectHelper.installJPA_2_1(project);
         }
      
         @Test
         public void checkCommandMetadata() throws Exception
         {
            try (CommandController controller = uiTestHarness.createCommandController(JPANewConverterCommand.class,
                     project.getRoot()))
            {
               controller.initialize();
               // Checks the command metadata
               assertTrue(controller.getCommand() instanceof JPANewConverterCommand);
               UICommandMetadata metadata = controller.getMetadata();
               assertEquals("JPA: New Converter", metadata.getName());
               assertEquals("Java EE", metadata.getCategory().getName());
               assertEquals("JPA", metadata.getCategory().getSubCategory().getName());
               assertFalse("Project is created, shouldn't have targetLocation", controller.hasInput("targetLocation"));
               assertEquals(5, controller.getInputs().size());
               assertTrue(controller.hasInput("named"));
               assertTrue(controller.hasInput("targetPackage"));
               assertTrue(controller.hasInput("fromAttribute"));
               assertTrue(controller.hasInput("toColumn"));
               assertTrue(controller.getValueFor("targetPackage").toString().endsWith(DEFAULT_ENTITY_PACKAGE));
            }
         }
      
         @Test
         public void checkCommandShell() throws Exception
         {
            shellTest.getShell().setCurrentResource(project.getRoot());
            Result result = shellTest.execute(("jpa-new-converter --named Dummy --fromAttribute java.lang.String --toColumn java.lang.Integer"), 10, TimeUnit.SECONDS);
      
            Assert.assertThat(result, not(instanceOf(Failed.class)));
            Assert.assertTrue(project.hasFacet(JPAFacet.class));
         }
      
         @Test
         public void testCreateConverter() throws Exception
         {
            facetFactory.install(project, JavaSourceFacet.class);
            try (CommandController controller = uiTestHarness.createCommandController(JPANewConverterCommand.class,
                     project.getRoot()))
            {
               controller.initialize();
               controller.setValueFor("named", "CreditCardConverter");
               controller.setValueFor("fromAttribute", "java.lang.String");
               controller.setValueFor("toColumn", "java.lang.Integer");
               controller.setValueFor("targetPackage", "org.jboss.forge.test");
               Assert.assertTrue(controller.isValid());
               Assert.assertTrue(controller.canExecute());
               Result result = controller.execute();
               Assert.assertThat(result, is(not(instanceOf(Failed.class))));
            }
            JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
            JavaResource javaResource = facet.getJavaResource("org.jboss.forge.test.CreditCardConverter");
            Assert.assertTrue(javaResource.exists());
            Assert.assertThat(javaResource.getJavaType(), is(instanceOf(JavaClass.class)));
            Assert.assertTrue(javaResource.getJavaType().hasAnnotation(Converter.class));
         }
      
         @Test
         public void testCreateConverterDefaultPackage() throws Exception
         {
            facetFactory.install(project, JavaSourceFacet.class);
            try (CommandController controller = uiTestHarness.createCommandController(JPANewConverterCommand.class,
                     project.getRoot()))
            {
               controller.initialize();
               controller.setValueFor("named", "CreditCardConverter");
               controller.setValueFor("fromAttribute", "java.lang.String");
               controller.setValueFor("toColumn", "java.lang.Integer");
               Assert.assertTrue(controller.isValid());
               Assert.assertTrue(controller.canExecute());
               Result result = controller.execute();
               Assert.assertThat(result, is(not(instanceOf(Failed.class))));
            }
            JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
            String packageName = project.getFacet(JavaSourceFacet.class).getBasePackage() + "." + DEFAULT_ENTITY_PACKAGE;
            JavaResource javaResource = facet.getJavaResource(packageName + ".CreditCardConverter");
            Assert.assertTrue(javaResource.exists());
            Assert.assertThat(javaResource.getJavaType(), is(instanceOf(JavaClass.class)));
            Assert.assertTrue(javaResource.getJavaType().hasAnnotation(Converter.class));
         }
      
         @Test
         public void testCreateConverterWithNoMandatoryAttribute() throws Exception
         {
            facetFactory.install(project, JavaSourceFacet.class);
            try (CommandController controller = uiTestHarness.createCommandController(JPANewConverterCommand.class,
                    project.getRoot()))
            {
               controller.initialize();
               controller.setValueFor("named", "CreditCardConverter");
               controller.setValueFor("targetPackage", "org.jboss.forge.test");
               Assert.assertFalse(controller.isValid());
            }
         }
      }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            agoncal Antonio Goncalves (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: