01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: TestCmfValidation.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf;
09:
10: import com.uwyn.rife.cmf.dam.ContentImage;
11: import com.uwyn.rife.cmf.validation.SupportedXhtml;
12: import com.uwyn.rife.resources.ResourceFinderClasspath;
13: import com.uwyn.rife.site.ConstrainedProperty;
14: import com.uwyn.rife.site.ValidationRule;
15: import com.uwyn.rife.site.ValidationRuleNotEmpty;
16: import com.uwyn.rife.site.ValidationRuleNotNull;
17: import com.uwyn.rife.tools.FileUtils;
18: import java.net.URL;
19: import java.util.Iterator;
20: import junit.framework.TestCase;
21:
22: public class TestCmfValidation extends TestCase {
23: public TestCmfValidation(String name) {
24: super (name);
25: }
26:
27: public void testInstantiation() {
28: CmfValidation validation = new CmfValidation();
29: assertEquals(0, validation.getConstrainedProperties().size());
30: assertEquals(0, validation.getRules().size());
31: }
32:
33: public void testMimeTypeConstraint() {
34: CmfValidation validation = new CmfValidation();
35: ConstrainedProperty property = new ConstrainedProperty(
36: "property").mimeType(MimeType.APPLICATION_XHTML)
37: .notNull(true);
38:
39: validation.addConstraint(property);
40:
41: assertEquals(1, validation.getConstrainedProperties().size());
42: assertEquals(2, validation.getRules().size());
43: assertSame(property, validation.getConstrainedProperties()
44: .iterator().next());
45: Iterator<ValidationRule> it = validation.getRules().iterator();
46: assertTrue(it.next() instanceof ValidationRuleNotNull);
47: assertTrue(it.next() instanceof SupportedXhtml);
48: }
49:
50: public void testNoCmfConstraints() {
51: CmfValidation validation = new CmfValidation();
52: ConstrainedProperty property = new ConstrainedProperty(
53: "property").notEmpty(true);
54:
55: validation.addConstraint(property);
56:
57: assertEquals(1, validation.getConstrainedProperties().size());
58: assertEquals(1, validation.getRules().size());
59: assertSame(property, validation.getConstrainedProperties()
60: .iterator().next());
61: Iterator<ValidationRule> it = validation.getRules().iterator();
62: assertTrue(it.next() instanceof ValidationRuleNotEmpty);
63: }
64:
65: public void testLoadingErrors() throws Exception {
66: URL image_resource_gif = ResourceFinderClasspath.getInstance()
67: .getResource("uwyn.gif");
68: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
69: ContentImage content = new ContentImage().name(
70: "the content name").image(data_image_gif);
71: assertTrue(content.validate());
72: assertNull(content.getLoadingErrors("image"));
73:
74: byte[] data_image_invalid = new byte[] { 12, 4, 34, 3, 23 }; // random invalid bytes
75: content.setImage(data_image_invalid);
76: assertFalse(content.validate());
77: assertTrue(content.getLoadingErrors("image").size() > 0);
78: }
79: }
|