/
ExcelInputMetaTest.java
160 lines (145 loc) · 7.06 KB
/
ExcelInputMetaTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2016 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.di.trans.steps.excelinput;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.trans.steps.loadsave.LoadSaveTester;
import org.pentaho.di.trans.steps.loadsave.validator.ArrayLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.FieldLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.IntLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.PrimitiveIntArrayLoadSaveValidator;
import org.pentaho.di.trans.steps.loadsave.validator.StringLoadSaveValidator;
public class ExcelInputMetaTest {
LoadSaveTester loadSaveTester;
@Before
public void setUp() throws Exception {
KettleEnvironment.init();
PluginRegistry.init( true );
List<String> attributes =
Arrays.asList( "fileName", "fileMask", "excludeFileMask", "fileRequired", "includeSubFolders", "field",
"sheetName", "startRow", "startColumn", "spreadSheetType", "fileField", "sheetField", "sheetRowNumberField",
"rowNumberField", "shortFileFieldName", "extensionFieldName", "pathFieldName", "sizeFieldName",
"hiddenFieldName", "lastModificationTimeFieldName", "uriNameFieldName", "rootUriNameFieldName" );
Map<String, String> getterMap = new HashMap<String, String>() {
{
put( "excludeFileMask", "getExludeFileMask" );
put( "shortFileFieldName", "getShortFileNameField" );
put( "extensionFieldName", "getExtensionField" );
put( "pathFieldName", "getPathField" );
put( "sizeFieldName", "getSizeField" );
put( "hiddenFieldName", "isHiddenField" );
put( "lastModificationTimeFieldName", "getLastModificationDateField" );
put( "uriNameFieldName", "getUriField" );
put( "rootUriNameFieldName", "getRootUriField" );
}
};
Map<String, String> setterMap = new HashMap<String, String>() {
{
put( "shortFileFieldName", "setShortFileNameField" );
put( "extensionFieldName", "setExtensionField" );
put( "pathFieldName", "setPathField" );
put( "sizeFieldName", "setSizeField" );
put( "hiddenFieldName", "setIsHiddenField" );
put( "lastModificationTimeFieldName", "setLastModificationDateField" );
put( "uriNameFieldName", "setUriField" );
put( "rootUriNameFieldName", "setRootUriField" );
}
};
FieldLoadSaveValidator<String[]> stringArrayLoadSaveValidator =
new ArrayLoadSaveValidator<String>( new StringLoadSaveValidator(), 5 );
Map<String, FieldLoadSaveValidator<?>> attrValidatorMap = new HashMap<String, FieldLoadSaveValidator<?>>();
attrValidatorMap.put( "fileName", stringArrayLoadSaveValidator );
attrValidatorMap.put( "sheetName", stringArrayLoadSaveValidator );
attrValidatorMap.put( "fileMask", stringArrayLoadSaveValidator );
attrValidatorMap.put( "excludeFileMask", stringArrayLoadSaveValidator );
attrValidatorMap.put( "fileRequired", stringArrayLoadSaveValidator );
attrValidatorMap.put( "includeSubFolders", stringArrayLoadSaveValidator );
attrValidatorMap.put( "field",
new ArrayLoadSaveValidator<ExcelInputField>( new ExcelInputFieldLoadSaveValidator(), 5 ) );
attrValidatorMap.put( "spreadSheetType", new SpreadSheetTypeFieldLoadSaveValidator() );
Map<String, FieldLoadSaveValidator<?>> typeValidatorMap = new HashMap<String, FieldLoadSaveValidator<?>>();
typeValidatorMap.put( int[].class.getCanonicalName(), new PrimitiveIntArrayLoadSaveValidator(
new IntLoadSaveValidator(), 5 ) );
loadSaveTester =
new LoadSaveTester( ExcelInputMeta.class, attributes, getterMap, setterMap, attrValidatorMap, typeValidatorMap );
}
@Test
public void testSerialization() throws KettleException {
loadSaveTester.testSerialization();
}
public class ExcelInputFieldLoadSaveValidator implements FieldLoadSaveValidator<ExcelInputField> {
final Random rand = new Random();
@Override
public ExcelInputField getTestObject() {
ExcelInputField rtn = new ExcelInputField( );
rtn.setCurrencySymbol( UUID.randomUUID().toString() );
rtn.setDecimalSymbol( UUID.randomUUID().toString() );
rtn.setFormat( UUID.randomUUID().toString() );
rtn.setGroupSymbol( UUID.randomUUID().toString() );
rtn.setName( UUID.randomUUID().toString() );
rtn.setTrimType( rand.nextInt( 4 ) );
rtn.setPrecision( rand.nextInt( 9 ) );
rtn.setRepeated( rand.nextBoolean() );
rtn.setLength( rand.nextInt( 50 ) );
rtn.setType( rand.nextInt( 5 ) + 1 );
return rtn;
}
@Override
public boolean validateTestObject( ExcelInputField testObject, Object actual ) {
if ( !( actual instanceof ExcelInputField) ) {
return false;
}
ExcelInputField another = (ExcelInputField) actual;
return new EqualsBuilder()
.append( testObject.getName(), another.getName() )
.append( testObject.getType(), another.getType() )
.append( testObject.getLength(), another.getLength() )
.append( testObject.getFormat(), another.getFormat() )
.append( testObject.getTrimType(), another.getTrimType() )
.append( testObject.getPrecision(), another.getPrecision() )
.append( testObject.getCurrencySymbol(), another.getCurrencySymbol() )
.append( testObject.getDecimalSymbol(), another.getDecimalSymbol() )
.append( testObject.getGroupSymbol(), another.getGroupSymbol() )
.append( testObject.isRepeated(), another.isRepeated() )
.isEquals();
}
}
public class SpreadSheetTypeFieldLoadSaveValidator implements FieldLoadSaveValidator<SpreadSheetType> {
@Override public SpreadSheetType getTestObject() {
return SpreadSheetType.POI;
}
@Override public boolean validateTestObject( SpreadSheetType testObject, Object actual ) {
return true;
}
}
// Note - removed cloneTest as it's now covered by the load/save tester.
}