View Javadoc
1   /**
2    * SpamSchutz - simple way to protect your mail addresses from naïve spammers
3    * Copyright (C) 2011, Aiki IT
4    * <p/>
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * <p/>
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * <p/>
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package de.aikiit.spamprotector.converter;
19  
20  import org.junit.Test;
21  
22  import static org.assertj.core.api.Assertions.assertThat;
23  
24  /**
25   * Convert real-world examples in order to avoid regressions.
26   */
27  public class SpamProtectorTest {
28  
29      @Test
30      public void conversionRoundtripNull() {
31          assertThat(SpamProtector.toEncoded(null)).isNull();
32          assertThat(SpamProtector.toPlain(null)).isNull();
33  
34          assertThat(SpamProtector.toEncoded("")).isEmpty();
35          assertThat(SpamProtector.toPlain("")).isEmpty();
36      }
37  
38      @Test
39      public void conversionExample() {
40          assertThat(SpamProtector.toEncoded(" ")).isEqualTo(CharacterConverter.SPACE.getReplacement());
41          assertThat(SpamProtector.toPlain("&nbsp;")).isEqualTo(CharacterConverter.SPACE.getPlain());
42      }
43  
44      @Test
45      public void singleCharactersAreConverted() {
46          assertThat(SpamProtector.toEncoded(String.valueOf(' '))).isEqualTo("&nbsp;");
47          assertThat(SpamProtector.toEncoded(" ")).isEqualTo("&nbsp;");
48      }
49  
50      @Test
51      public void asciiOnlyIsEncodedCompletelyWithSpacesAndAtSign() {
52          String input = "mailto:w oo@suck.er";
53          assertThat(SpamProtector.toEncoded(input)).isEqualTo("&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;&nbsp;&#111;" +
54                  "&#111;&#64;&#115;&#117;&#99;&#107;&#46;&#101;&#114;");
55      }
56  
57      @Test
58      public void nonAsciCharactersAreNotEncodedButTheRest() {
59          String input = "mailto:wフィリップoo@suck.нетer";
60          assertThat(SpamProtector.toEncoded(input)).isEqualTo("&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;フィリップ&#111;" +
61                  "&#111;&#64;&#115;&#117;&#99;&#107;&#46;нет&#101;&#114;");
62      }
63  
64      @Test
65      public void asciiIsConvertedToPlainText() {
66          String input = "mailto:w oo@suck.er";
67          String encoded = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;&nbsp;&#111;&#111;&#64;&#115;&#117;&#99;&#107;&#46;&#101;&#114;";
68  
69          assertThat(SpamProtector.toPlain(encoded)).isEqualTo(input);
70      }
71  
72      @Test
73      public void mixOfEncodedAndNonAsciiPlainCanBeDecoded() {
74          String plain = "mailto:wフィリップoo@suck.нетer";
75          String encoded = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;フィリップ&#111;&#111;&#64;&#115;&#117;&#99;&#107;&#46;нет&#101;&#114;";
76          assertThat(SpamProtector.toPlain(encoded)).isEqualTo(plain);
77      }
78  
79      @Test
80      public void stupidInputDecode() {
81          assertThat(SpamProtector.toPlain("")).isEmpty();
82          assertThat(SpamProtector.toPlain(null)).isNull();
83          assertThat(SpamProtector.toPlain("²")).isEqualTo("²");
84      }
85  
86      @Test
87      public void stupidInputEncode() {
88          assertThat(SpamProtector.toEncoded("")).isEmpty();
89          assertThat(SpamProtector.toEncoded(null)).isNull();
90          assertThat(SpamProtector.toEncoded("²")).isEqualTo("²");
91      }
92  
93      @Test
94      public void issue24LowercasingIsDefaultBehaviour() {
95          assertThat(SpamProtector.toPlain(CharacterConverter.CAPITAL_A.getPlain())).isEqualTo("A");
96          assertThat(SpamProtector.toPlain(CharacterConverter.A.getPlain())).isEqualTo("a");
97      }
98  
99  }