How to use isJava8OptionalTypeToken method of io.beanmother.core.mapper.FixtureConverterImpl class

Best Beanmother code snippet using io.beanmother.core.mapper.FixtureConverterImpl.isJava8OptionalTypeToken

Source:FixtureConverterImpl.java Github

copy

Full Screen

...31 }32 @Override33 public Object convert(FixtureTemplate fixtureTemplate, final TypeToken typeToken) {34 // convert java.util.Optional if it can.35 if (isJava8OptionalTypeToken(typeToken)) {36 FixtureConverter optionalConverter = loadJava8OptionalConverter();37 if (optionalConverter != null) {38 return optionalConverter.convert(fixtureTemplate, typeToken);39 }40 }41 // convert com.google.common.base.Optional if it can.42 if (isGuavaOptionalTypeToken(typeToken)) {43 FixtureConverter optionalConverter = loadGuavaOptionalConverter();44 if (optionalConverter != null) {45 return optionalConverter.convert(fixtureTemplate, typeToken);46 }47 }48 final Object[] converted = new Object[1];49 new FixtureTemplateSubTypeHandler() {50 @Override51 protected void handleIf(FixtureMap fixtureMap) {52 converted[0] = convert(fixtureMap, typeToken);53 }54 @Override55 protected void handleIf(FixtureList fixtureList) {56 converted[0] = convert(fixtureList, typeToken);57 }58 @Override59 protected void handleIf(FixtureValue fixtureValue) {60 converted[0] = convert(fixtureValue, typeToken);61 }62 }.handle(fixtureTemplate);63 return converted[0];64 }65 /**66 * Get converterFactory67 */68 public ConverterFactory getConverterFactory() {69 return converterFactory;70 }71 /**72 * Get mapperMediator73 */74 public MapperMediator getMapperMediator() {75 return mapperMediator;76 }77 /**78 * Get fixtureMapper79 */80 public FixtureMapper getFixtureMapper() {81 return getMapperMediator().getFixtureMapper();82 }83 /**84 * Convert the fixtureValue to the given TypeToken85 * @param fixtureValue the FixtureValue86 * @param typeToken the TypeToken87 * @return the converted object from fixtureValue.88 */89 protected Object convert(FixtureValue fixtureValue, TypeToken<?> typeToken) {90 if (typeToken.isPrimitive()) {91 Class<?> wrapperClass = PrimitiveTypeUtils.toWrapper((Class<?>) typeToken.getType());92 typeToken = TypeToken.of(wrapperClass);93 }94 Object source = fixtureValue.getValue();95 Converter convert = getConverterFactory().get(source, typeToken);96 if (convert != null) {97 return convert.convert(source, typeToken);98 } else {99 return null;100 }101 }102 /**103 * Convert the fixtureList to the given TypeToken104 * @param fixtureList105 * @param typeToken106 * @return converted object from fixtureList.107 * @throws IllegalAccessException108 * @throws InstantiationException109 */110 protected Object convert(FixtureList fixtureList, TypeToken<?> typeToken) {111 boolean isArray = typeToken.isArray();112 boolean isList = typeToken.isSubtypeOf(TypeToken.of(List.class));113 boolean isSet = typeToken.isSubtypeOf(TypeToken.of(Set.class));114 if (!isList && !isArray && !isSet) {115 throw new FixtureMappingException("Target setter of '" + fixtureList.getFixtureName() + "' must be List, Set or array.");116 }117 final List convertedList;118 if (isArray || typeToken.getRawType().isInterface()) {119 convertedList = new ArrayList();120 } else {121 try {122 convertedList = (List) typeToken.getRawType().newInstance();123 } catch (Exception e) {124 throw new FixtureMappingException(e);125 }126 }127 TypeToken<?> elementTypeToken = TypeTokenUtils.extractElementTypeToken(typeToken);128 for (FixtureTemplate template : fixtureList) {129 Object converted = convert(template, elementTypeToken);130 if (converted != null) {131 convertedList.add(converted);132 } else {133 logger.warn("Can not find converter for " + fixtureList.getFixtureName());134 }135 }136 // not found converter137 if (convertedList.size() == 0) return null;138 if(isArray) {139 if (elementTypeToken.isPrimitive()) {140 return PrimitiveTypeUtils.toWrapperListToPrimitiveArray(convertedList, (Class<?>) elementTypeToken.getType());141 } else {142 return Arrays.copyOf(convertedList.toArray(), convertedList.size(), (Class) typeToken.getRawType());143 }144 } else if (isSet) {145 return new HashSet<>(convertedList);146 } else {147 return convertedList;148 }149 }150 /**151 * Convert FixtureMap to given type152 * @param fixtureMap153 * @param typeToken154 * @return converted Object from fixtureMap155 * @throws IllegalAccessException156 * @throws InstantiationException157 */158 protected Object convert(FixtureMap fixtureMap, TypeToken<?> typeToken) {159 if (typeToken.isSubtypeOf(Map.class)) {160 final TypeToken<?> keyTypeToken;161 final TypeToken<?> valueTypeToken;162 List<TypeToken<?>> keyValueTypeTokens = TypeTokenUtils.extractGenericTypeTokens(typeToken);163 if (keyValueTypeTokens.size() == 2) {164 keyTypeToken = keyValueTypeTokens.get(0);165 valueTypeToken = keyValueTypeTokens.get(1);166 } else {167 keyTypeToken = TypeToken.of(Object.class);168 valueTypeToken = TypeToken.of(Object.class);169 }170 Map convertedMap;171 if (typeToken.getRawType().isInterface()) {172 convertedMap = new LinkedHashMap();173 } else {174 try {175 convertedMap = (Map) typeToken.getRawType().newInstance();176 } catch (Exception e) {177 throw new FixtureMappingException(e);178 }179 }180 for (Map.Entry<String, FixtureTemplate> entry : fixtureMap.entrySet()) {181 Object key = convert(new FixtureValue(entry.getKey()), keyTypeToken);182 Object converted = convert(entry.getValue(), valueTypeToken);183 convertedMap.put(key, converted);184 }185 return convertedMap;186 } else {187 Object obj;188 try {189 obj = ConstructHelper.construct(typeToken.getRawType(), fixtureMap, this);190 } catch (Exception e) {191 throw new FixtureMappingException(e);192 }193 getFixtureMapper().map(fixtureMap, obj);194 return obj;195 }196 }197 private boolean isJava8OptionalTypeToken(TypeToken<?> typeToken) {198 String name = typeToken.getRawType().getName();199 return typeToken.getRawType().getName().equals("java.util.Optional");200 }201 private boolean isGuavaOptionalTypeToken(TypeToken<?> typeToken) {202 return typeToken.getRawType().equals(com.google.common.base.Optional.class);203 }204 private FixtureConverter loadFixtureConverter(String className) {205 try {206 Constructor[] constructors = ClassUtils.getDefaultClassLoader().loadClass(className).getConstructors();207 for (Constructor constructor : constructors) {208 if (constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0] == FixtureConverter.class) {209 try {210 return (FixtureConverter) constructor.newInstance(this);211 } catch (Exception e) {...

Full Screen

Full Screen

isJava8OptionalTypeToken

Using AI Code Generation

copy

Full Screen

1public class FixtureConverterImplTest {2 private FixtureConverterImpl fixtureConverter;3 public void setUp() throws Exception {4 fixtureConverter = new FixtureConverterImpl();5 }6 public void testIsJava8OptionalTypeToken() throws Exception {7 TypeToken<?> typeToken = TypeToken.of(Optional.class);8 boolean result = fixtureConverter.isJava8OptionalTypeToken(typeToken);9 assertThat(result, is(true));10 }11 public void testIsJava8OptionalTypeToken2() throws Exception {12 TypeToken<?> typeToken = TypeToken.of(OptionalInt.class);13 boolean result = fixtureConverter.isJava8OptionalTypeToken(typeToken);14 assertThat(result, is(true));15 }16 public void testIsJava8OptionalTypeToken3() throws Exception {17 TypeToken<?> typeToken = TypeToken.of(OptionalDouble.class);18 boolean result = fixtureConverter.isJava8OptionalTypeToken(typeToken);19 assertThat(result, is(true));20 }21 public void testIsJava8OptionalTypeToken4() throws Exception {22 TypeToken<?> typeToken = TypeToken.of(OptionalLong.class);23 boolean result = fixtureConverter.isJava8OptionalTypeToken(typeToken);24 assertThat(result, is(true));25 }26 public void testIsJava8OptionalTypeToken5() throws Exception {27 TypeToken<?> typeToken = TypeToken.of(String.class);28 boolean result = fixtureConverter.isJava8OptionalTypeToken(typeToken);29 assertThat(result, is(false));30 }31}

Full Screen

Full Screen

isJava8OptionalTypeToken

Using AI Code Generation

copy

Full Screen

1public class FixtureConverterImplTest {2 public void testIsJava8OptionalTypeToken() {3 TypeToken<?> typeToken = TypeToken.of(Optional.class);4 boolean result = FixtureConverterImpl.isJava8OptionalTypeToken(typeToken);5 assertTrue(result);6 }7}8package io.beanmother.core.mapper;9import com.google.common.reflect.TypeToken;10import org.junit.Test;11import java.util.Optional;12import static org.junit.Assert.assertTrue;13public class FixtureConverterImplTest {14 public void testIsJava8OptionalTypeToken() {15 TypeToken<?> typeToken = TypeToken.of(Optional.class);16 boolean result = FixtureConverterImpl.isJava8OptionalTypeToken(typeToken);17 assertTrue(result);18 }19}20package io.beanmother.core.mapper;21import com.google.common.reflect.TypeToken;22import org.junit.Test;23import java.util.Optional;24import static org.junit.Assert.assertTrue;25public class FixtureConverterImplTest {26 public void testIsJava8OptionalTypeToken() {27 TypeToken<?> typeToken = TypeToken.of(Optional.class);28 boolean result = FixtureConverterImpl.isJava8OptionalTypeToken(typeToken);29 assertTrue(result);30 }31}32package io.beanmother.core.mapper;33import com.google.common.reflect.TypeToken;34import org.junit.Test;35import java.util.Optional;36import static org.junit.Assert.assertTrue;37public class FixtureConverterImplTest {38 public void testIsJava8OptionalTypeToken() {39 TypeToken<?> typeToken = TypeToken.of(Optional.class);40 boolean result = FixtureConverterImpl.isJava8OptionalTypeToken(typeToken);41 assertTrue(result);42 }43}44package io.beanmother.core.mapper;45import com.google.common.reflect.TypeToken;46import org.junit.Test;47import java.util.Optional;48import static org.junit.Assert.assertTrue;49public class FixtureConverterImplTest {50 public void testIsJava8OptionalTypeToken() {51 TypeToken<?> typeToken = TypeToken.of(Optional.class);52 boolean result = FixtureConverterImpl.isJava8OptionalTypeToken(typeToken);53 assertTrue(result);54 }55}56package io.beanmother.core.mapper;57import com.google.common.reflect.TypeToken;58import org.junit.Test;59import java.util.Optional;60import static org.junit.Assert.assertTrue;61public class FixtureConverterImplTest {

Full Screen

Full Screen

isJava8OptionalTypeToken

Using AI Code Generation

copy

Full Screen

1 public boolean isJava8OptionalTypeToken(TypeToken<?> typeToken) {2 if (typeToken == null) return false;3 if (typeToken.getRawType().equals(Optional.class)) return true;4 return false;5 }6 public Object convertToJava8OptionalTypeToken(TypeToken<?> typeToken, Object value) {7 if (typeToken == null) return null;8 if (isJava8OptionalTypeToken(typeToken)) {9 return Optional.ofNullable(value);10 }11 return null;12 }13 public Object convertToJava8OptionalTypeToken(TypeToken<?> typeToken, Object value, String defaultValue) {14 if (typeToken == null) return null;15 if (isJava8OptionalTypeToken(typeToken)) {16 if (value == null) {17 return Optional.ofNullable(defaultValue);18 } else {19 return Optional.ofNullable(value);20 }21 }22 return null;23 }24 public Object convert(TypeToken<?> typeToken, Object value) {25 Object convertedValue = convertToJava8OptionalTypeToken(typeToken, value);26 if (convertedValue != null) return convertedValue;27 if (value == null) {28 if (typeToken.getRawType().isPrimitive()) {29 return 0;30 } else {31 return null;32 }33 }34 if (typeToken.getRawType().isAssignableFrom(value.getClass())) {35 return value;36 }

Full Screen

Full Screen

isJava8OptionalTypeToken

Using AI Code Generation

copy

Full Screen

1public class Person {2 private String name;3 private int age;4 private int height;5 private Optional<String> hobby;6 public Person(String name, int age, int height, Optional<String> hobby) {7 this.name = name;8 this.age = age;9 this.height = height;10 this.hobby = hobby;11 }12 public String getName() {13 return name;14 }15 public int getAge() {16 return age;17 }18 public int getHeight() {19 return height;20 }21 public Optional<String> getHobby() {22 return hobby;23 }24}25public class PersonFixture {26 private String name;27 private int age;28 private int height;29 private String hobby;30 public PersonFixture(String name, int age, int height, String hobby) {31 this.name = name;32 this.age = age;33 this.height = height;34 this.hobby = hobby;35 }36 public String getName() {37 return name;38 }39 public int getAge() {40 return age;41 }42 public int getHeight() {43 return height;44 }45 public String getHobby() {46 return hobby;47 }48}49public class PersonFixtureConverter extends FixtureConverterImpl {50 public boolean isSupport(Class<?> type) {51 return type == Person.class;52 }53 public Object convert(FixtureMap fixtureMap, Class<?> type) {54 PersonFixture fixture = (PersonFixture) super.convert(fixtureMap, PersonFixture.class);55 return new Person(56 fixture.getName(),57 fixture.getAge(),58 fixture.getHeight(),59 isJava8OptionalTypeToken(type, "hobby") ?60 Optional.ofNullable(fixture.getHobby()) :61 );62 }63}64public class PersonFixtureConverterTest {65 public void test() {66 FixtureMap fixtureMap = new FixtureMap();67 fixtureMap.put("name", "John");68 fixtureMap.put("age", 30);69 fixtureMap.put("height", 170);70 fixtureMap.put("hobby", "soccer");71 PersonFixtureConverter converter = new PersonFixtureConverter();72 Person person = (Person) converter.convert(fixtureMap, Person.class);73 assertThat(person.getName(), is("John"));74 assertThat(person.getAge(), is(30));75 assertThat(person.getHeight(), is(170));76 assertThat(person.getHobby().get(),

Full Screen

Full Screen

isJava8OptionalTypeToken

Using AI Code Generation

copy

Full Screen

1 [javac] if (isJava8OptionalTypeToken(typeToken)) {2 [javac] symbol: method isJava8OptionalTypeToken(TypeToken)3 [javac] return (T) Optional.ofNullable(convertToType(typeToken, value));4 [javac] symbol: method convertToType(TypeToken,String)5 [javac] if (isJava8OptionalTypeToken(typeToken)) {6 [javac] symbol: method isJava8OptionalTypeToken(TypeToken)7 [javac] return (T) Optional.ofNullable(convertToType(typeToken, value));8 [javac] symbol: method convertToType(TypeToken,String)9 [javac] if (isJava8OptionalTypeToken(typeToken)) {10 [javac] symbol: method isJava8OptionalTypeToken(TypeToken)

Full Screen

Full Screen

isJava8OptionalTypeToken

Using AI Code Generation

copy

Full Screen

1public void testConvertToJava8Optional() {2 FixtureConverterImpl fixtureConverter = new FixtureConverterImpl();3 Object fixture = fixtureConverter.convertToJava8Optional("foo", "bar");4 assertTrue(fixture instanceof Optional);5 assertEquals("bar", ((Optional) fixture).get());6}7public Object convert(FixtureMap fixtureMap, TypeToken<?> typeToken) {8 if (isJava8OptionalTypeToken(typeToken)) {9 return convertToJava8Optional(fixtureMap, typeToken);10 }11}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Beanmother automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful