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.lang.annotation.ElementType;
19  import java.lang.annotation.Retention;
20  import java.lang.annotation.RetentionPolicy;
21  import java.lang.annotation.Target;
22  
23  import org.dbunit.dataset.DataSetException;
24  import org.dbunit.dataset.IDataSet;
25  import org.dbunit.dataset.SortedDataSet;
26  import org.dbunit.operation.DatabaseOperation;
27  
28  
29  /**
30   * DbUnit test method annotation.
31   * @author kiy0taka
32   */
33  @Retention(RetentionPolicy.RUNTIME)
34  @Target(ElementType.METHOD)
35  public @interface DbUnitTest {
36  
37      /**
38       * Initial dataset file.
39       */
40      String init();
41  
42      /**
43       * Expected dataset file.
44       */
45      String expected() default "";
46  
47      /**
48       * Database setup operation.
49       */
50      Operation operation() default Operation.CLEAN_INSERT;
51  
52      /**
53       * Exclude column names.
54       * (i.e. {"empno", "emname"} or {"emp.empno", "dept.deptno"})
55       */
56      String[] excludeColumns() default "";
57  
58      /**
59       * Convert this value to null.
60       */
61      String nullValue() default "";
62  
63      /**
64       * Right trim String value.
65       */
66      boolean rtrim() default false;
67  
68      /**
69       * Sort data set.
70       */
71      Sort sort() default Sort.AUTO;
72  
73      /**
74       * Execute update before setup dataSet.
75       */
76      String[] sql() default "";
77  
78      /**
79       * Annotation of Database operation.
80       * @author kiy0taka
81       */
82      public enum Operation {
83  
84          /**
85           * @see DatabaseOperation#NONE
86           */
87          NONE(DatabaseOperation.NONE),
88  
89          /**
90           * @see DatabaseOperation#UPDATE
91           */
92          UPDATE(DatabaseOperation.UPDATE),
93  
94          /**
95           * @see DatabaseOperation#INSERT
96           */
97          INSERT(DatabaseOperation.INSERT),
98  
99          /**
100          * @see DatabaseOperation#REFRESH
101          */
102         REFRESH(DatabaseOperation.REFRESH),
103 
104         /**
105          * @see DatabaseOperation#DELETE
106          */
107         DELETE(DatabaseOperation.DELETE),
108 
109         /**
110          * @see DatabaseOperation#DELETE_ALL
111          */
112         DELETE_ALL(DatabaseOperation.DELETE_ALL),
113 
114         /**
115          * @see DatabaseOperation#TRUNCATE_TABLE
116          */
117         TRUNCATE_TABLE(DatabaseOperation.TRUNCATE_TABLE),
118 
119         /**
120          * @see DatabaseOperation#CLEAN_INSERT
121          */
122         CLEAN_INSERT(DatabaseOperation.CLEAN_INSERT);
123 
124         private DatabaseOperation operation;
125 
126         private Operation(DatabaseOperation operation) {
127             this.operation = operation;
128         }
129 
130         /**
131          * Convert to {@link DatabaseOperation}.
132          * @return {@link DatabaseOperation}
133          */
134         public DatabaseOperation toDatabaseOperation() {
135             return operation;
136         }
137     }
138 
139     /**
140      * DataSet sort storategy.
141      * @author kiy0taka
142      *
143      */
144     public enum Sort {
145 
146         /**
147          * Sort the decorated table by its own columns order.
148          * All table columns will be used.
149          */
150         AUTO {
151 
152             /**
153              * @see org.kiy0taka.dbunit.DbUnitTest.Sort#sort(org.dbunit.dataset.IDataSet)
154              */
155             public IDataSet sort(IDataSet dataSet) throws DataSetException {
156                 return new SortedDataSet(dataSet);
157             }
158         },
159 
160         /**
161          * No sort.
162          */
163         NONE {
164 
165             /**
166              * @see org.kiy0taka.dbunit.DbUnitTest.Sort#sort(org.dbunit.dataset.IDataSet)
167              */
168             public IDataSet sort(IDataSet dataSet) {
169                 return dataSet;
170             }
171         };
172 
173         /**
174          * Sort Data set.
175          * @param dataSet Data set
176          * @return sorted data set
177          */
178         public abstract IDataSet sort(IDataSet dataSet) throws DataSetException;
179     }
180 }