View Javadoc

1   /**
2    * Copyright (C) 2009 kiy0taka.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kiy0taka.dbunit;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.regex.Pattern;
25  
26  import org.dbunit.dataset.DataSetException;
27  import org.dbunit.dataset.DefaultDataSet;
28  import org.dbunit.dataset.IDataSet;
29  import org.dbunit.dataset.ITable;
30  import org.dbunit.dataset.ReplacementDataSet;
31  import org.dbunit.dataset.ReplacementTable;
32  import org.dbunit.dataset.filter.DefaultColumnFilter;
33  
34  /**
35   * DataSet Builder.
36   * @author kiy0taka
37   */
38  public class DataSetBuilder {
39  
40      private final IDataSet dataSet;
41  
42      private final Map<Object, Object> replaceMap = new HashMap<Object, Object>();
43  
44      private final List<String> defaultExcludeColumns = new ArrayList<String>();
45  
46      private final Map<String, List<String>> tableExcludeColumns = new HashMap<String, List<String>>();
47  
48      private boolean isTrim;
49  
50      /**
51       * Create new Builder.
52       * @param dataSet {@link IDataSet}
53       */
54      public DataSetBuilder(IDataSet dataSet) {
55          this.dataSet = dataSet;
56      }
57  
58      /**
59       * Add null replacement.
60       * @param nullValue value to replace null
61       * @return this builder
62       */
63      public DataSetBuilder nullValue(Object nullValue) {
64          return replacement(nullValue, null);
65      }
66  
67      /**
68       * Add replacement.
69       * @param orig Original value
70       * @param replacement replacement value
71       * @return this builder
72       */
73      public DataSetBuilder replacement(Object orig, Object replacement) {
74          replaceMap.put(orig, replacement);
75          return this;
76      }
77  
78      /**
79       * Add exclude columns.
80       * @param excludeColumnNames exclude column names (i. e. "empno", "emp.empno")
81       * @return this builder
82       */
83      public DataSetBuilder excludeColumns(String... excludeColumnNames) {
84          for (String columnName : excludeColumnNames) {
85              int dotIndex = columnName.indexOf('.');
86              if (dotIndex > 0) {
87                  String tableName = columnName.substring(0, dotIndex);
88                  List<String> columnNames = tableExcludeColumns.get(tableName);
89                  if (columnNames == null) {
90                      columnNames = new ArrayList<String>();
91                      tableExcludeColumns.put(tableName, columnNames);
92                  }
93                  columnNames.add(columnName.substring(dotIndex + 1));
94              } else {
95                  defaultExcludeColumns.add(columnName);
96              }
97          }
98          return this;
99      }
100 
101     /**
102      * RTrim string value.
103      * @param trim trim
104      * @return this builder
105      */
106     public DataSetBuilder rtrim(boolean trim) {
107         this.isTrim = trim;
108         return this;
109     }
110 
111     /**
112      * Convert this to {@link IDataSet}.
113      * @return converted {@link IDataSet}
114      * @throws DataSetException DataSet creation failure.
115      */
116     public IDataSet toDataSet() throws DataSetException {
117         IDataSet result = dataSet;
118         if (!replaceMap.isEmpty()) {
119             result = new ReplacementDataSet(result, replaceMap, null);
120         }
121         if (!defaultExcludeColumns.isEmpty() || !tableExcludeColumns.isEmpty()) {
122             DefaultDataSet defaultDataSet = new DefaultDataSet();
123             for (String tableName : result.getTableNames()) {
124                 Set<String> excludeColumnNameSet = new HashSet<String>(defaultExcludeColumns);
125                 List<String> excludeColumns = tableExcludeColumns.get(tableName);
126                 if (excludeColumns != null) {
127                     excludeColumnNameSet.addAll(excludeColumns);
128                 }
129                 defaultDataSet.addTable(DefaultColumnFilter.excludedColumnsTable(result.getTable(tableName),
130                         excludeColumnNameSet.toArray(new String[excludeColumnNameSet.size()])));
131             }
132             result = defaultDataSet;
133         }
134         if (isTrim) {
135             DefaultDataSet defaultDataSet = new DefaultDataSet();
136             for (String tableName : result.getTableNames()) {
137                 defaultDataSet.addTable(new RTrimTable(result.getTable(tableName)));
138             }
139             result = defaultDataSet;
140         }
141         return result;
142     }
143 
144     /**
145      * Create new DataSetBuilder.
146      * @param dataSet {@link IDataSet}
147      * @return new DataSetBuilder
148      */
149     public static DataSetBuilder dataSet(IDataSet dataSet) {
150         return new DataSetBuilder(dataSet);
151     }
152 
153     private static class RTrimTable extends ReplacementTable {
154 
155         private static final Pattern RTRIM = Pattern.compile(" +$");
156         public RTrimTable(ITable table) {
157             super(table);
158         }
159 
160         @Override
161         public Object getValue(int row, String column) throws DataSetException {
162             Object result = super.getValue(row, column);
163             if (result instanceof String) {
164                 return RTRIM.matcher((String) result).replaceAll("");
165             }
166             return result;
167         }
168     }
169 }