001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.text; 017 018import static org.hamcrest.core.Is.is; 019import static org.hamcrest.core.IsNull.notNullValue; 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNull; 022import static org.junit.Assert.assertThat; 023import org.junit.Before; 024import org.junit.Test; 025 026public class XmlValueEncoderTest { 027 028 private XmlValueEncoder encoder = new XmlValueEncoder(); 029 030 @Before 031 public void beforeEach() { 032 } 033 034 protected void checkEncoding( String input, 035 String expected ) { 036 String output = this.encoder.encode(input); 037 assertThat(output, is(notNullValue())); 038 assertEquals(expected, output); 039 assertThat(output.length(), is(expected.length())); 040 assertThat(output, is(expected)); 041 042 checkDecoding(output, input); 043 } 044 045 protected void checkForNoEncoding( String input ) { 046 String output = this.encoder.encode(input); 047 assertThat(output, is(notNullValue())); 048 assertEquals(input, output); 049 assertThat(output.length(), is(input.length())); 050 assertThat(output, is(input)); 051 052 checkForNoDecoding(input); 053 } 054 055 protected void checkForNoDecoding( String input ) { 056 String output = this.encoder.decode(input); 057 assertThat(output, is(notNullValue())); 058 assertEquals(input, output); 059 assertThat(output.length(), is(input.length())); 060 assertThat(output, is(input)); 061 } 062 063 protected void checkDecoding( String input, 064 String output ) { 065 String decoded = this.encoder.decode(input); 066 assertEquals(output, decoded); 067 assertThat(decoded.length(), is(output.length())); 068 assertThat(decoded, is(output)); 069 } 070 071 @Test 072 public void shouldEncodeStringWithNoSpecialChars() { 073 checkForNoEncoding("The quick brown fox jumped over the lazy dog.?+=!@#$%^*()_+-[]{}|\\"); 074 } 075 076 @Test 077 public void shouldEncodeStringWithSpecialChars() { 078 checkEncoding("<>&'\"", "<>&'""); 079 } 080 081 @Test 082 public void shouldHandleTrivialCase() { 083 assertNull(encoder.encode(null)); 084 assertNull(encoder.decode(null)); 085 checkEncoding("", ""); 086 087 } 088 089 @Test 090 public void shouldDecodeStringWithInvalidMappings() { 091 checkDecoding("&", "&"); 092 checkDecoding(""", """); 093 checkDecoding(">", ">"); 094 checkDecoding("<", "<"); 095 checkDecoding("amp;", "amp;"); 096 checkDecoding("quot;", "quot;"); 097 checkDecoding("gt;", "gt;"); 098 checkDecoding("lt;", "lt;"); 099 checkDecoding("&;", "&;"); 100 checkDecoding("&&", "&&"); 101 } 102}