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.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  
25  /**
26   * Convert real-world examples in order to avoid regressions.
27   */
28  public class SpamProtectorTest {
29  
30      @Test
31      public void conversionRoundtripNull() {
32          assertNull(SpamProtector.toEncoded(null));
33          assertNull(SpamProtector.toPlain(null));
34  
35          assertEquals("", SpamProtector.toEncoded(""));
36          assertEquals("", SpamProtector.toPlain(""));
37      }
38  
39      @Test
40      public void conversionExample() {
41          assertEquals(CharacterConverter.SPACE.getReplacement(), SpamProtector.toEncoded(" "));
42          assertEquals(CharacterConverter.SPACE.getPlain(), SpamProtector.toPlain("&nbsp;"));
43      }
44  
45      @Test
46      public void singleCharactersAreConverted() {
47          assertEquals("&nbsp;", SpamProtector.toEncoded(String.valueOf(' ')));
48          assertEquals("&nbsp;", SpamProtector.toEncoded(" "));
49      }
50  
51      @Test
52      public void asciiOnlyIsEncodedCompletelyWithSpacesAndAtSign() {
53          String input = "mailto:w oo@suck.er";
54          assertEquals("&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;&nbsp;&#111;" +
55                  "&#111;&#64;&#115;&#117;&#99;&#107;&#46;&#101;&#114;", SpamProtector.toEncoded(input));
56      }
57  
58      @Test
59      public void nonAsciCharactersAreNotEncodedButTheRest() {
60          String input = "mailto:wフィリップoo@suck.нетer";
61          assertEquals("&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;フィリップ&#111;" +
62                  "&#111;&#64;&#115;&#117;&#99;&#107;&#46;нет&#101;&#114;", SpamProtector.toEncoded(input));
63      }
64  
65      @Test
66      public void asciiIsConvertedToPlainText() {
67          String input = "mailto:w oo@suck.er";
68          String encoded = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;&nbsp;&#111;&#111;&#64;&#115;&#117;&#99;&#107;&#46;&#101;&#114;";
69  
70          assertEquals(input, SpamProtector.toPlain(encoded));
71      }
72  
73      @Test
74      public void mixOfEncodedAndNonAsciiPlainCanBeDecoded() {
75          String plain = "mailto:wフィリップoo@suck.нетer";
76          String encoded = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#119;フィリップ&#111;&#111;&#64;&#115;&#117;&#99;&#107;&#46;нет&#101;&#114;";
77          assertEquals(plain, SpamProtector.toPlain(encoded));
78      }
79  
80      @Test
81      public void stupidInputDecode() {
82          assertEquals("", SpamProtector.toPlain(""));
83          assertNull(SpamProtector.toPlain(null));
84          assertEquals("²", SpamProtector.toPlain("²"));
85      }
86  
87      @Test
88      public void stupidInputEncode() {
89          assertEquals("", SpamProtector.toEncoded(""));
90          assertNull(SpamProtector.toEncoded(null));
91          assertEquals("²", SpamProtector.toEncoded("²"));
92      }
93  
94      @Test
95      public void issue24LowercasingIsDefaultBehaviour() {
96          assertEquals("A", SpamProtector.toPlain(CharacterConverter.CAPITAL_A.getPlain()));
97          assertEquals("a", SpamProtector.toPlain(CharacterConverter.A.getPlain()));
98      }
99  
100 }