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: TestSupportedXhtml.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.site.ConstrainedProperty;
012: import com.uwyn.rife.site.Validation;
013: import junit.framework.TestCase;
014:
015: public class TestSupportedXhtml extends TestCase {
016: public TestSupportedXhtml(String name) {
017: super (name);
018: }
019:
020: public void testValidateNull() throws Exception {
021: SupportedXhtml rule = new SupportedXhtml("xhtml", true);
022: XhtmlBean bean = new XhtmlBean();
023: ConstrainedProperty property = new ConstrainedProperty("xhtml")
024: .mimeType(MimeType.APPLICATION_XHTML);
025: bean.addConstraint(property);
026: rule.setBean(bean);
027: assertTrue(rule.validate());
028: assertNull(rule.getLoadingErrors());
029: assertNull(property.getCachedLoadedData());
030: }
031:
032: public void testValidateSupportedDocument() throws Exception {
033: SupportedXhtml rule = new SupportedXhtml("xhtml", false);
034: XhtmlBean bean = new XhtmlBean();
035: ConstrainedProperty property = new ConstrainedProperty("xhtml")
036: .mimeType(MimeType.APPLICATION_XHTML);
037: bean.addConstraint(property);
038: bean
039: .setXhtml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
040: + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
041: + "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title></title></head><body>\n"
042: + "<p>body</p>\n" + "</body></html>");
043: rule.setBean(bean);
044: assertTrue(rule.validate());
045: assertNull(rule.getLoadingErrors());
046: assertNotNull(property.getCachedLoadedData());
047: }
048:
049: public void testValidateUnsupportedDocument() throws Exception {
050: SupportedXhtml rule = new SupportedXhtml("xhtml", false);
051: XhtmlBean bean = new XhtmlBean();
052: ConstrainedProperty property = new ConstrainedProperty("xhtml")
053: .mimeType(MimeType.APPLICATION_XHTML);
054: bean.addConstraint(property);
055: bean.setXhtml("<p>some <b>html</b> here</p>");
056: rule.setBean(bean);
057: assertFalse(rule.validate());
058: assertTrue(rule.getLoadingErrors().size() > 0);
059: assertNull(property.getCachedLoadedData());
060: }
061:
062: public void testValidateSupportedFragment() throws Exception {
063: SupportedXhtml rule = new SupportedXhtml("xhtml", true);
064: XhtmlBean bean = new XhtmlBean();
065: ConstrainedProperty property = new ConstrainedProperty("xhtml")
066: .mimeType(MimeType.APPLICATION_XHTML);
067: bean.addConstraint(property);
068: bean.setXhtml("<p>some <b>html</b> here</p>");
069: rule.setBean(bean);
070: assertTrue(rule.validate());
071: assertNull(rule.getLoadingErrors());
072: assertNotNull(property.getCachedLoadedData());
073: }
074:
075: public void testValidateUnsupportedFragment() throws Exception {
076: SupportedXhtml rule = new SupportedXhtml("xhtml", true);
077: XhtmlBean bean = new XhtmlBean();
078: ConstrainedProperty property = new ConstrainedProperty("xhtml")
079: .mimeType(MimeType.APPLICATION_XHTML);
080: bean.addConstraint(property);
081: bean.setXhtml("<i><b>error</i>");
082: rule.setBean(bean);
083: assertFalse(rule.validate());
084: assertTrue(rule.getLoadingErrors().size() > 0);
085: assertNull(property.getCachedLoadedData());
086: }
087:
088: public void testValidateNotConstrained() throws Exception {
089: SupportedXhtml rule = new SupportedXhtml("xhtml", true);
090: XhtmlBeanNotConstrained bean = new XhtmlBeanNotConstrained();
091: bean.setXhtml("<p>some <b>html</b> here</p>");
092: rule.setBean(bean);
093: assertTrue(rule.validate());
094: assertNull(rule.getLoadingErrors());
095: }
096:
097: public void testValidateNotCmfProperty() throws Exception {
098: SupportedXhtml rule = new SupportedXhtml("xhtml", true);
099: XhtmlBeanValidation bean = new XhtmlBeanValidation();
100: ConstrainedProperty property = new ConstrainedProperty("xhtml");
101: bean.addConstraint(property);
102: bean.setXhtml("<p>some <b>html</b> here</p>");
103: rule.setBean(bean);
104: assertTrue(rule.validate());
105: assertNull(rule.getLoadingErrors());
106: }
107:
108: public void testValidateUnknownProperty() throws Exception {
109: SupportedXhtml rule = new SupportedXhtml("xhtml_unknown", true);
110: XhtmlBean bean = new XhtmlBean();
111: rule.setBean(bean);
112: assertTrue(rule.validate());
113: assertNull(rule.getLoadingErrors());
114: }
115:
116: public void testGetError() throws Exception {
117: SupportedXhtml rule = new SupportedXhtml("xhtml", true);
118: assertEquals("xhtml", rule.getError().getSubject());
119: assertEquals("INVALID", rule.getError().getIdentifier());
120: }
121:
122: public static class XhtmlBean extends Validation {
123: private String mXhtml = null;
124:
125: public XhtmlBean() {
126: }
127:
128: public String getXhtml() {
129: return mXhtml;
130: }
131:
132: public void setXhtml(String xhtml) {
133: mXhtml = xhtml;
134: }
135: }
136:
137: public static class XhtmlBeanValidation extends Validation {
138: private String mXhtml = null;
139:
140: public XhtmlBeanValidation() {
141: }
142:
143: public String getXhtml() {
144: return mXhtml;
145: }
146:
147: public void setXhtml(String xhtml) {
148: mXhtml = xhtml;
149: }
150: }
151:
152: public static class XhtmlBeanNotConstrained {
153: private String mXhtml = null;
154:
155: public XhtmlBeanNotConstrained() {
156: }
157:
158: public String getXhtml() {
159: return mXhtml;
160: }
161:
162: public void setXhtml(String xhtml) {
163: mXhtml = xhtml;
164: }
165: }
166: }
|