How to use FieldBasedRowFormatter class of com.tngtech.jgiven.format.table package

Best JGiven code snippet using com.tngtech.jgiven.format.table.FieldBasedRowFormatter

Source:FieldBasedRowFormatter.java Github

copy

Full Screen

...25 *26 * @See {@link com.tngtech.jgiven.annotation.Table} for details<br>27 * {@link NamedFormats} annotation<br>28 */29public class FieldBasedRowFormatter extends RowFormatter {30 private ParameterFormattingUtil pfu = new ParameterFormattingUtil( new DefaultConfiguration() );31 private final Table tableAnnotation;32 private List<Field> fields;33 boolean[] nonNullColumns;34 Map<String, ObjectFormatter<?>> formattersByFieldName;35 public FieldBasedRowFormatter( Class<?> type, String parameterName, Table tableAnnotation,36 Annotation[] annotations ) {37 this.tableAnnotation = tableAnnotation;38 this.fields = getFields( tableAnnotation, type );39 this.nonNullColumns = new boolean[fields.size()];40 this.formattersByFieldName = retrieveFieldsFormatters( tableAnnotation, fields );41 }42 @Override43 public List<String> header() {44 return getFieldNames( fields );45 }46 @SuppressWarnings( "unchecked" )47 @Override48 public List<String> formatRow( Object object ) {49 List<Object> allFieldValues = ReflectionUtil.getAllFieldValues( object, fields, "" );50 List<String> res = Lists.newArrayList();51 for( int i = 0; i < allFieldValues.size(); i++ ) {52 Object v = allFieldValues.get( i );53 Field field = fields.get( i );54 if( v != null ) {55 nonNullColumns[i] = true;56 }57 @SuppressWarnings( "rawtypes" )58 ObjectFormatter formatter = formattersByFieldName.get( field.getName() );59 if( formatter != null ) {60 res.add( formatter.format( v ) );61 } else {62 formatter = DefaultFormatter.INSTANCE;63 res.add( formatter.format( v ) );64 }65 }66 return res;67 }68 private Map<String, ObjectFormatter<?>> retrieveFieldsFormatters( Table annotation, List<Field> fields ) {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 );115 }116 if( excludeFields.contains( name ) ) {117 return false;118 }119 return true;120 }121 } ).toList();122 }123 private static List<String> getFieldNames( Iterable<Field> fields ) {124 return FluentIterable.from( ReflectionUtil.getAllFieldNames( fields ) ).transform( new Function<String, String>() {125 @Override126 public String apply( String input ) {127 return input.replace( '_', ' ' );128 }129 } ).toList();130 }131 @Override132 public List<List<String>> postProcess( List<List<String>> list ) {133 if( !tableAnnotation.includeNullColumns() ) {134 return removeNullColumns( list );135 }136 return list;137 }138 private List<List<String>> removeNullColumns( List<List<String>> list ) {139 List<List<String>> newList = Lists.newArrayListWithCapacity( list.size() );140 for( List<String> row : list ) {141 List<String> newRow = Lists.newArrayList();142 newList.add( newRow );143 for( int i = 0; i < nonNullColumns.length; i++ ) {144 if( nonNullColumns[i] ) {145 newRow.add( row.get( i ) );146 }147 }148 }149 return newList;150 }151 /**152 * Factory for creating instances of153 * {@link com.tngtech.jgiven.format.table.FieldBasedRowFormatter}154 *155 * @see com.tngtech.jgiven.format.table.FieldBasedRowFormatter156 * @see com.tngtech.jgiven.format.table.RowFormatterFactory157 * @see com.tngtech.jgiven.annotation.Table158 * @see PlainRowFormatter.Factory159 * @since 0.9.6160 */161 public static class Factory implements RowFormatterFactory {162 @Override163 public RowFormatter create( Class<?> parameterType, String parameterName, Table tableAnnotation,164 Annotation[] annotations, FormatterConfiguration configuration, ObjectFormatter<?> objectFormatter ) {165 return new FieldBasedRowFormatter( parameterType, parameterName, tableAnnotation, annotations );166 }167 }168}...

Full Screen

Full Screen

Source:DefaultRowFormatterFactory.java Github

copy

Full Screen

...8 * Default RowFormatterFactory that evaluates the {@link com.tngtech.jgiven.annotation.Table#objectFormatting()}9 * attribute to create a RowFormatter.10 *11 * @see com.tngtech.jgiven.annotation.Table12 * @see com.tngtech.jgiven.format.table.FieldBasedRowFormatter13 * @see com.tngtech.jgiven.format.table.PlainRowFormatter14 * @since 0.10.015 */16public class DefaultRowFormatterFactory implements RowFormatterFactory {17 @Override18 public RowFormatter create( Class<?> parameterType, String parameterName, Table tableAnnotation,19 Annotation[] annotations, FormatterConfiguration configuration, ObjectFormatter<?> objectFormatter ) {20 Table.ObjectFormatting objectFormatting = tableAnnotation.objectFormatting();21 RowFormatterFactory factory;22 if( objectFormatting == Table.ObjectFormatting.PLAIN23 || !( objectFormatter instanceof DefaultFormatter ) ) {24 factory = new PlainRowFormatter.Factory();25 } else {26 factory = new FieldBasedRowFormatter.Factory();27 }28 return factory.create( parameterType, parameterName, tableAnnotation, annotations, configuration, objectFormatter );29 }30}...

Full Screen

Full Screen

FieldBasedRowFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.examples.table;2import com.tngtech.jgiven.Stage;3import com.tngtech.jgiven.annotation.Table;4import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;5public class TableFormatExample extends Stage<TableFormatExample> {6 public TableFormatExample a_table_with_an_object_list(@Table(rowFormatter = FieldBasedRowFormatter.class) 7 List<MyObject> myObjectList) {8 return self();9 }10}11package com.tngtech.jgiven.examples.table;12import com.tngtech.jgiven.Stage;13public class TableFormatExample2 extends Stage<TableFormatExample2> {14 public TableFormatExample2 a_table_with_an_object_list(List<MyObject> myObjectList) {15 return self();16 }17}18package com.tngtech.jgiven.examples.table;19import com.tngtech.jgiven.Stage;20public class TableFormatExample3 extends Stage<TableFormatExample3> {21 public TableFormatExample3 a_table_with_an_object_list(List<MyObject> myObjectList) {22 return self();23 }24}25package com.tngtech.jgiven.examples.table;26public class MyObject {27 private String name;28 private String description;29 public String getName() {30 return name;31 }32 public void setName(String name) {33 this.name = name;34 }35 public String getDescription() {36 return description;37 }38 public void setDescription(String description) {39 this.description = description;40 }41}42package com.tngtech.jgiven.examples.table;43import com.tngtech.jgiven.format.table.TableFormatter;44import com.tngtech.jgiven.format.table.TableFormatterCell;45import java.util.ArrayList;46import java.util.List;47public class MyObjectRowFormatter implements TableFormatter<MyObject> {48 public List<TableFormatterCell> format(MyObject myObject) {49 List<TableFormatterCell> cells = new ArrayList<>();50 cells.add(new TableFormatterCell(myObject.getName()));51 cells.add(new TableFormatterCell(myObject.getDescription()));52 return cells;53 }54}55package com.tngtech.jgiven.examples.table;56import com.tngtech.jgiven.format.table.TableFormatter;57import com.tngtech.jgiven.format.table.TableFormatterCell;58import java.util

Full Screen

Full Screen

FieldBasedRowFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.format.table;2import java.lang.reflect.Field;3import com.tngtech.jgiven.format.Formatter;4import com.tngtech.jgiven.format.ObjectFormatter;5import com.tngtech.jgiven.format.ObjectFormatterFactory;6import com.tngtech.jgiven.format.table.TableFormatter.TableFormatterBuilder;7import com.tngtech.jgiven.impl.util.ReflectionUtil;8public class FieldBasedRowFormatter<T> implements RowFormatter<T> {9 private final Field[] fields;10 private final Formatter[] formatters;11 public FieldBasedRowFormatter( Class<T> type ) {12 fields = ReflectionUtil.getAllFields( type );13 formatters = new Formatter[fields.length];14 for( int i = 0; i < fields.length; i++ ) {15 Class<?> fieldType = fields[i].getType();16 formatters[i] = ObjectFormatterFactory.getFormatter( fieldType );17 }18 }19 public String[] formatRow( T row ) {20 String[] result = new String[fields.length];21 for( int i = 0; i < fields.length; i++ ) {22 Object value = ReflectionUtil.getFieldValue( fields[i], row );23 result[i] = formatters[i].format( value );24 }25 return result;26 }27 public static <T> TableFormatterBuilder<T> create( Class<T> type ) {28 return TableFormatter.rowFormatter( new FieldBasedRowFormatter<T>( type ) );29 }30 public static <T> TableFormatterBuilder<T> create() {31 return TableFormatter.rowFormatter( new RowFormatter<T>() {32 public String[] formatRow( T row ) {33 return ObjectFormatter.formatObject( row );34 }35 } );36 }37}38package com.tngtech.jgiven.format.table;39import java.util.List;40import com.tngtech.jgiven.format.ObjectFormatter;41public class TableFormatter<T> {42 private final List<T> rows;43 private final RowFormatter<T> rowFormatter;44 public TableFormatter( List<T> rows, RowFormatter<T> rowFormatter ) {45 this.rows = rows;46 this.rowFormatter = rowFormatter;47 }48 public String format() {49 int[] columnWidths = calculateColumnWidths();50 StringBuilder sb = new StringBuilder();

Full Screen

Full Screen

FieldBasedRowFormatter

Using AI Code Generation

copy

Full Screen

1package com.jgiven.test;2import com.tngtech.jgiven.Stage;3import com.tngtech.jgiven.annotation.Table;4import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;5import java.util.List;6public class ThenTest extends Stage<ThenTest>{7 public ThenTest the_list_of_objects_is_formatted_as_table_using_FieldBasedRowFormatter(@Table List<TestClass> list){8 return self();9 }10 public static class TestClass{11 public String name;12 public int age;13 }14}15package com.jgiven.test;16import com.tngtech.jgiven.Stage;17import com.tngtech.jgiven.annotation.Table;18import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;19import java.util.List;20public class ThenTest extends Stage<ThenTest>{21 public ThenTest the_list_of_objects_is_formatted_as_table_using_FieldBasedRowFormatter(@Table List<TestClass> list){22 return self();23 }24 public static class TestClass{25 public String name;26 public int age;27 }28}29package com.jgiven.test;30import com.tngtech.jgiven.Stage;31import com.tngtech.jgiven.annotation.Table;32import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;33import java.util.List;34public class ThenTest extends Stage<ThenTest>{35 public ThenTest the_list_of_objects_is_formatted_as_table_using_FieldBasedRowFormatter(@Table List<TestClass> list){36 return self();37 }38 public static class TestClass{39 public String name;40 public int age;41 }42}43package com.jgiven.test;44import com.tngtech.jgiven.Stage;45import com.tngtech.jgiven.annotation.Table;46import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;47import java.util.List;48public class ThenTest extends Stage<ThenTest>{49 public ThenTest the_list_of_objects_is_formatted_as_table_using_FieldBasedRowFormatter(@Table List<TestClass> list){50 return self();51 }52 public static class TestClass{53 public String name;

Full Screen

Full Screen

FieldBasedRowFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.format.table;2import com.tngtech.jgiven.annotation.Table;3import com.tngtech.jgiven.format.ArgumentFormatter;4import com.tngtech.jgiven.format.table.TableFormatter;5import com.tngtech.jgiven.format.table.TableFormatterRegistry;6import com.tngtech.jgiven.format.table.TableFormatterRegistryBuilder;7import com.tngtech.jgiven.format.table.TableFormatterRegistryFactory;8import com.tngtech.jgiven.format.table.TableFormatterRegistryFactoryImpl;9import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl;10import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl;11import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl.TableFormatterRegistryBuilderImplImpl;12import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl.TableFormatterRegistryBuilderImplImpl.TableFormatterRegistryBuilderImplImplImpl;13import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl.TableFormatterRegistryBuilderImplImpl.TableFormatterRegistryBuilderImplImplImpl.TableFormatterRegistryBuilderImplImplImplImpl;14import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl.TableFormatterRegistryBuilderImplImpl.TableFormatterRegistryBuilderImplImplImpl.TableFormatterRegistryBuilderImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImpl;15import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl.TableFormatterRegistryBuilderImplImpl.TableFormatterRegistryBuilderImplImplImpl.TableFormatterRegistryBuilderImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImplImpl;16import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl.TableFormatterRegistryBuilderImplImpl.TableFormatterRegistryBuilderImplImplImpl.TableFormatterRegistryBuilderImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImplImplImpl;17import com.tngtech.jgiven.format.table.TableFormatterRegistryImpl.TableFormatterRegistryBuilderImpl.TableFormatterRegistryBuilderImplImpl.TableFormatterRegistryBuilderImplImplImpl.TableFormatterRegistryBuilderImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImplImplImpl.TableFormatterRegistryBuilderImplImplImplImplImplImplImplImpl;18import com

Full Screen

Full Screen

FieldBasedRowFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.example;2import com.tngtech.jgiven.Stage;3import com.tngtech.jgiven.annotation.ExpectedScenarioState;4import com.tngtech.jgiven.annotation.Format;5import com.tngtech.jgiven.annotation.Table;6import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;7import com.tngtech.jgiven.format.table.TableFormatter;8import com.tngtech.jgiven.format.table.TableFormatterRegistry;9import com.tngtech.jgiven.format.table.TableRowFormatter;10import com.tngtech.jgiven.integration.spring.JGivenStage;11import com.tngtech.jgiven.report.model.TableModel;12import com.tngtech.jgiven.report.model.TableRowModel;13import com.tngtech.jgiven.report.model.ValueModel;14import org.springframework.beans.BeanUtils;15import java.util.ArrayList;16import java.util.List;17public class TableFormatterStage extends Stage<TableFormatterStage> {18 @Format(value = FieldBasedRowFormatter.class, args = { "firstName", "lastName" })19 private List<Person> people;20 public TableFormatterStage the_people_are_formatted_using_FieldBasedRowFormatter() {21 return self();22 }23 public TableFormatterStage the_people_are_formatted_using_TableRowFormatter() {24 TableFormatterRegistry.INSTANCE.register(Person.class, new TableRowFormatter<Person>() {25 public TableRowModel format(Person person) {26 TableRowModel tableRowModel = new TableRowModel();27 tableRowModel.addValue(new ValueModel(person.getFirstName()));28 tableRowModel.addValue(new ValueModel(person.getLastName()));29 return tableRowModel;30 }31 });32 return self();33 }34 public TableFormatterStage the_people_are_formatted_using_TableFormatter() {35 TableFormatterRegistry.INSTANCE.register(Person.class, new TableFormatter<Person>() {36 public TableModel format(List<Person> people) {37 TableModel tableModel = new TableModel();38 TableRowModel header = new TableRowModel();39 header.addValue(new ValueModel("First Name"));40 header.addValue(new ValueModel("Last Name"));41 tableModel.addRow(header);42 for (Person person : people) {43 TableRowModel tableRowModel = new TableRowModel();44 tableRowModel.addValue(new ValueModel(person.getFirstName()));45 tableRowModel.addValue(new ValueModel(person.getLastName()));46 tableModel.addRow(tableRowModel);47 }48 return tableModel;

Full Screen

Full Screen

FieldBasedRowFormatter

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;2import com.tngtech.jgiven.format.table.TableFormatter;3public class 1 {4 public static void main(String[] args) {5 TableFormatter tableFormatter = new FieldBasedRowFormatter();6 String table = tableFormatter.formatTable(7 );8 System.out.println(table);9 }10}11import com.tngtech.jgiven.format.table.TableFormatter;12public class 2 {13 public static void main(String[] args) {14 TableFormatter tableFormatter = new TableFormatter();15 String table = tableFormatter.formatTable(16 );17 System.out.println(table);18 }19}20import com.tngtech.jgiven.format.table.TableFormatter;21public class 3 {22 public static void main(String[] args) {23 TableFormatter tableFormatter = new TableFormatter();24 String table = tableFormatter.formatTable(25 );26 System.out.println(table);27 }28}29import com.tngtech.jgiven.format.table.TableFormatter;30public class 4 {31 public static void main(String[] args) {32 TableFormatter tableFormatter = new TableFormatter();33 String table = tableFormatter.formatTable(34 );35 System.out.println(table);36 }37}38import com.tngtech.jgiven.format.table.TableFormatter;39public class 5 {40 public static void main(String[] args) {41 TableFormatter tableFormatter = new TableFormatter();

Full Screen

Full Screen

FieldBasedRowFormatter

Using AI Code Generation

copy

Full Screen

1package com.jgiven.test;2import com.tngtech.jgiven.Stage;3import com.tngtech.jgiven.annotation.ExpectedScenarioState;4import com.tngtech.jgiven.annotation.ProvidedScenarioState;5import com.tngtech.jgiven.format.table.FieldBasedRowFormatter;6import com.tngtech.jgiven.format.table.TableFormatter;7import com.tngtech.jgiven.junit.ScenarioTest;8import org.junit.Test;9public class JGivenTest extends ScenarioTest<GivenTest, WhenTest, ThenTest> {10 public void testJGiven() {11 given().a_test_data();12 when().it_is_passed_to_the_test();13 then().the_test_passes();14 }15}16public class GivenTest extends Stage<GivenTest> {17 String test_data;18 public GivenTest a_test_data() {19 test_data = "a test data";20 return self();21 }22}23public class WhenTest extends Stage<WhenTest> {24 String test_data;25 public WhenTest it_is_passed_to_the_test() {26 return self();27 }28}29public class ThenTest extends Stage<ThenTest> {30 String test_data;31 public ThenTest the_test_passes() {32 return self();33 }34}

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.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful