001/* 002 * Shredzone Commons 003 * 004 * Copyright (C) 2012 Richard "Shred" Körber 005 * http://commons.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Library General Public License as 009 * published by the Free Software Foundation, either version 3 of the 010 * License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU Library General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020package org.shredzone.commons.text.filter; 021 022import org.junit.Assert; 023import org.junit.Test; 024 025/** 026 * Unit test for {@link LinkToUrlFilterTest}. 027 * 028 * @author Richard "Shred" Körber 029 */ 030public class LinkToUrlFilterTest { 031 032 @Test 033 public void simpleTest() { 034 LinkToUrlFilter filter = new LinkToUrlFilter(); 035 036 StringBuilder sb = new StringBuilder(); 037 sb.append("This is a http://www.link.example/to/somewhere.gif. "); 038 sb.append("This is another https://www.link.example/to/ a directory. "); 039 sb.append("Download here ftp://ftp.foobar.example/file.png"); 040 041 CharSequence out = filter.apply(sb); 042 043 StringBuilder expect = new StringBuilder(); 044 expect.append("This is a <a href=\"http://www.link.example/to/somewhere.gif\">http://www.link.example/to/somewhere.gif</a>. "); 045 expect.append("This is another <a href=\"https://www.link.example/to/\">https://www.link.example/to/</a> a directory. "); 046 expect.append("Download here <a href=\"ftp://ftp.foobar.example/file.png\">ftp://ftp.foobar.example/file.png</a>"); 047 048 Assert.assertEquals(expect.toString(), out.toString()); 049 } 050 051 @Test 052 public void nofollowTest() { 053 LinkToUrlFilter filter = new LinkToUrlFilter(); 054 filter.setNoFollow(true); 055 056 StringBuilder sb = new StringBuilder(); 057 sb.append("This is a http://www.link.example/to/somewhere.gif "); 058 sb.append("This is another HTTPS://www.link.example/to/ a directory. "); 059 sb.append("Download here ftp://ftp.foobar.example/file.png"); 060 061 CharSequence out = filter.apply(sb); 062 063 StringBuilder expect = new StringBuilder(); 064 expect.append("This is a <a href=\"http://www.link.example/to/somewhere.gif\" rel=\"nofollow\">http://www.link.example/to/somewhere.gif</a> "); 065 expect.append("This is another <a href=\"HTTPS://www.link.example/to/\" rel=\"nofollow\">HTTPS://www.link.example/to/</a> a directory. "); 066 expect.append("Download here <a href=\"ftp://ftp.foobar.example/file.png\" rel=\"nofollow\">ftp://ftp.foobar.example/file.png</a>"); 067 068 Assert.assertEquals(expect.toString(), out.toString()); 069 } 070 071 @Test 072 public void noReferrerTest() { 073 LinkToUrlFilter filter = new LinkToUrlFilter(); 074 filter.setNoReferrer(true); 075 076 StringBuilder sb = new StringBuilder(); 077 sb.append("This is a http://www.link.example/to/somewhere.gif "); 078 sb.append("This is another HTTPS://www.link.example/to/ a directory. "); 079 sb.append("Download here ftp://ftp.foobar.example/file.png"); 080 081 CharSequence out = filter.apply(sb); 082 083 StringBuilder expect = new StringBuilder(); 084 expect.append("This is a <a href=\"http://www.link.example/to/somewhere.gif\" rel=\"noreferrer\">http://www.link.example/to/somewhere.gif</a> "); 085 expect.append("This is another <a href=\"HTTPS://www.link.example/to/\" rel=\"noreferrer\">HTTPS://www.link.example/to/</a> a directory. "); 086 expect.append("Download here <a href=\"ftp://ftp.foobar.example/file.png\" rel=\"noreferrer\">ftp://ftp.foobar.example/file.png</a>"); 087 088 Assert.assertEquals(expect.toString(), out.toString()); 089 } 090 091 @Test 092 public void targetTest() { 093 LinkToUrlFilter filter = new LinkToUrlFilter(); 094 filter.setTarget("_blank"); 095 096 StringBuilder sb = new StringBuilder(); 097 sb.append("This is a http://www.link.example/to/somewhere.gif "); 098 sb.append("This is another https://www.link.example/to/ a directory. "); 099 sb.append("Download here ftp://ftp.foobar.example/file.png"); 100 101 CharSequence out = filter.apply(sb); 102 103 StringBuilder expect = new StringBuilder(); 104 expect.append("This is a <a href=\"http://www.link.example/to/somewhere.gif\" target=\"_blank\" rel=\"noopener\">http://www.link.example/to/somewhere.gif</a> "); 105 expect.append("This is another <a href=\"https://www.link.example/to/\" target=\"_blank\" rel=\"noopener\">https://www.link.example/to/</a> a directory. "); 106 expect.append("Download here <a href=\"ftp://ftp.foobar.example/file.png\" target=\"_blank\" rel=\"noopener\">ftp://ftp.foobar.example/file.png</a>"); 107 108 Assert.assertEquals(expect.toString(), out.toString()); 109 } 110 111 @Test 112 public void targetNoBlankTest() { 113 LinkToUrlFilter filter = new LinkToUrlFilter(); 114 filter.setTarget("_top"); 115 116 StringBuilder sb = new StringBuilder(); 117 sb.append("This is a http://www.link.example/to/somewhere.gif "); 118 sb.append("This is another https://www.link.example/to/ a directory. "); 119 sb.append("Download here ftp://ftp.foobar.example/file.png"); 120 121 CharSequence out = filter.apply(sb); 122 123 StringBuilder expect = new StringBuilder(); 124 expect.append("This is a <a href=\"http://www.link.example/to/somewhere.gif\" target=\"_top\">http://www.link.example/to/somewhere.gif</a> "); 125 expect.append("This is another <a href=\"https://www.link.example/to/\" target=\"_top\">https://www.link.example/to/</a> a directory. "); 126 expect.append("Download here <a href=\"ftp://ftp.foobar.example/file.png\" target=\"_top\">ftp://ftp.foobar.example/file.png</a>"); 127 128 Assert.assertEquals(expect.toString(), out.toString()); 129 } 130 131 @Test 132 public void targetOpenerTest() { 133 LinkToUrlFilter filter = new LinkToUrlFilter(); 134 filter.setTarget("_blank"); 135 filter.setNoOpener(false); 136 137 StringBuilder sb = new StringBuilder(); 138 sb.append("This is a http://www.link.example/to/somewhere.gif "); 139 sb.append("This is another https://www.link.example/to/ a directory. "); 140 sb.append("Download here ftp://ftp.foobar.example/file.png"); 141 142 CharSequence out = filter.apply(sb); 143 144 StringBuilder expect = new StringBuilder(); 145 expect.append("This is a <a href=\"http://www.link.example/to/somewhere.gif\" target=\"_blank\">http://www.link.example/to/somewhere.gif</a> "); 146 expect.append("This is another <a href=\"https://www.link.example/to/\" target=\"_blank\">https://www.link.example/to/</a> a directory. "); 147 expect.append("Download here <a href=\"ftp://ftp.foobar.example/file.png\" target=\"_blank\">ftp://ftp.foobar.example/file.png</a>"); 148 149 Assert.assertEquals(expect.toString(), out.toString()); 150 } 151 152 @Test 153 public void combinationTest() { 154 LinkToUrlFilter filter = new LinkToUrlFilter(); 155 filter.setTarget("_blank"); 156 filter.setNoReferrer(true); 157 filter.setNoFollow(true); 158 159 StringBuilder sb = new StringBuilder(); 160 sb.append("This is a http://www.link.example/to/somewhere.gif "); 161 sb.append("This is another https://www.link.example/to/ a directory. "); 162 sb.append("Download here ftp://ftp.foobar.example/file.png"); 163 164 CharSequence out = filter.apply(sb); 165 166 StringBuilder expect = new StringBuilder(); 167 expect.append("This is a <a href=\"http://www.link.example/to/somewhere.gif\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">http://www.link.example/to/somewhere.gif</a> "); 168 expect.append("This is another <a href=\"https://www.link.example/to/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://www.link.example/to/</a> a directory. "); 169 expect.append("Download here <a href=\"ftp://ftp.foobar.example/file.png\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ftp://ftp.foobar.example/file.png</a>"); 170 171 Assert.assertEquals(expect.toString(), out.toString()); 172 } 173 174}