001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestSupportedImage.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.validation;
009:
010: import com.uwyn.rife.cmf.MimeType;
011: import com.uwyn.rife.resources.ResourceFinderClasspath;
012: import com.uwyn.rife.site.ConstrainedProperty;
013: import com.uwyn.rife.site.Validation;
014: import com.uwyn.rife.tools.FileUtils;
015: import java.net.URL;
016: import junit.framework.TestCase;
017:
018: public class TestSupportedImage extends TestCase {
019: public TestSupportedImage(String name) {
020: super (name);
021: }
022:
023: public void testValidateNull() throws Exception {
024: SupportedImage rule = new SupportedImage("image");
025: ImageBean bean = new ImageBean();
026: ConstrainedProperty property = new ConstrainedProperty("image")
027: .mimeType(MimeType.IMAGE_PNG);
028: bean.addConstraint(property);
029: rule.setBean(bean);
030: assertTrue(rule.validate());
031: assertNull(rule.getLoadingErrors());
032: assertNull(property.getCachedLoadedData());
033: }
034:
035: public void testValidateSupported() throws Exception {
036: SupportedImage rule = new SupportedImage("image");
037: ImageBean bean = new ImageBean();
038: ConstrainedProperty property = new ConstrainedProperty("image")
039: .mimeType(MimeType.IMAGE_PNG);
040: bean.addConstraint(property);
041: URL image_resource = ResourceFinderClasspath.getInstance()
042: .getResource("uwyn.png");
043: byte[] image_bytes = FileUtils.readBytes(image_resource);
044: bean.setImage(image_bytes);
045: rule.setBean(bean);
046: assertTrue(rule.validate());
047: assertNull(rule.getLoadingErrors());
048: assertNotNull(property.getCachedLoadedData());
049: }
050:
051: public void testValidateUnsupported() throws Exception {
052: SupportedImage rule = new SupportedImage("image");
053: ImageBean bean = new ImageBean();
054: ConstrainedProperty property = new ConstrainedProperty("image")
055: .mimeType(MimeType.IMAGE_PNG);
056: bean.addConstraint(property);
057: byte[] image_bytes = new byte[] { 2, 9, 7, 12, 45 }; // just random values
058: bean.setImage(image_bytes);
059: rule.setBean(bean);
060: assertFalse(rule.validate());
061: assertTrue(rule.getLoadingErrors().size() > 0);
062: assertNull(property.getCachedLoadedData());
063: }
064:
065: public void testValidateNotConstrained() throws Exception {
066: SupportedImage rule = new SupportedImage("image");
067: ImageBeanNotConstrained bean = new ImageBeanNotConstrained();
068: URL image_resource = ResourceFinderClasspath.getInstance()
069: .getResource("uwyn.png");
070: byte[] image_bytes = FileUtils.readBytes(image_resource);
071: bean.setImage(image_bytes);
072: rule.setBean(bean);
073: assertTrue(rule.validate());
074: assertNull(rule.getLoadingErrors());
075: }
076:
077: public void testValidateNotCmfProperty() throws Exception {
078: SupportedImage rule = new SupportedImage("image");
079: ImageBeanValidation bean = new ImageBeanValidation();
080: ConstrainedProperty property = new ConstrainedProperty("image");
081: bean.addConstraint(property);
082: URL image_resource = ResourceFinderClasspath.getInstance()
083: .getResource("uwyn.png");
084: byte[] image_bytes = FileUtils.readBytes(image_resource);
085: bean.setImage(image_bytes);
086: rule.setBean(bean);
087: assertTrue(rule.validate());
088: assertNull(rule.getLoadingErrors());
089: }
090:
091: public void testValidateUnknownProperty() throws Exception {
092: SupportedImage rule = new SupportedImage("image_unknown");
093: ImageBean bean = new ImageBean();
094: rule.setBean(bean);
095: assertTrue(rule.validate());
096: assertNull(rule.getLoadingErrors());
097: }
098:
099: public void testGetError() throws Exception {
100: SupportedImage rule = new SupportedImage("image");
101: assertEquals("image", rule.getError().getSubject());
102: assertEquals("INVALID", rule.getError().getIdentifier());
103: }
104:
105: public static class ImageBean extends Validation {
106: private byte[] mImage = null;
107:
108: public ImageBean() {
109: }
110:
111: public byte[] getImage() {
112: return mImage;
113: }
114:
115: public void setImage(byte[] image) {
116: mImage = image;
117: }
118: }
119:
120: public static class ImageBeanNotConstrained {
121: private byte[] mImage = null;
122:
123: public ImageBeanNotConstrained() {
124: }
125:
126: public byte[] getImage() {
127: return mImage;
128: }
129:
130: public void setImage(byte[] image) {
131: mImage = image;
132: }
133: }
134:
135: public static class ImageBeanValidation extends Validation {
136: private byte[] mImage = null;
137:
138: public ImageBeanValidation() {
139: }
140:
141: public byte[] getImage() {
142: return mImage;
143: }
144:
145: public void setImage(byte[] image) {
146: mImage = image;
147: }
148: }
149: }
|