1 package com.atlassian.core.test.util; 2 3 import junit.framework.TestCase; 4 import com.atlassian.core.util.collection.EasyList; 5 6 import java.sql.SQLException; 7 8 public class TestDuckTypeProxy extends TestCase 9 { 10 public interface MyInterface 11 { 12 String getString(); 13 Long getLong(); 14 Integer getInteger(); 15 String get(String string); 16 String get(Integer integer); 17 String getStuffed() throws SQLException; 18 } 19 20 public void testProxyReturns() throws Exception 21 { 22 Object obj = new Object() 23 { 24 public String getString() 25 { 26 return "who's you're daddy?"; 27 } 28 }; 29 MyInterface impl = (MyInterface) DuckTypeProxy.getProxy(MyInterface.class, EasyList.build(obj), DuckTypeProxy.THROW); 30 assertEquals("who's you're daddy?", impl.getString()); 31 } 32 33 public void testProxyThrows() throws Exception 34 { 35 MyInterface impl = (MyInterface) DuckTypeProxy.getProxy(MyInterface.class, EasyList.build(new Object()), DuckTypeProxy.THROW); 36 try 37 { 38 impl.getString(); 39 fail("should have thrown USOE"); 40 } 41 catch (UnsupportedOperationException yay) 42 { 43 } 44 } 45 46 public void testProxyThrowsTarget() throws Exception 47 { 48 Object obj = new Object() 49 { 50 public String getStuffed() throws SQLException 51 { 52 throw new SQLException("bad, bad, bad"); 53 } 54 }; 55 MyInterface impl = (MyInterface) DuckTypeProxy.getProxy(MyInterface.class, EasyList.build(obj)); 56 try 57 { 58 impl.getStuffed(); 59 fail("should have thrown USOE"); 60 } 61 catch (SQLException yay) 62 { 63 } 64 } 65 66 public void testProxyDelegatesToSecond() throws Exception 67 { 68 Object obj = new Object() 69 { 70 public String getString() 71 { 72 return "who's you're daddy?"; 73 } 74 }; 75 MyInterface impl = (MyInterface) DuckTypeProxy.getProxy(MyInterface.class, EasyList.build(new Object(), obj), DuckTypeProxy.THROW); 76 assertEquals("who's you're daddy?", impl.getString()); 77 } 78 79 public void testNotNullParameter() throws Exception 80 { 81 Object obj = new Object() 82 { 83 public String get(String string) 84 { 85 return "how about: " + string; 86 } 87 }; 88 MyInterface impl = (MyInterface) DuckTypeProxy.getProxy(MyInterface.class, EasyList.build(obj), DuckTypeProxy.THROW); 89 assertEquals("how about: me", impl.get("me")); 90 } 91 92 public void testNullParameter() throws Exception 93 { 94 Object obj = new Object() 95 { 96 public String get(String string) 97 { 98 return "how about: " + string; 99 } 100 }; 101 MyInterface impl = (MyInterface) DuckTypeProxy.getProxy(MyInterface.class, EasyList.build(obj), DuckTypeProxy.THROW); 102 assertEquals("how about: null", impl.get((String) null)); 103 } 104 }