001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 116920
011: *******************************************************************************/package org.eclipse.core.tests.internal.databinding.conversion;
012:
013: import junit.framework.TestCase;
014:
015: import org.eclipse.core.databinding.BindingException;
016: import org.eclipse.core.internal.databinding.conversion.IdentityConverter;
017:
018: /**
019: * @since 3.2
020: *
021: */
022: public class IdentityConverterTest extends TestCase {
023:
024: private IdentityConverter c;
025:
026: /* (non-Javadoc)
027: * @see junit.framework.TestCase#setUp()
028: */
029: protected void setUp() throws Exception {
030: c = new IdentityConverter(Integer.TYPE, Integer.TYPE);
031: }
032:
033: public void testIsPrimitiveTypeMatchedWithBoxed() throws Exception {
034: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Integer.class,
035: Integer.TYPE));
036: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Integer.TYPE,
037: Integer.class));
038:
039: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Byte.class,
040: Byte.TYPE));
041: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Byte.TYPE,
042: Byte.class));
043:
044: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Short.class,
045: Short.TYPE));
046: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Short.TYPE,
047: Short.class));
048:
049: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Long.class,
050: Long.TYPE));
051: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Long.TYPE,
052: Long.class));
053:
054: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Float.class,
055: Float.TYPE));
056: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Float.TYPE,
057: Float.class));
058:
059: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Double.class,
060: Double.TYPE));
061: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Double.TYPE,
062: Double.class));
063:
064: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Boolean.class,
065: Boolean.TYPE));
066: assertTrue(c.isPrimitiveTypeMatchedWithBoxed(Boolean.TYPE,
067: Boolean.class));
068:
069: assertFalse(c.isPrimitiveTypeMatchedWithBoxed(Boolean.class,
070: Integer.TYPE));
071: }
072:
073: public void testConvert_NullToPrimitive() {
074: IdentityConverter p2b = new IdentityConverter(Float.TYPE,
075: Float.TYPE);
076: try {
077: p2b.convert(null);
078: fail("Should have thrown an exception");
079: } catch (BindingException b) {
080: // success
081: }
082: }
083:
084: public void testConvert_PrimitiveToBoxed() throws Exception {
085: IdentityConverter p2b = new IdentityConverter(Float.TYPE,
086: Float.class);
087: assertEquals("4.2", new Float(4.2), p2b.convert(new Float(4.2)));
088: }
089:
090: public void testConvert_BoxedToPrimitive() throws Exception {
091: IdentityConverter p2b = new IdentityConverter(Float.class,
092: Float.TYPE);
093: assertEquals("4.2", new Float(4.2), p2b.convert(new Float(4.2)));
094: }
095:
096: public void testConvert_PrimitiveToPrimitive() throws Exception {
097: IdentityConverter p2b = new IdentityConverter(Float.TYPE,
098: Float.TYPE);
099: assertEquals("4.2", new Float(4.2), p2b.convert(new Float(4.2)));
100: }
101:
102: public void testConvert_BoxedToBoxed() throws Exception {
103: IdentityConverter p2b = new IdentityConverter(Float.class,
104: Float.class);
105: assertEquals("4.2", new Float(4.2), p2b.convert(new Float(4.2)));
106: }
107:
108: public static class Person {
109: public String foo = "blah";
110: }
111:
112: public static class Animal {
113: public String name = "fido";
114: }
115:
116: public void test_Convert_ValidAssignment() throws Exception {
117: IdentityConverter pc = new IdentityConverter(Object.class,
118: Person.class);
119: Person orig = new Person();
120: Object person = pc.convert(orig);
121: assertTrue("Person class", person.getClass().equals(
122: Person.class));
123: assertTrue("Need correct Person", person.equals(orig));
124: }
125:
126: public void test_Convert_ValidAssignment2() throws Exception {
127: IdentityConverter pc = new IdentityConverter(Person.class,
128: Object.class);
129: Person orig = new Person();
130: Object person = pc.convert(orig);
131: assertTrue("Person class", person.getClass().equals(
132: Person.class));
133: assertTrue("Need correct Person", person.equals(orig));
134: }
135:
136: public void testConvert_InvalidAssignment() throws Exception {
137: IdentityConverter pc = new IdentityConverter(Object.class,
138: Person.class);
139: try {
140: pc.convert(new Animal());
141: fail("Should have gotten an exception");
142: } catch (Exception e) {
143: // success
144: }
145: }
146: }
|