Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9bcdfa09627e34cd54b06ae1dfd75e77f6f3d55f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2008, 2012 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;

import junit.framework.TestCase;
import org.eclipse.jpt.common.utility.Association;
import org.eclipse.jpt.common.utility.internal.SimpleAssociation;

@SuppressWarnings("nls")
public class SimpleAssociationTests
	extends TestCase
{
	private SimpleAssociation<String, String> assoc;

	public SimpleAssociationTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.assoc = new SimpleAssociation<String, String>("foo", "bar");
	}

	@Override
	protected void tearDown() throws Exception {
		TestTools.clear(this);
		super.tearDown();
	}

	public void testGetKey() {
		assertEquals("foo", this.assoc.getKey());
	}

	public void testGetValue() {
		assertEquals("bar", this.assoc.getValue());
	}

	public void testSetValue() {
		assertEquals("bar", this.assoc.getValue());
		this.assoc.setValue("baz");
		assertEquals("baz", this.assoc.getValue());
	}

	public void testEquals() {
		assertFalse(this.assoc.equals("foo"));

		assertEquals(this.assoc, this.copy(this.assoc));

		SimpleAssociation<String, String> assoc2 = new SimpleAssociation<String, String>("foo", "baz");
		assertFalse(this.assoc.equals(assoc2));

		assoc2 = new SimpleAssociation<String, String>("fop", "bar");
		assertFalse(this.assoc.equals(assoc2));

		SimpleAssociation<String, String> assoc3 = new SimpleAssociation<String, String>(null, null);
		SimpleAssociation<String, String> assoc4 = new SimpleAssociation<String, String>(null, null);
		assertEquals(assoc3, assoc4);
	}

	public void testHashCode() {
		assertEquals(this.assoc.hashCode(), this.copy(this.assoc).hashCode());

		SimpleAssociation<String, String> assoc2 = new SimpleAssociation<String, String>(null, null);
		assertEquals(assoc2.hashCode(), this.copy(assoc2).hashCode());
	}

	public void testToString() {
		assertNotNull(this.assoc.toString());
	}

	public void testClone() {
		this.verifyClone(this.assoc, this.assoc.clone());
	}

	private void verifyClone(Association<String, String> expected, Association<String, String> actual) {
		assertEquals(expected, actual);
		assertNotSame(expected, actual);
		assertEquals(expected.getKey(), actual.getKey());
		assertSame(expected.getKey(), actual.getKey());
		assertEquals(expected.getValue(), actual.getValue());
		assertSame(expected.getValue(), actual.getValue());
	}

	public void testSerialization() throws Exception {
		@SuppressWarnings("cast")
		Association<String, String> assoc2 = (Association<String, String>) TestTools.serialize(this.assoc);

		assertEquals(this.assoc, assoc2);
		assertNotSame(this.assoc, assoc2);
		assertEquals(this.assoc.getKey(), assoc2.getKey());
		assertNotSame(this.assoc.getKey(), assoc2.getKey());
		assertEquals(this.assoc.getValue(), assoc2.getValue());
		assertNotSame(this.assoc.getValue(), assoc2.getValue());
	}

	private SimpleAssociation<String, String> copy(SimpleAssociation<String, String> sa) {
		return new SimpleAssociation<String, String>(sa.getKey(), sa.getValue());
	}

}

Back to the top