How to use getFormatting method of com.tngtech.jgiven.impl.format.ParameterFormattingUtil class

Best JGiven code snippet using com.tngtech.jgiven.impl.format.ParameterFormattingUtil.getFormatting

Source:ParameterFormattingUtil.java Github

copy

Full Screen

...29 public ParameterFormattingUtil( FormatterConfiguration configuration ) {30 this.configuration = configuration;31 }32 @SuppressWarnings( { "rawtypes", "unchecked" } )33 public <T> ObjectFormatter<?> getFormatting( Class<T> parameterType, String parameterName, Annotation[] annotations ) {34 ObjectFormatter<?> formatting = getFormatting( annotations, Sets.<Class<?>>newHashSet(), null, parameterName );35 if( formatting != null ) {36 return formatting;37 }38 Formatter<T> formatter = (Formatter<T>) configuration.getFormatter( parameterType );39 if( formatter != null ) {40 return new StepFormatter.TypeBasedFormatting<T>( formatter, annotations );41 }42 return DEFAULT_FORMATTING;43 }44 /**45 * Recursively searches for formatting annotations.46 *47 * @param visitedTypes used to prevent an endless loop48 * @param parameterName49 */50 private StepFormatter.Formatting<?, ?> getFormatting( Annotation[] annotations, Set<Class<?>> visitedTypes,51 Annotation originalAnnotation, String parameterName ) {52 List<StepFormatter.Formatting<?, ?>> foundFormatting = Lists.newArrayList();53 Table tableAnnotation = null;54 for( Annotation annotation : annotations ) {55 try {56 if( annotation instanceof Format ) {57 Format arg = (Format) annotation;58 foundFormatting.add( new StepFormatter.ArgumentFormatting( ReflectionUtil.newInstance( arg.value() ), arg.args() ) );59 } else if( annotation instanceof Table ) {60 tableAnnotation = (Table) annotation;61 } else if( annotation instanceof AnnotationFormat ) {62 AnnotationFormat arg = (AnnotationFormat) annotation;63 foundFormatting.add( new StepFormatter.ArgumentFormatting(64 new StepFormatter.AnnotationBasedFormatter( arg.value().newInstance(), originalAnnotation ) ) );65 } else {66 Class<? extends Annotation> annotationType = annotation.annotationType();67 if( !visitedTypes.contains( annotationType ) ) {68 visitedTypes.add( annotationType );69 StepFormatter.Formatting<?, ?> formatting = getFormatting( annotationType.getAnnotations(), visitedTypes,70 annotation, parameterName );71 if( formatting != null ) {72 foundFormatting.add( formatting );73 }74 }75 }76 } catch( Exception e ) {77 throw Throwables.propagate( e );78 }79 }80 if( foundFormatting.size() > 1 ) {81 Formatting<?, ?> innerFormatting = Iterables.getLast( foundFormatting );82 foundFormatting.remove( innerFormatting );83 ChainedFormatting<?> chainedFormatting = new StepFormatter.ChainedFormatting<Object>( (ObjectFormatter<Object>) innerFormatting );84 for( StepFormatter.Formatting<?, ?> formatting : Lists.reverse( foundFormatting ) ) {85 chainedFormatting.addFormatting( (StepFormatter.Formatting<?, String>) formatting );86 }87 foundFormatting.clear();88 foundFormatting.add( chainedFormatting );89 }90 if( tableAnnotation != null ) {91 ObjectFormatter<?> objectFormatter = foundFormatting.isEmpty()92 ? DefaultFormatter.INSTANCE93 : foundFormatting.get( 0 );94 return getTableFormatting( annotations, parameterName, tableAnnotation, objectFormatter );95 }96 if( foundFormatting.isEmpty() ) {97 return null;98 }99 return foundFormatting.get( 0 );100 }101 private StepFormatter.Formatting<?, ?> getTableFormatting( Annotation[] annotations, String parameterName, Table annotation,102 ObjectFormatter<?> objectFormatter ) {103 Table tableAnnotation = annotation;104 TableFormatterFactory factory = createTableFormatterFactory( parameterName, tableAnnotation );105 TableFormatter tableFormatter = factory.create( configuration, objectFormatter );106 return new StepFormatter.TableFormatting( tableFormatter, tableAnnotation, parameterName, annotations );107 }108 private TableFormatterFactory createTableFormatterFactory( String parameterName, Table tableAnnotation ) {109 Class<? extends TableFormatterFactory> formatterFactoryClass = tableAnnotation.formatter();110 try {111 return ReflectionUtil.newInstance( formatterFactoryClass );112 } catch( Exception e ) {113 throw new JGivenWrongUsageException(114 "Could not create an instance of " + formatterFactoryClass.getName()115 + " which was specified at the @Table annotation for parameter '" + parameterName116 + "'. Most likely this was due to a missing default constructor",117 TableFormatterFactory.class, e );118 }119 }120 public List<String> toStringList( List<ObjectFormatter<?>> formatter, List<?> arguments ) {121 List<String> result = Lists.newArrayList();122 for( int i = 0; i < arguments.size(); i++ ) {123 ObjectFormatter<?> formatting = DEFAULT_FORMATTING;124 if( i < formatter.size() && formatter.get( i ) != null ) {125 formatting = formatter.get( i );126 }127 result.add( formatUsingFormatterOrDefault( formatting, arguments.get( i ) ) );128 }129 return result;130 }131 private <T> String formatUsingFormatterOrDefault( ObjectFormatter<T> formatting, Object o ) {132 return formatting.format( (T) o );133 }134 public List<ObjectFormatter<?>> getFormatter( Class<?>[] parameterTypes, List<String> parameterNames,135 Annotation[][] parameterAnnotations ) {136 List<ObjectFormatter<?>> res = Lists.newArrayList();137 for( int i = 0; i < parameterTypes.length; i++ ) {138 Annotation[] annotations = parameterAnnotations[i];139 if( !AnnotationUtil.isHidden( annotations ) ) {140 String parameterName = i < parameterNames.size() ? parameterNames.get( i ) : "param" + i;141 res.add( this.getFormatting( parameterTypes[i], parameterName, annotations ) );142 }143 }144 return res;145 }146}...

Full Screen

Full Screen

Source:FieldBasedRowFormatter.java Github

copy

Full Screen

...69 Map<String, ObjectFormatter<?>> inter = Maps.newHashMap();70 // First, look for any format defined at field level71 for( int i = 0; i < fields.size(); i++ ) {72 Field field = fields.get( i );73 ObjectFormatter<?> formatter = pfu.getFormatting( field.getType(), field.getName(), field.getAnnotations() );74 // Finally, bind format to the field when found75 if( formatter != null ) {76 inter.put( field.getName(), formatter );77 }78 }79 // Then, override with any formats specified through the Table80 // annotation81 NamedFormat[] nftab;82 // Array of NamedFormat has precedence over NamedFormats83 nftab = annotation.fieldsFormat();84 if( nftab.length == 0 ) {85 // Fall back on a custom NamedFormats annotation86 Class<? extends Annotation> aclazz = annotation.fieldsFormatSetAnnotation();87 if( aclazz.isAnnotationPresent( NamedFormats.class ) ) {88 NamedFormats nfset = aclazz.getAnnotation( NamedFormats.class );89 nftab = nfset.value();90 }91 }92 for( NamedFormat nf : nftab ) {93 ObjectFormatter<?> formatter;94 // Custom format annotation has precedence here95 Class<? extends Annotation> cfa = nf.formatAnnotation();96 if( cfa.equals( Annotation.class ) ) {97 // Custom format annotation not set, fallback on any format98 formatter = pfu.getFormatting( Object.class, nf.name(), new Annotation[] { nf.format() } );99 } else {100 formatter = pfu.getFormatting( Object.class, nf.name(), cfa.getAnnotations() );101 }102 inter.put( nf.name(), formatter );103 }104 return inter;105 }106 private static List<Field> getFields( Table tableAnnotation, Class<?> type ) {107 final Set<String> includeFields = Sets.newHashSet( tableAnnotation.includeFields() );108 final Set<String> excludeFields = Sets.newHashSet( tableAnnotation.excludeFields() );109 return FluentIterable.from( ReflectionUtil.getAllNonStaticFields( type ) ).filter( new Predicate<Field>() {110 @Override111 public boolean apply( Field input ) {112 String name = input.getName();113 if( !includeFields.isEmpty() ) {114 return includeFields.contains( name );...

Full Screen

Full Screen

Source:POJOAnnotationFormatter.java Github

copy

Full Screen

...82 Map<String, ObjectFormatter<?>> inter = Maps.newHashMap();83 // First, look for any format defined at field level84 for( int i = 0; i < fields.size(); i++ ) {85 Field field = fields.get( i );86 ObjectFormatter<?> formatter = pfu.getFormatting( field.getType(), field.getName(), field.getAnnotations() );87 // Finally, bind format to the field when found88 if( formatter != null ) {89 inter.put( field.getName(), formatter );90 }91 }92 // Then, override with any formats specified through the Table93 // annotation94 NamedFormat[] nftab;95 // Array of NamedFormat has precedence over NamedFormats96 nftab = annotation.fieldFormats();97 if( nftab.length == 0 ) {98 // Fall back on a custom NamedFormats annotation99 Class<? extends Annotation> aclazz = annotation.fieldFormatsAnnotation();100 if( aclazz.isAnnotationPresent( NamedFormats.class ) ) {101 NamedFormats nfset = aclazz.getAnnotation( NamedFormats.class );102 nftab = nfset.value();103 }104 }105 for( NamedFormat nf : nftab ) {106 ObjectFormatter<?> formatter;107 // Custom format annotation has precedence here108 Class<? extends Annotation> cfa = nf.formatAnnotation();109 if( cfa.equals( Annotation.class ) ) {110 // Custom format annotation not set, fallback on any format111 formatter = pfu.getFormatting( Object.class, nf.name(), new Annotation[] { nf.format() } );112 } else {113 formatter = pfu.getFormatting( Object.class, nf.name(), cfa.getAnnotations() );114 }115 inter.put( nf.name(), formatter );116 }117 return inter;118 }119 private List<Field> getFields( Class<?> type, POJOFormat annotation ) {120 final Set<String> includeFields = Sets.newHashSet( annotation.includeFields() );121 final Set<String> excludeFields = Sets.newHashSet( annotation.excludeFields() );122 return FluentIterable.from( ReflectionUtil.getAllNonStaticFields( type ) )123 .filter( new Predicate<Field>() {124 @Override125 public boolean apply( Field input ) {126 String name = input.getName();127 if( !includeFields.isEmpty() ) {...

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 JGiven 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