public FormatterLineAggregator<T> build() { Assert.notNull(this.format, "A format is required"); Assert.isTrue((this.names != null && !this.names.isEmpty()) || this.fieldExtractor != null, "A list of field names or a field extractor is required"); FormatterLineAggregator<T> formatterLineAggregator = new FormatterLineAggregator<>(); formatterLineAggregator.setFormat(this.format); formatterLineAggregator.setLocale(this.locale); formatterLineAggregator.setMinimumLength(this.minimumLength); formatterLineAggregator.setMaximumLength(this.maximumLength); if (this.fieldExtractor == null) { BeanWrapperFieldExtractor<T> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>(); beanWrapperFieldExtractor.setNames(this.names.toArray(new String[this.names.size()])); try { beanWrapperFieldExtractor.afterPropertiesSet(); } catch (Exception e) { throw new IllegalStateException("Unable to initialize FormatterLineAggregator", e); } this.fieldExtractor = beanWrapperFieldExtractor; } formatterLineAggregator.setFieldExtractor(this.fieldExtractor); return formatterLineAggregator; } }
/** * Test aggregation with center alignment */ @Test public void testAggregateFormattedCenter() { String[] args = { "Matchsize", "Smallsize" }; aggregator.setFormat("%13s%12s"); aggregator.setMinimumLength(25); aggregator.setMaximumLength(25); aggregator.setFieldExtractor(new FieldExtractor<String[]>() { private int[] widths = new int[] { 13, 12 }; @Override public Object[] extract(String[] item) { String[] strings = new String[item.length]; for (int i = 0; i < strings.length; i++) { strings[i] = item[i]; if (item[i].length() < widths[i]) { StringBuilder buffer = new StringBuilder(strings[i]); for (int j = 0; j < (widths[i] - item[i].length() + 1) / 2; j++) { buffer.append(" "); } strings[i] = buffer.toString(); } } return strings; } }); String result = aggregator.aggregate(args); assertEquals(" Matchsize Smallsize ", result); }
@Before public void setup() { aggregator = new FormatterLineAggregator<>(); aggregator.setFieldExtractor(defaultFieldExtractor); }
/** * Text length exceeds the length of the column. */ @Test public void testAggregateInvalidInputLength() { String[] args = { "Oversize" }; aggregator.setMaximumLength(3); aggregator.setFormat("%3s"); try { aggregator.aggregate(args); fail("Invalid text length, exception should have been thrown"); } catch (IllegalStateException expected) { // expected } }
/** * If no ranges are specified, IllegalArgumentException is thrown */ @Test public void testAggregateNullRecordDescriptor() { String[] args = { "does not matter what is here" }; try { aggregator.aggregate(args); fail("should not work with no format specified"); } catch (IllegalArgumentException expected) { // expected } }
public FormatterLineAggregator<T> build() { Assert.notNull(this.format, "A format is required"); Assert.isTrue((this.names != null && !this.names.isEmpty()) || this.fieldExtractor != null, "A list of field names or a field extractor is required"); FormatterLineAggregator<T> formatterLineAggregator = new FormatterLineAggregator<>(); formatterLineAggregator.setFormat(this.format); formatterLineAggregator.setLocale(this.locale); formatterLineAggregator.setMinimumLength(this.minimumLength); formatterLineAggregator.setMaximumLength(this.maximumLength); if (this.fieldExtractor == null) { BeanWrapperFieldExtractor<T> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>(); beanWrapperFieldExtractor.setNames(this.names.toArray(new String[this.names.size()])); try { beanWrapperFieldExtractor.afterPropertiesSet(); } catch (Exception e) { throw new IllegalStateException("Unable to initialize FormatterLineAggregator", e); } this.fieldExtractor = beanWrapperFieldExtractor; } formatterLineAggregator.setFieldExtractor(this.fieldExtractor); return formatterLineAggregator; } }
/** * Test aggregation with left alignment */ @Test public void testAggregateWithCustomPadding() { String[] args = { "Matchsize", "Smallsize" }; aggregator.setFormat("%13s%11s"); aggregator.setMinimumLength(24); aggregator.setMaximumLength(24); aggregator.setFieldExtractor(new FieldExtractor<String[]>() { private int[] widths = new int[] { 13, 11 }; @Override public Object[] extract(String[] item) { String[] strings = new String[item.length]; for (int i = 0; i < strings.length; i++) { strings[i] = item[i]; if (item[i].length() < widths[i]) { StringBuilder buffer = new StringBuilder(strings[i]); for (int j = 0; j < widths[i] - item[i].length(); j++) { buffer.append("."); } strings[i] = buffer.toString(); } } return strings; } }); String result = aggregator.aggregate(args); assertEquals("Matchsize....Smallsize..", result); }
/** * Test aggregation with right alignment */ @Test public void testAggregateFormattedRight() { String[] args = { "Matchsize", "Smallsize" }; aggregator.setFormat("%13s%10s"); String result = aggregator.aggregate(args); assertEquals(23, result.length()); assertEquals(" Matchsize Smallsize", result); }