001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal;
016:
017: import org.apache.tapestry.Field;
018: import org.apache.tapestry.MarkupWriter;
019: import org.apache.tapestry.ValidationDecorator;
020: import org.apache.tapestry.ValidationTracker;
021: import org.apache.tapestry.dom.Element;
022: import org.apache.tapestry.dom.XMLMarkupModel;
023: import org.apache.tapestry.internal.services.MarkupWriterImpl;
024: import org.apache.tapestry.services.Environment;
025: import org.apache.tapestry.test.TapestryTestCase;
026: import org.testng.annotations.Test;
027:
028: public class DefaultValidationDecoratorTest extends TapestryTestCase {
029: @Test
030: public void label_has_no_field() {
031: Environment env = mockEnvironment();
032:
033: replay();
034:
035: ValidationDecorator decorator = new DefaultValidationDecorator(
036: env, null, null);
037:
038: decorator.insideLabel(null, null);
039:
040: verify();
041: }
042:
043: @Test
044: public void label_error_no_existing_class_attribute() {
045: MarkupWriter writer = new MarkupWriterImpl(
046: new XMLMarkupModel(), null);
047: Environment env = mockEnvironment();
048: Field field = mockField();
049: ValidationTracker tracker = mockValidationTracker();
050:
051: train_peekRequired(env, ValidationTracker.class, tracker);
052: train_inError(tracker, field, true);
053:
054: replay();
055:
056: Element e = writer.element("label", "accesskey", "f");
057:
058: ValidationDecorator decorator = new DefaultValidationDecorator(
059: env, null, null);
060:
061: decorator.insideLabel(field, e);
062:
063: assertEquals(writer.toString(),
064: "<label accesskey=\"f\" class=\"t-error\"/>");
065:
066: verify();
067: }
068:
069: @Test
070: public void label_error_with_existing_class_attribute() {
071: MarkupWriter writer = new MarkupWriterImpl(
072: new XMLMarkupModel(), null);
073: Environment env = mockEnvironment();
074: Field field = mockField();
075: ValidationTracker tracker = mockValidationTracker();
076:
077: train_peekRequired(env, ValidationTracker.class, tracker);
078: train_inError(tracker, field, true);
079:
080: replay();
081:
082: Element e = writer.element("label", "accesskey", "f", "class",
083: "foo");
084:
085: ValidationDecorator decorator = new DefaultValidationDecorator(
086: env, null, null);
087:
088: decorator.insideLabel(field, e);
089:
090: assertEquals(writer.toString(),
091: "<label accesskey=\"f\" class=\"foo t-error\"/>");
092:
093: verify();
094: }
095:
096: @Test
097: public void field_error() {
098: MarkupWriter writer = new MarkupWriterImpl(
099: new XMLMarkupModel(), null);
100: Environment env = mockEnvironment();
101: Field field = mockField();
102: ValidationTracker tracker = mockValidationTracker();
103:
104: train_peekRequired(env, ValidationTracker.class, tracker);
105: train_inError(tracker, field, true);
106: train_peekRequired(env, MarkupWriter.class, writer);
107:
108: replay();
109:
110: writer.element("input", "type", "text", "name", "ex", "class",
111: "foo", "value", "freddy", "size", "30");
112:
113: ValidationDecorator decorator = new DefaultValidationDecorator(
114: env, null, null);
115:
116: decorator.insideField(field);
117:
118: assertEquals(
119: writer.toString(),
120: "<input class=\"foo t-error\" name=\"ex\" size=\"30\" type=\"text\" value=\"freddy\"/>");
121:
122: verify();
123: }
124:
125: @Test
126: public void field_ok() {
127: Environment env = mockEnvironment();
128: Field field = mockField();
129: ValidationTracker tracker = mockValidationTracker();
130:
131: train_peekRequired(env, ValidationTracker.class, tracker);
132: train_inError(tracker, field, false);
133:
134: replay();
135:
136: ValidationDecorator decorator = new DefaultValidationDecorator(
137: env, null, null);
138:
139: decorator.insideField(field);
140:
141: verify();
142: }
143:
144: @Test
145: public void label_when_field_not_in_error() {
146: Environment env = mockEnvironment();
147: Field field = mockField();
148: ValidationTracker tracker = mockValidationTracker();
149:
150: train_peekRequired(env, ValidationTracker.class, tracker);
151: train_inError(tracker, field, false);
152:
153: replay();
154:
155: ValidationDecorator decorator = new DefaultValidationDecorator(
156: env, null, null);
157:
158: decorator.insideLabel(field, null);
159:
160: verify();
161: }
162: }
|