1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.validator.validators;
17  
18  import static org.seasar.cubby.validator.validators.ScalarFieldValidatorAssert.assertFail;
19  import static org.seasar.cubby.validator.validators.ScalarFieldValidatorAssert.assertSuccess;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.io.UnsupportedEncodingException;
26  import java.util.regex.Pattern;
27  
28  import org.apache.commons.fileupload.FileItem;
29  import org.junit.Test;
30  import org.seasar.cubby.validator.ScalarFieldValidator;
31  
32  public class FileRegexpValidatorTest {
33  
34  	@Test
35  	public void validate1() {
36  		ScalarFieldValidator validator = new FileRegexpValidator("a.*34");
37  		assertSuccess(validator, null, "", new MockFileItem("a5634"));
38  		assertFail(validator, new MockFileItem("b5634"));
39  	}
40  
41  	@Test
42  	public void validate2() {
43  		ScalarFieldValidator validator = new FileRegexpValidator(Pattern
44  				.compile("(?i)a.*34"));
45  		assertSuccess(validator, null, "", new MockFileItem("a5634"));
46  		assertSuccess(validator, null, "", new MockFileItem("A5634"));
47  		assertFail(validator, new MockFileItem("b5634"));
48  	}
49  
50  	static class MockFileItem implements FileItem {
51  
52  		private static final long serialVersionUID = 1L;
53  
54  		private String name;
55  
56  		public MockFileItem(String name) {
57  			this.name = name;
58  		}
59  
60  		public void delete() {
61  		}
62  
63  		public byte[] get() {
64  			return null;
65  		}
66  
67  		public String getContentType() {
68  			return null;
69  		}
70  
71  		public String getFieldName() {
72  			return null;
73  		}
74  
75  		public InputStream getInputStream() throws IOException {
76  			return null;
77  		}
78  
79  		public String getName() {
80  			return this.name;
81  		}
82  
83  		public OutputStream getOutputStream() throws IOException {
84  			return null;
85  		}
86  
87  		public long getSize() {
88  			return 0;
89  		}
90  
91  		public String getString() {
92  			return null;
93  		}
94  
95  		public String getString(String encoding)
96  				throws UnsupportedEncodingException {
97  			return null;
98  		}
99  
100 		public boolean isFormField() {
101 			return false;
102 		}
103 
104 		public boolean isInMemory() {
105 			return false;
106 		}
107 
108 		public void setFieldName(String name) {
109 		}
110 
111 		public void setFormField(boolean state) {
112 		}
113 
114 		public void write(File file) throws Exception {
115 		}
116 	}
117 }