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