PageRenderTime 37ms CodeModel.GetById 25ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/stat/src/test/java/com/google/sitebricks/stat/StatReadersTest.java

http://github.com/dhanji/sitebricks
Java | 143 lines | 89 code | 28 blank | 26 comment | 0 complexity | 7dbd23780ad12e09ade492accc142d00 MD5 | raw file
  1/**
  2 * Copyright (C) 2011 Google Inc.
  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
 18package com.google.sitebricks.stat;
 19
 20import com.google.common.collect.Lists;
 21import org.testng.annotations.BeforeMethod;
 22import org.testng.annotations.Test;
 23
 24import java.lang.reflect.Field;
 25import java.lang.reflect.Method;
 26import java.util.List;
 27
 28import static org.testng.Assert.assertEquals;
 29
 30
 31/**
 32 * This class includes tests for the logic within {@link StatReaders}.
 33 *
 34 * @author ffaber@gmail.com (Fred Faber)
 35 */
 36public class StatReadersTest {
 37
 38  static class TestClass {
 39    static Integer staticField;
 40
 41    static Integer getStaticFieldValue() {
 42      return staticField;
 43    }
 44
 45    Integer instanceField;
 46
 47    Integer getInstanceFieldValue() {
 48      return instanceField;
 49    }
 50  }
 51
 52  static final Integer STATIC_FIELD_STARTING_VALUE = 1;
 53  static final Integer INSTANCE_FIELD_STARTING_VALUE = 2;
 54
 55  Field staticField;
 56  Method staticMethod;
 57  Field instanceField;
 58  Method instanceMethod;
 59  TestClass testClass;
 60  
 61  @BeforeMethod
 62  public void setUp() throws Exception {
 63    testClass = new TestClass();
 64    TestClass.staticField = STATIC_FIELD_STARTING_VALUE;
 65    testClass.instanceField = INSTANCE_FIELD_STARTING_VALUE;
 66
 67    staticField = TestClass.class.getDeclaredField("staticField");
 68    staticMethod = TestClass.class.getDeclaredMethod("getStaticFieldValue");
 69    instanceField = TestClass.class.getDeclaredField("instanceField");
 70    instanceMethod = TestClass.class.getDeclaredMethod("getInstanceFieldValue");
 71  }
 72
 73  @Test
 74  public void testInstanceFieldReader() {
 75    StatReader statReader = StatReaders.forField(instanceField, testClass);
 76    assertEquals(testClass.instanceField, statReader.readStat());
 77
 78    // Mutate the field and confirm that its updated value is read.
 79    testClass.instanceField++;
 80    assertEquals(testClass.instanceField, statReader.readStat());
 81  }
 82  
 83  @Test public void testStaticFieldReader() { 
 84    StatReader statReader = StatReaders.forStaticField(staticField);
 85    assertEquals(TestClass.staticField, statReader.readStat());
 86
 87    // Mutate the field and confirm that its updated value is read.
 88    TestClass.staticField++;
 89    assertEquals(TestClass.staticField, statReader.readStat());
 90  }
 91  
 92  @Test public void testInstanceMethodReader() { 
 93    StatReader statReader = StatReaders.forMethod(instanceMethod, testClass);
 94    assertEquals(testClass.getInstanceFieldValue(), statReader.readStat());
 95    
 96    // Mutate the field and confirm that its updated value is read.
 97    testClass.instanceField++;
 98    assertEquals(testClass.getInstanceFieldValue(), statReader.readStat());
 99  }
100  
101  @Test public void testStaticMethodReader() { 
102    StatReader statReader = StatReaders.forStaticMethod(staticMethod);
103    assertEquals(TestClass.getStaticFieldValue(), statReader.readStat());
104
105    // Mutate the field and confirm that its updated value is read.
106    TestClass.staticField++;
107    assertEquals(TestClass.getStaticFieldValue(), statReader.readStat());
108  }
109
110  @Test public void testInstanceMemberReader_forField() {
111    StatReader memberReader = StatReaders.forMember(instanceField, testClass);
112    StatReader fieldReader = StatReaders.forField(instanceField, testClass);
113    assertEquals(memberReader, fieldReader);
114  }
115  
116  @Test public void testInstanceMemberReader_forMethod() {
117    StatReader memberReader = StatReaders.forMember(instanceMethod, testClass);
118    StatReader methodReader = StatReaders.forMethod(instanceMethod, testClass);
119    assertEquals(memberReader, methodReader);
120  }
121  
122  @Test public void testStaticMemberReader_forField() {
123    StatReader memberReader = StatReaders.forStaticMember(staticField);
124    StatReader fieldReader = StatReaders.forStaticField(staticField);
125    assertEquals(memberReader, fieldReader);
126  }
127  
128  @Test public void testStaticMemberReader_forMember() {
129    StatReader memberReader = StatReaders.forStaticMember(staticMethod);
130    StatReader methodReader = StatReaders.forStaticMethod(staticMethod);
131    assertEquals(memberReader, methodReader );
132  }
133
134  @Test public void testObjectReader() {
135    List<Integer> statList = Lists.newArrayList(1, 2);
136    StatReader statReader = StatReaders.forObject(statList);
137    assertEquals(statList, statReader.readStat());
138
139    // If we add a value, we expect it to be reflected in the stat that is read
140    statList.add(3);
141    assertEquals(statList, statReader.readStat());
142  }
143}