Skip to content

Commit

Permalink
the generated mapper is an application-scoped CDI bean and can be
Browse files Browse the repository at this point in the history
retrieved via @Inject
  • Loading branch information
marcosemiao committed Aug 25, 2016
1 parent ed47d9a commit 009cc90
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 12 deletions.
29 changes: 29 additions & 0 deletions processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,36 @@
<scope>test</scope>
</dependency>

<!-- Ioc CDI -->
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.1_spec</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.2_spec</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ void emitCustomMappersFields(JavaWriter writer, boolean assign) throws IOExcepti

writer.emitEmptyLine();
writer.emitJavadoc("Custom Mapper setter for " + field);
writer.beginMethod("void", "setCustomMapper" + customMapperField.getSimpleName(),
EnumSet.of(PUBLIC, FINAL), customMapperField.asType().toString(), "mapper");

EnumSet<Modifier> modifiers = EnumSet.of(PUBLIC, FINAL);
if (ioC == IoC.CDI) {
modifiers = EnumSet.of(PUBLIC);
}
writer.beginMethod("void", "setCustomMapper" + customMapperField.getSimpleName(),modifiers, customMapperField.asType().toString(), "mapper");
writer.emitStatement("this.%s = mapper", field);
writer.endMethod();
writer.emitEmptyLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ public void emitFactoryFields(JavaWriter writer, boolean assign) throws IOExcept

writer.emitEmptyLine();
writer.emitJavadoc("Factory setter for " + field);
writer.beginMethod("void", "setFactory" + factoryField.getSimpleName(),
EnumSet.of(PUBLIC, FINAL), factoryField.asType().toString(), "_factory");

EnumSet<Modifier> modifiers = EnumSet.of(PUBLIC, FINAL);
if (ioC == IoC.CDI) {
modifiers = EnumSet.of(PUBLIC);
}
writer.beginMethod("void", "setFactory" + factoryField.getSimpleName(), modifiers, factoryField.asType().toString(), "_factory");
writer.emitStatement("this.%s = _factory", field);
writer.endMethod();
writer.emitEmptyLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,22 @@ public void build() throws IOException {
writer.emitSingleLineComment(GENERATED_BY_SELMA);
writer.emitPackage(packageName);
writer.emitEmptyLine();
if (mapper.ioC == IoC.SPRING) {
if (mapper.ioCServiceName != "") {
writer.emitAnnotation("org.springframework.stereotype.Service", "\"" + mapper.ioCServiceName + "\"");
} else {
writer.emitAnnotation("org.springframework.stereotype.Service");
}
}

switch (mapper.ioC) {
case SPRING:
if (mapper.ioCServiceName != "") {
writer.emitAnnotation("org.springframework.stereotype.Service", "\"" + mapper.ioCServiceName + "\"");
} else {
writer.emitAnnotation("org.springframework.stereotype.Service");
}
break;
case CDI:
writer.emitAnnotation("javax.enterprise.context.ApplicationScoped");
break;
default:
break;
}

openClassBlock(writer, adapterName, strippedTypeName);
writer.emitEmptyLine();
firstMethod = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2013 Séven Le Mesle
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package fr.xebia.extras.selma.it.inject;

import fr.xebia.extras.selma.IoC;
import fr.xebia.extras.selma.Mapper;
import fr.xebia.extras.selma.beans.AddressIn;
import fr.xebia.extras.selma.beans.AddressOut;

/**
* Created by slemesle on 25/03/15.
*/
@Mapper(withIgnoreFields = "extras", withCustom = CustomImmutableMapperClass.class, withFactories = BeanFactoryClass.class, withIoC = IoC.CDI, withFinalMappers = false)
public interface AddressMapperCDI {

AddressOut asAddressOut(AddressIn in);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2013 Séven Le Mesle
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package fr.xebia.extras.selma.it.inject;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;

import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.spi.ContainerLifecycle;
import org.junit.Assert;
import org.junit.Test;

import fr.xebia.extras.selma.beans.AddressIn;
import fr.xebia.extras.selma.beans.AddressOut;
import fr.xebia.extras.selma.beans.CityIn;
import fr.xebia.extras.selma.it.custom.mapper.CustomImmutableMapper;
import fr.xebia.extras.selma.it.utils.Compile;
import fr.xebia.extras.selma.it.utils.IntegrationTestBase;

@Compile(withClasses = { AddressMapperCDI.class, CustomImmutableMapperClass.class, BeanFactoryClass.class })
public class CustomMapperUsingCdiIoCIT extends IntegrationTestBase {

@Test
public void given_custom_as_selma_mapper_should_map_bean_with_it()
throws IllegalAccessException, InstantiationException, ClassNotFoundException {

final WebBeansContext currentInstance = WebBeansContext.currentInstance();

final ContainerLifecycle container = currentInstance.getService(ContainerLifecycle.class);

container.startApplication(null);

final BeanManager beanManager = container.getBeanManager();

final Set<Bean<?>> beans = beanManager.getBeans(AddressMapperCDI.class);

final Iterator<Bean<?>> iterator = beans.iterator();
final Bean<?> bean = iterator.next();
final Object create = bean.create(null);
final AddressMapperCDI addressMapper = (AddressMapperCDI) create;

final AddressIn addressIn = new AddressIn();
addressIn.setCity(new CityIn());
addressIn.getCity().setCapital(true);
addressIn.getCity().setName("Paris");
addressIn.getCity().setPopulation(3 * 1000 * 1000);
addressIn.setPrincipal(true);
addressIn.setNumber(55);
addressIn.setStreet("rue de la truanderie");
addressIn.setExtras(Arrays.asList("titi", "toto"));

final AddressOut res = addressMapper.asAddressOut(addressIn);

Assert.assertNotNull(res);

Assert.assertEquals(addressIn.getStreet(), res.getStreet());
Assert.assertEquals(addressIn.getNumber(), res.getNumber());
Assert.assertNull(res.getExtras());

Assert.assertEquals(addressIn.getCity().getName() + CustomImmutableMapper.IMMUTABLY_MAPPED, res.getCity().getName());
Assert.assertEquals(addressIn.getCity().getPopulation() + CustomImmutableMapper.POPULATION_INC, res.getCity().getPopulation());
Assert.assertEquals(addressIn.getCity().isCapital(), res.getCity().isCapital());
}
}
2 changes: 1 addition & 1 deletion selma/src/main/java/fr/xebia/extras/selma/IoC.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
* Created by slemesle on 27/03/15.
*/
public enum IoC {
NO, SPRING
NO, SPRING, CDI
}

0 comments on commit 009cc90

Please sign in to comment.