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.assertThat;
021import org.junit.Before;
022import org.junit.Test;
023
024/**
025 * @author Randall Hauch
026 */
027public class Jsr283EncoderTest {
028
029    private Jsr283Encoder encoder = new Jsr283Encoder();
030
031    @Before
032    public void beforeEach() {
033
034    }
035
036    protected void checkSingleCharacterEncoding( char input, char expected ) {
037        String inputString = new String(new char[] {input});
038        String output = this.encoder.encode(inputString);
039        assertThat(output, is(notNullValue()));
040        assertThat(output.length(), is(1));
041        assertThat(output.charAt(0), is(expected));
042
043        String decoded = this.encoder.decode(output);
044        assertThat(decoded, is(notNullValue()));
045        assertThat(decoded.length(), is(1));
046        assertThat(decoded.charAt(0), is(input));
047    }
048
049    protected void checkForNoEncoding( String input ) {
050        String output = this.encoder.encode(input);
051        assertThat(output, is(notNullValue()));
052        assertThat(output.length(), is(input.length()));
053        assertThat(output, is(input));
054
055        String decoded = this.encoder.decode(output);
056        assertThat(decoded.length(), is(input.length()));
057        assertThat(decoded, is(input));
058    }
059
060    @Test
061    public void shouldEncodeAsterisk() {
062        checkSingleCharacterEncoding('*', '\uF02A');
063    }
064
065    @Test
066    public void shouldEncodeForwardSlash() {
067        checkSingleCharacterEncoding('/', '\uF02F');
068    }
069
070    @Test
071    public void shouldEncodeColon() {
072        checkSingleCharacterEncoding(':', '\uF03A');
073    }
074
075    @Test
076    public void shouldEncodeOpenBracket() {
077        checkSingleCharacterEncoding('[', '\uF05B');
078    }
079
080    @Test
081    public void shouldEncodeCloseBracket() {
082        checkSingleCharacterEncoding(']', '\uF05D');
083    }
084
085    @Test
086    public void shouldEncodePipe() {
087        checkSingleCharacterEncoding('|', '\uF07C');
088    }
089
090    @Test
091    public void shouldNotEncodeAlphabeticCharacters() {
092        checkForNoEncoding("abcdefghijklmnopqrstuvwxyz");
093        checkForNoEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
094    }
095
096    @Test
097    public void shouldNotEncodeNumericCharacters() {
098        checkForNoEncoding("0123456789");
099    }
100
101    @Test
102    public void shouldNotEncodePunctuationCharacters() {
103        checkForNoEncoding("~`!@#$%^&()-_+={}\\;\"'<,>.?");
104    }
105}