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