package com.google.gdata.data; import static org.junit.Assert.*; import java.math.BigDecimal; import java.math.BigInteger; import org.junit.Before; import org.junit.Test; import org.xml.sax.Attributes; import org.mockito.Mockito; import com.google.gdata.util.ParseException; public class TestAttributeHelper { private Attributes emptyAttr = Mockito.mock(Attributes.class); private Attributes mockedAttr = Mockito.mock(Attributes.class); private Attributes mockedIntAttr = Mockito.mock(Attributes.class); private Attributes mockedFloatAttr = Mockito.mock(Attributes.class); private Attributes mockedBigIntAttr = Mockito.mock(Attributes.class); private Attributes mockedBigDecAttr = Mockito.mock(Attributes.class); private Attributes mockedDateTimeAttr = Mockito.mock(Attributes.class); public enum myEnum {value}; @Before public void setup() { Mockito.when(mockedAttr.getLength()).thenReturn(1); Mockito.when(mockedIntAttr.getLength()).thenReturn(1); Mockito.when(mockedFloatAttr.getLength()).thenReturn(1); Mockito.when(mockedBigIntAttr.getLength()).thenReturn(1); Mockito.when(mockedBigDecAttr.getLength()).thenReturn(1); Mockito.when(mockedDateTimeAttr.getLength()).thenReturn(1); Mockito.when(mockedAttr.getURI(0)).thenReturn(""); Mockito.when(mockedIntAttr.getURI(0)).thenReturn(""); Mockito.when(mockedFloatAttr.getURI(0)).thenReturn(""); Mockito.when(mockedBigIntAttr.getURI(0)).thenReturn(""); Mockito.when(mockedBigDecAttr.getURI(0)).thenReturn(""); Mockito.when(mockedDateTimeAttr.getURI(0)).thenReturn(""); Mockito.when(mockedAttr.getQName(0)).thenReturn("qname"); Mockito.when(mockedIntAttr.getQName(0)).thenReturn("qname"); Mockito.when(mockedFloatAttr.getQName(0)).thenReturn("qname"); Mockito.when(mockedBigIntAttr.getQName(0)).thenReturn("qname"); Mockito.when(mockedBigDecAttr.getQName(0)).thenReturn("qname"); Mockito.when(mockedDateTimeAttr.getQName(0)).thenReturn("qname"); Mockito.when(mockedAttr.getValue(0)).thenReturn("value"); Mockito.when(mockedIntAttr.getValue(0)).thenReturn("10"); Mockito.when(mockedFloatAttr.getValue(0)).thenReturn("10.33"); Mockito.when(mockedBigIntAttr.getValue(0)).thenReturn("10"); Mockito.when(mockedBigDecAttr.getValue(0)).thenReturn("10.33"); Mockito.when(mockedDateTimeAttr.getValue(0)).thenReturn("2002-09-24T06:00:00.0"); } @Test public void consumeTest() { AttributeHelper helper = new AttributeHelper(this.emptyAttr); boolean exception = false; try { helper.consume(null, true); } catch(ParseException e) { exception = true; } assertTrue(exception); exception = false; try { helper.consume("missing", true); } catch(ParseException e) { exception = true; } assertTrue(exception); exception = false; String result = null; try { result = helper.consume("missing", false); } catch(ParseException e) { exception = true; } assertFalse(exception); assertNull(result); helper = new AttributeHelper(this.mockedAttr); exception = false; result = null; try { result = helper.consume("qname", false); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals("value", result); } @Test public void consumeByteTest() { AttributeHelper helper = new AttributeHelper(this.emptyAttr); byte defaultval = 'A'; byte result = 'B'; boolean exception = false; try { result = helper.consumeByte("missing", false, defaultval); } catch(ParseException e) { exception = true; } assertFalse(exception); assertTrue(result == defaultval); } @Test public void consumeShortTest() { AttributeHelper helper = new AttributeHelper(this.mockedIntAttr); boolean exception = false; short result = -1; try { result = helper.consumeShort("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result, 10); } @Test public void consumeIntegerTest() { AttributeHelper helper = new AttributeHelper(this.mockedIntAttr); boolean exception = false; int result = -1; try { result = helper.consumeInteger("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result, 10); } @Test public void consumeLongTest() { AttributeHelper helper = new AttributeHelper(this.mockedIntAttr); boolean exception = false; long result = -1; try { result = helper.consumeLong("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result, 10); } @Test public void consumeBigIntegerTest() { AttributeHelper helper = new AttributeHelper(this.mockedBigIntAttr); boolean exception = false; BigInteger result = new BigInteger("1"); try { result = helper.consumeBigInteger("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result.intValue(), 10); } @Test public void consumeBigDecimalTest() { AttributeHelper helper = new AttributeHelper(this.mockedBigDecAttr); boolean exception = false; BigDecimal result = new BigDecimal("1"); try { result = helper.consumeBigDecimal("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result.doubleValue(), 10.33, 0.1); } @Test public void consumeDoubleTest() { AttributeHelper helper = new AttributeHelper(this.mockedFloatAttr); boolean exception = false; double result = -1; try { result = helper.consumeDouble("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result, 10.33, 0.1); } @Test public void consumeFloatTest() { AttributeHelper helper = new AttributeHelper(this.mockedFloatAttr); boolean exception = false; double result = -1; try { result = helper.consumeDouble("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result, 10.33, 0.1); } @Test public void consumeDateTimeTest() { AttributeHelper helper = new AttributeHelper(this.mockedDateTimeAttr); boolean exception = false; DateTime result = null; try { result = helper.consumeDateTime("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertNotNull(result); } @Test public void assertAllConsumedTest() { AttributeHelper helper = new AttributeHelper(this.mockedFloatAttr); boolean exception = false; double result = -1; try { result = helper.consumeDouble("qname", true); } catch(ParseException e) { exception = true; } assertFalse(exception); assertEquals(result, 10.33, 0.1); try { helper.assertAllConsumed(); } catch(ParseException e) { exception = true; } assertFalse(exception); } }