Best JGiven code snippet using com.tngtech.jgiven.report.model.TableAnnotation
Source:DefaultTableFormatter.java
1package com.tngtech.jgiven.format.table;2import java.lang.annotation.Annotation;3import java.lang.reflect.Array;4import java.util.Arrays;5import java.util.Collections;6import java.util.List;7import com.google.common.collect.ImmutableList;8import com.google.common.collect.Lists;9import com.tngtech.jgiven.annotation.Table;10import com.tngtech.jgiven.config.FormatterConfiguration;11import com.tngtech.jgiven.exception.JGivenWrongUsageException;12import com.tngtech.jgiven.format.DefaultFormatter;13import com.tngtech.jgiven.format.ObjectFormatter;14import com.tngtech.jgiven.impl.util.AnnotationUtil;15import com.tngtech.jgiven.impl.util.ApiUtil;16import com.tngtech.jgiven.impl.util.ReflectionUtil;17import com.tngtech.jgiven.report.model.DataTable;18/**19 * The default implementation to format a table argument20 */21public class DefaultTableFormatter implements TableFormatter {22 public static final String DEFAULT_NUMBERED_HEADER = "#";23 private final FormatterConfiguration formatterConfiguration;24 private final ObjectFormatter<?> objectFormatter;25 public DefaultTableFormatter( FormatterConfiguration formatterConfiguration, ObjectFormatter<?> objectFormatter ) {26 this.formatterConfiguration = formatterConfiguration;27 this.objectFormatter = objectFormatter;28 }29 @Override30 public DataTable format( Object tableArgument, Table tableAnnotation, String parameterName, Annotation... allAnnotations ) {31 DataTable dataTable = toDataTable( tableArgument, tableAnnotation, parameterName, allAnnotations );32 addNumberedRows( tableAnnotation, dataTable );33 addNumberedColumns( tableAnnotation, dataTable );34 return dataTable;35 }36 private static void addNumberedRows( Table tableAnnotation, DataTable dataTable ) {37 String customHeader = tableAnnotation.numberedRowsHeader();38 boolean hasCustomerHeader = !customHeader.equals( AnnotationUtil.ABSENT );39 if( tableAnnotation.numberedRows() || hasCustomerHeader ) {40 ApiUtil.isTrue( !hasCustomerHeader || dataTable.hasHorizontalHeader(),41 "Using numberedRowsHeader in @Table without having a horizontal header." );42 int rowCount = dataTable.getRowCount();43 List<String> column = Lists.newArrayListWithExpectedSize( rowCount );44 addHeader( customHeader, column, dataTable.hasHorizontalHeader() );45 addNumbers( rowCount, column );46 dataTable.addColumn( 0, column );47 }48 }49 private static void addNumberedColumns( Table tableAnnotation, DataTable dataTable ) {50 String customHeader = tableAnnotation.numberedColumnsHeader();51 boolean hasCustomerHeader = !customHeader.equals( AnnotationUtil.ABSENT );52 if( tableAnnotation.numberedColumns() || hasCustomerHeader ) {53 ApiUtil.isTrue( !hasCustomerHeader || dataTable.hasVerticalHeader(),54 "Using numberedColumnsHeader in @Table without having a vertical header." );55 int columnCount = dataTable.getColumnCount();56 List<String> row = Lists.newArrayListWithExpectedSize( columnCount );57 addHeader( customHeader, row, dataTable.hasVerticalHeader() );58 addNumbers( columnCount, row );59 dataTable.addRow( 0, row );60 }61 }62 private static void addHeader( String customHeader, List<String> column, boolean hasHeader ) {63 boolean hasCustomerHeader = !customHeader.equals( AnnotationUtil.ABSENT );64 if( hasHeader ) {65 String header = DEFAULT_NUMBERED_HEADER;66 if( hasCustomerHeader ) {67 header = customHeader;68 }69 column.add( header );70 }71 }72 private static void addNumbers( int count, List<String> column ) {73 int counter = 1;74 while( column.size() < count ) {75 column.add( Integer.toString( counter ) );76 counter++;77 }78 }79 private DataTable toDataTable( Object tableValue, Table tableAnnotation, String parameterName, Annotation[] annotations ) {80 List<List<String>> result = Lists.newArrayList();81 Iterable<?> rows = toIterable( tableValue );82 if( rows == null ) {83 rows = ImmutableList.of( tableValue );84 }85 boolean first = true;86 int ncols = 0;87 for( Object row : rows ) {88 if( first ) {89 if( toIterable( row ) == null ) {90 return pojosToTableValue( rows, tableAnnotation, parameterName, annotations );91 }92 }93 List<String> values = toStringList( row );94 if( !first && ncols != values.size() ) {95 throw new JGivenWrongUsageException( "Number of columns in @Table annotated parameter is not equal for all rows. Expected "96 + ncols + " got " + values.size() );97 }98 ncols = values.size();99 result.add( values );100 first = false;101 }102 if( tableAnnotation.columnTitles().length > 0 ) {103 result.add( 0, Arrays.asList( tableAnnotation.columnTitles() ) );104 }105 result = tableAnnotation.transpose() ? transpose( result ) : result;106 return new DataTable( tableAnnotation.header(), result );107 }108 DataTable pojosToTableValue( Iterable<?> objects, final Table tableAnnotation, String parameterName, Annotation[] annotations ) {109 Object first = objects.iterator().next();110 RowFormatterFactory objectRowFormatterFactory = ReflectionUtil.newInstance( tableAnnotation.rowFormatter() );111 RowFormatter formatter = objectRowFormatterFactory.create( first.getClass(), parameterName, tableAnnotation, annotations,112 formatterConfiguration, objectFormatter );113 List<List<String>> list = Lists.newArrayList();114 if( tableAnnotation.header() != Table.HeaderType.NONE ) {115 if( tableAnnotation.columnTitles().length > 0 ) {116 list.add( Arrays.asList( tableAnnotation.columnTitles() ) );117 } else {118 list.add( formatter.header() );119 }120 }121 for( Object o : objects ) {122 list.add( formatter.formatRow( o ) );123 }124 list = formatter.postProcess( list );125 list = tableAnnotation.transpose() || tableAnnotation.header().isVertical() ? transpose( list ) : list;126 return new DataTable( tableAnnotation.header(), list );127 }128 static List<List<String>> transpose( List<List<String>> list ) {129 List<List<String>> transposed = Lists.newArrayList();130 for( int rowIdx = 0; rowIdx < list.size(); rowIdx++ ) {131 List<String> row = list.get( rowIdx );132 for( int colIdx = 0; colIdx < row.size(); colIdx++ ) {133 if( rowIdx == 0 ) {134 transposed.add( Lists.<String>newArrayList() );135 }136 transposed.get( colIdx ).add( row.get( colIdx ) );137 }138 }139 return transposed;140 }141 private static List<String> toStringList( Object row ) {142 List<String> list = Lists.newArrayList();143 Iterable<?> objects = toIterable( row );144 if( objects == null ) {145 throw new JGivenWrongUsageException( "@Table annotated argument cannot be converted to a data table." );146 }147 for( Object o : objects ) {148 list.add( toDefaultStringFormat( o ) );149 }150 return list;151 }152 private static Iterable<?> toIterable( Object value ) {153 if( value instanceof Iterable<?> ) {154 return (Iterable<?>) value;155 }156 if( value.getClass().isArray() ) {157 return arrayToList( value );158 }159 return null;160 }161 private static Iterable<?> arrayToList( Object array ) {162 int length = Array.getLength( array );163 if( length == 0 ) {164 return Collections.emptyList();165 }166 List<Object> result = Lists.newArrayList();167 for( int i = 0; i < length; i++ ) {168 result.add( Array.get( array, i ) );169 }170 return result;171 }172 private static String toDefaultStringFormat( Object value ) {173 return DefaultFormatter.INSTANCE.format( value );174 }175 public static class Factory implements TableFormatterFactory {176 @Override177 public TableFormatter create( FormatterConfiguration formatterConfiguration, ObjectFormatter<?> objectFormatter ) {178 return new DefaultTableFormatter( formatterConfiguration, objectFormatter );179 }180 }181}...
Source:ParameterFormattingUtil.java
1package com.tngtech.jgiven.impl.format;2import java.lang.annotation.Annotation;3import java.util.List;4import java.util.Set;5import com.google.common.base.Throwables;6import com.google.common.collect.Iterables;7import com.google.common.collect.Lists;8import com.google.common.collect.Sets;9import com.tngtech.jgiven.annotation.AnnotationFormat;10import com.tngtech.jgiven.annotation.Format;11import com.tngtech.jgiven.annotation.Table;12import com.tngtech.jgiven.config.FormatterConfiguration;13import com.tngtech.jgiven.exception.JGivenWrongUsageException;14import com.tngtech.jgiven.format.ArgumentFormatter;15import com.tngtech.jgiven.format.DefaultFormatter;16import com.tngtech.jgiven.format.Formatter;17import com.tngtech.jgiven.format.ObjectFormatter;18import com.tngtech.jgiven.format.table.TableFormatter;19import com.tngtech.jgiven.format.table.TableFormatterFactory;20import com.tngtech.jgiven.impl.util.AnnotationUtil;21import com.tngtech.jgiven.impl.util.ReflectionUtil;22import com.tngtech.jgiven.report.model.StepFormatter;23import com.tngtech.jgiven.report.model.StepFormatter.ChainedFormatting;24import com.tngtech.jgiven.report.model.StepFormatter.Formatting;25public class ParameterFormattingUtil {26 private static final StepFormatter.Formatting<?, ?> DEFAULT_FORMATTING = new StepFormatter.ArgumentFormatting<ArgumentFormatter<Object>, Object>(27 new DefaultFormatter<Object>() );28 private final FormatterConfiguration configuration;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}...
Source:TableFormatter.java
1package com.tngtech.jgiven.format.table;2import java.lang.annotation.Annotation;3import com.tngtech.jgiven.annotation.Table;4import com.tngtech.jgiven.report.model.DataTable;5/**6 * Formatter that will format arguments as a table.7 * This formatter is used when a parameter is annotated with the {@link com.tngtech.jgiven.annotation.Table}8 * annotation.9 *10 * @see com.tngtech.jgiven.annotation.Table11 */12public interface TableFormatter {13 /**14 * Generates a {@link DataTable} from a given table argument15 *16 * @param tableArgument the actual argument passed to the step method17 * @param tableAnnotation the annotation of the step method parameter18 * @param parameterName the name of the step method parameter19 * @param allAnnotations all annotations of the step method parameter, including {@link com.tngtech.jgiven.annotation.Table}20 * @return a DataTable instance that defines how the table should look in the report21 */22 DataTable format( Object tableArgument, Table tableAnnotation, String parameterName, Annotation... allAnnotations );23}...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!