1 /* 2 * Copyright 2012 AndroidPlot.com 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.androidplot.util; 18 19 import android.content.Context; 20 import android.util.Log; 21 import com.androidplot.mock.MockContext; 22 import mockit.Mockit; 23 import mockit.UsingMocksAndStubs; 24 import org.junit.Test; 25 26 import java.lang.reflect.Method; 27 28 import static junit.framework.Assert.assertEquals; 29 import static org.junit.Assert.fail; 30 31 @UsingMocksAndStubs({Log.class}) 32 public class ConfiguratorTest { 33 34 class A { 35 int d = 0; 36 getD()37 public int getD() { 38 return d; 39 } 40 setD(int d)41 public void setD(int d) { 42 this.d = d; 43 } 44 } 45 46 class B { 47 A a = new A(); 48 getA()49 public A getA() { 50 return a; 51 } 52 setA(A a)53 public void setA(A a) { 54 this.a = a; 55 } 56 } 57 58 class C { 59 B b = new B(); 60 getB()61 public B getB() { 62 return b; 63 } 64 setB(B a)65 public void setB(B a) { 66 this.b = b; 67 } 68 } 69 70 @org.junit.Before setUp()71 public void setUp() throws Exception { 72 73 } 74 75 @org.junit.After tearDown()76 public void tearDown() throws Exception { 77 78 } 79 80 81 @Test testGetFieldAt()82 public void testGetFieldAt() throws Exception { 83 Context context = Mockit.setUpMock(new MockContext()); 84 C c = new C(); 85 assertEquals(c, Configurator.getObjectContaining(c, "b")); 86 assertEquals(c.getB(), Configurator.getObjectContaining(c, "b.a")); 87 assertEquals(c.getB().getA(), Configurator.getObjectContaining(c, "b.a.d")); 88 } 89 90 @Test testGetSetter()91 public void testGetSetter() throws Exception { 92 Context context = Mockit.setUpMock(new MockContext()); 93 C c = new C(); 94 95 Method m = Configurator.getSetter(c.getClass(), "b"); 96 assertEquals(1, m.getParameterTypes().length); 97 assertEquals(B.class, m.getParameterTypes()[0]); 98 } 99 } 100