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.Before; 024import org.junit.Test; 025 026/** 027 * Unit test for {@link SmilyFilterTest}. 028 * 029 * @author Richard "Shred" Körber 030 */ 031public class SmilyFilterTest { 032 033 private SmilyFilter filter; 034 035 @Before 036 public void setup() { 037 filter = new SmilyFilter(); 038 filter.setBaseUrl("foo/smileys"); 039 filter.addSmily(":-)", "happy.png"); 040 filter.addSmily(":-(", "sad.png"); 041 filter.addSmily(":-P", "tongue.png"); 042 filter.addSmily(":-))", "veryhappy.png"); 043 } 044 045 @Test 046 public void filterTest() { 047 StringBuilder sb = new StringBuilder(); 048 sb.append("Hi! :-P\n"); 049 sb.append("I am happy :-)), yeah!\n"); 050 sb.append("(Even more happy :-)))"); 051 052 CharSequence out = filter.apply(sb); 053 054 StringBuilder expect = new StringBuilder(); 055 expect.append("Hi! <img src=\"foo/smileys/tongue.png\" alt=\":-P\" />\n"); 056 expect.append("I am happy <img src=\"foo/smileys/veryhappy.png\" alt=\":-))\" />, yeah!\n"); 057 expect.append("(Even more happy <img src=\"foo/smileys/veryhappy.png\" alt=\":-))\" />)"); 058 059 Assert.assertEquals(expect.toString(), out.toString()); 060 } 061 062 @Test 063 public void noSmilyTest() { 064 StringBuilder sb = new StringBuilder(); 065 sb.append("There is no smile in here."); 066 067 CharSequence out = filter.apply(sb); 068 069 StringBuilder expect = new StringBuilder(); 070 expect.append("There is no smile in here."); 071 072 Assert.assertEquals(expect.toString(), out.toString()); 073 074 // As the filter will not change the StringBuilder at all, we expect the 075 // very same instance back. 076 Assert.assertSame(sb, out); 077 } 078 079}