Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 57ec12a0ec1cbe85983938f525edfa80687ac80b (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/****************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *    Composent, Inc. - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package org.eclipse.ecf.tests.core.identity;

import java.io.ByteArrayOutputStream;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.net.URI;
import java.util.UUID;

import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;

public class UuIDTest extends IDAbstractTestCase {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.tests.IDTestCase#createID()
	 */

	public ID createID() {
		return IDFactory.getDefault().createUuID();
	}
	
	public void testCreateRandom() throws Exception {
		final ID newID = createID();
		assertNotNull(newID);
	}

	public void testCreateUUID() throws Exception {
		final ID newID = IDFactory.getDefault().createUuID(UUID.randomUUID());
		assertNotNull(newID);
	}

	public void testCreateString() throws Exception {
		final ID newID = IDFactory.getDefault().createUuID(UUID.randomUUID().toString());
		assertNotNull(newID);
	}

	public void testCreateURI() throws Exception {
		final ID newID = IDFactory.getDefault().createUuID(new URI("uuid:"+UUID.randomUUID()));
		assertNotNull(newID);
	}

	public void testGetName() throws Exception {
		UUID uuid = UUID.randomUUID();
		final ID id1 = IDFactory.getDefault().createUuID(uuid);
		final ID id2 = IDFactory.getDefault().createUuID(uuid);
		assertTrue(id1.getName().equals(id2.getName()));
	}

	public void testToExternalForm() throws Exception {
		final ID id = createID();
		assertNotNull(id.toExternalForm());
	}

	public void testToString() throws Exception {
		final ID id = IDFactory.getDefault().createUuID();
		assertNotNull(id.toString());
	}

	public void testIsEqual() throws Exception {
		UUID uuid = UUID.randomUUID();
		final ID id1 = IDFactory.getDefault().createUuID(uuid);
		final ID id2 = IDFactory.getDefault().createUuID(uuid);
		assertTrue(id1.equals(id2));
	}

	public void testHashCode() throws Exception {
		UUID uuid = UUID.randomUUID();
		final ID id1 = IDFactory.getDefault().createUuID(uuid);
		final ID id2 = IDFactory.getDefault().createUuID(uuid);
		assertTrue(id1.hashCode() == id2.hashCode());
	}

	public void testCompareToEqual() throws Exception {
		UUID uuid = UUID.randomUUID();
		final ID id1 = IDFactory.getDefault().createUuID(uuid);
		final ID id2 = IDFactory.getDefault().createUuID(uuid);
		assertTrue(id1.compareTo(id2) == 0);
		assertTrue(id2.compareTo(id1) == 0);
	}

	public void testCompareToNotEqual() throws Exception {
		final ID id1 = createID();
		final ID id2 = createID();
		assertTrue(id1.compareTo(id2) != 0);
	}

	public void testGetNamespace() throws Exception {
		final ID id = createID();
		final Namespace ns = id.getNamespace();
		assertNotNull(ns);
	}

	public void testEqualNamespaces() throws Exception {
		final ID id1 = createID();
		final ID id2 = createID();
		final Namespace ns1 = id1.getNamespace();
		final Namespace ns2 = id2.getNamespace();
		assertTrue(ns1.equals(ns2));
		assertTrue(ns2.equals(ns2));
	}

	public void testSerializable() throws Exception {
		final ByteArrayOutputStream buf = new ByteArrayOutputStream();
		final ObjectOutputStream out = new ObjectOutputStream(buf);
		try {
			out.writeObject(createID());
		} catch (final NotSerializableException ex) {
			fail(ex.getLocalizedMessage());
		} finally {
			out.close();
		}
	}

	public void testCreateFromExternalForm() throws Exception {
		final ID id1 = createID();
		final String externalForm = id1.toExternalForm();
		final ID id2 = IDFactory.getDefault().createID(id1.getNamespace(),
				externalForm);
		assertTrue(id1.equals(id2));
	}
}

Back to the top