001 // Copyright 2005 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.form.validator; 016 017 import static org.easymock.EasyMock.expect; 018 019 import org.apache.tapestry.IMarkupWriter; 020 import org.apache.tapestry.IRequestCycle; 021 import org.apache.tapestry.form.FormComponentContributorContext; 022 import org.apache.tapestry.form.IFormComponent; 023 import org.apache.tapestry.form.ValidationMessages; 024 import org.apache.tapestry.json.JSONObject; 025 import org.apache.tapestry.util.RegexpMatcher; 026 import org.apache.tapestry.valid.ValidationConstraint; 027 import org.apache.tapestry.valid.ValidationStrings; 028 import org.apache.tapestry.valid.ValidatorException; 029 import org.testng.annotations.Test; 030 031 /** 032 * Tests for {@link org.apache.tapestry.form.validator.Pattern} 033 * 034 * @author Howard Lewis Ship 035 * @since 4.0 036 */ 037 @Test 038 public class TestPattern extends BaseValidatorTestCase 039 { 040 041 public void test_OK() throws Exception 042 { 043 IFormComponent field = newField(); 044 ValidationMessages messages = newMessages(); 045 046 replay(); 047 048 new Pattern("pattern=\\d+").validate(field, messages, "1232"); 049 050 verify(); 051 } 052 053 public void test_Fail() 054 { 055 String pattern = "\\d+"; 056 IFormComponent field = newField("My Pattern"); 057 ValidationMessages messages = newMessages( 058 null, 059 ValidationStrings.PATTERN_MISMATCH, 060 new Object[] 061 { "My Pattern", pattern }, 062 "default message"); 063 064 replay(); 065 066 try 067 { 068 new Pattern("pattern=\\d+").validate(field, messages, "fred"); 069 unreachable(); 070 } 071 catch (ValidatorException ex) 072 { 073 assertEquals("default message", ex.getMessage()); 074 assertEquals(ValidationConstraint.PATTERN_MISMATCH, ex.getConstraint()); 075 } 076 077 verify(); 078 } 079 080 public void test_Fail_Custom_Message() 081 { 082 String pattern = "\\d+"; 083 IFormComponent field = newField("My Pattern"); 084 ValidationMessages messages = newMessages( 085 "custom", 086 ValidationStrings.PATTERN_MISMATCH, 087 new Object[] 088 { "My Pattern", pattern }, 089 "custom message"); 090 091 replay(); 092 093 try 094 { 095 new Pattern("pattern=\\d+,message=custom").validate(field, messages, "fred"); 096 unreachable(); 097 } 098 catch (ValidatorException ex) 099 { 100 assertEquals("custom message", ex.getMessage()); 101 assertEquals(ValidationConstraint.PATTERN_MISMATCH, ex.getConstraint()); 102 } 103 104 verify(); 105 } 106 107 public void test_Render_Contribution() 108 { 109 String rawPattern = "\\d+"; 110 String pattern = new RegexpMatcher().getEscapedPatternString(rawPattern); 111 112 IMarkupWriter writer = newWriter(); 113 IRequestCycle cycle = newCycle(); 114 JSONObject json = new JSONObject(); 115 116 FormComponentContributorContext context = newMock(FormComponentContributorContext.class); 117 118 IFormComponent field = newField("Fred", "myfield"); 119 120 expect(context.getProfile()).andReturn(json); 121 122 trainFormatMessage(context, null, ValidationStrings.PATTERN_MISMATCH, 123 new Object[] { "Fred", rawPattern }, "default message"); 124 125 replay(); 126 127 new Pattern("pattern=\\d+").renderContribution(writer, cycle, context, field); 128 129 verify(); 130 131 assertEquals("{\"constraints\":{\"myfield\":[[tapestry.form.validation.isValidPattern,\"" 132 + pattern + "\"]]}," 133 + "\"myfield\":{\"constraints\":[\"default message\"]}}", 134 json.toString()); 135 } 136 137 public void test_Render_Contribution_CustomMessage() 138 { 139 String rawPattern = "\\d+"; 140 String pattern = new RegexpMatcher().getEscapedPatternString(rawPattern); 141 142 IMarkupWriter writer = newWriter(); 143 IRequestCycle cycle = newCycle(); 144 JSONObject json = new JSONObject(); 145 146 FormComponentContributorContext context = newMock(FormComponentContributorContext.class); 147 148 IFormComponent field = newField("Fred", "myfield"); 149 150 expect(context.getProfile()).andReturn(json); 151 152 trainFormatMessage( 153 context, 154 "custom", 155 ValidationStrings.PATTERN_MISMATCH, 156 new Object[] { "Fred", rawPattern }, 157 "custom\\message"); 158 159 replay(); 160 161 new Pattern("pattern=\\d+,message=custom").renderContribution(writer, cycle, context, field); 162 163 verify(); 164 165 assertEquals("{\"constraints\":{\"myfield\":[[tapestry.form.validation.isValidPattern,\"" 166 + pattern + "\"]]}," 167 +"\"myfield\":{\"constraints\":[\"custom\\\\message\"]}}", 168 json.toString()); 169 } 170 171 }