Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3829f2091b603fcc3baeba5491f2c5f23e18dec1 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*******************************************************************************
 * Copyright (c) 2007 Versant Corp.
 * 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:
 *     Markus Kuppe (mkuppe <at> versant <dot> com) - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.tests.discovery.identity;

import java.util.Arrays;

import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.discovery.identity.IServiceID;
import org.eclipse.ecf.discovery.identity.IServiceTypeID;
import org.eclipse.ecf.discovery.identity.ServiceIDFactory;
import org.eclipse.ecf.discovery.identity.ServiceTypeID;
import org.eclipse.ecf.tests.discovery.AbstractDiscoveryTest;

public abstract class ServiceIDTest extends AbstractDiscoveryTest {

	protected String namespace;

	public ServiceIDTest(String string) {
		namespace = string;
	}

	protected IServiceID createIDFromString(String serviceType) {
		try {
			return createIDFromStringWithEx(serviceType);
		} catch (final IDCreateException e) {
			fail(e.getMessage());
		} catch (final ClassCastException e) {
			fail(e.getMessage());
		}
		return null;
	}

	protected IServiceID createIDFromStringWithEx(String serviceType) throws IDCreateException {
		return ServiceIDFactory.getDefault().createServiceID(IDFactory.getDefault().getNamespaceByName(namespace), serviceType);
	}

	protected IServiceID createIDFromServiceTypeID(IServiceTypeID serviceType) {
		try {
			return createIDFromServiceTypeIDWithEx(serviceType);
		} catch (final IDCreateException e) {
			fail(e.getMessage());
		} catch (final ClassCastException e) {
			fail(e.getMessage());
		}
		return null;
	}

	protected IServiceID createIDFromServiceTypeIDWithEx(IServiceTypeID serviceType) throws IDCreateException {
		return ServiceIDFactory.getDefault().createServiceID(IDFactory.getDefault().getNamespaceByName(namespace), serviceType);
	}

	public void testServiceTypeIDWithNullString() {
		try {
			createIDFromStringWithEx(null);
		} catch (final IDCreateException ex) {
			return;
		}
		fail();
	}

	public void testServiceTypeIDWithEmptyString() {
		try {
			createIDFromStringWithEx("");
		} catch (final IDCreateException ex) {
			return;
		}
		fail();
	}

	/*
	 * use case: consumer instantiates a IServiceTypeID with the generic (ECF) String
	 */
	public void testServiceTypeIDWithECFGenericString() {
		final IServiceID sid = createIDFromString(SERVICE_TYPE);
		final IServiceTypeID stid = sid.getServiceTypeID();
		assertEquals(stid.getName(), SERVICE_TYPE);
		assertEquals(stid.getNamingAuthority(), NAMINGAUTHORITY);
		assertTrue(Arrays.equals(stid.getProtocols(), new String[] {PROTOCOL}));
		assertTrue(Arrays.equals(stid.getScopes(), new String[] {SCOPE}));
		assertTrue(Arrays.equals(stid.getServices(), SERVICES));
	}

	/*
	 * use case: consumer instantiates a IServiceTypeID with the generic (ECF) String
	 */
	public void testServiceTypeIDWithECFGenericString2() {
		final String serviceType = "_service._dns-srv._udp.ecf.eclipse.org._IANA";
		final IServiceID sid = createIDFromString(serviceType);
		final IServiceTypeID stid = sid.getServiceTypeID();
		assertEquals(stid.getName(), serviceType);
		assertEquals(stid.getNamingAuthority(), "IANA");
		assertTrue(Arrays.equals(stid.getProtocols(), new String[] {"udp"}));
		assertTrue(Arrays.equals(stid.getScopes(), new String[] {"ecf.eclipse.org"}));
		assertTrue(Arrays.equals(stid.getServices(), new String[] {"service", "dns-srv"}));
	}

	/*
	 * use case: consumer instantiates a IServiceTypeID with the generic (ECF) String
	 */
	public void testServiceTypeIDWithECFGenericString3() {
		final String serviceType = "_service._dns-srv._udp.ecf.eclipse.org._ECLIPSE";
		final IServiceID sid = createIDFromString(serviceType);
		final IServiceTypeID stid = sid.getServiceTypeID();
		assertEquals(stid.getName(), serviceType);
		assertEquals(stid.getNamingAuthority(), "ECLIPSE");
		assertTrue(Arrays.equals(stid.getProtocols(), new String[] {"udp"}));
		assertTrue(Arrays.equals(stid.getScopes(), new String[] {"ecf.eclipse.org"}));
		assertTrue(Arrays.equals(stid.getServices(), new String[] {"service", "dns-srv"}));
	}

	/*
	 * use case: conversion from one IServiceTypeID to another (provider A -> provider B)
	 */
	public void testServiceTypeIDWithServiceTypeID() throws IDCreateException {
		final Namespace ns = IDFactory.getDefault().getNamespaceByName(namespace);
		final IServiceTypeID aServiceTypeID = ServiceIDFactory.getDefault().createServiceID(ns, "_service._ecf._foo._bar._tcp.ecf.eclipse.org._IANA").getServiceTypeID();
		final IServiceID sid = createIDFromServiceTypeID(aServiceTypeID);
		final IServiceTypeID stid = sid.getServiceTypeID();

		// this is the only differences
		assertNotSame(aServiceTypeID.getInternal(), stid.getInternal());

		// members should be the same
		assertEquals(aServiceTypeID.getNamingAuthority(), stid.getNamingAuthority());
		assertTrue(Arrays.equals(aServiceTypeID.getServices(), stid.getServices()));
		assertTrue(Arrays.equals(aServiceTypeID.getScopes(), stid.getScopes()));
		assertTrue(Arrays.equals(aServiceTypeID.getProtocols(), stid.getProtocols()));

		// logically they should be the same
		assertTrue(aServiceTypeID.hashCode() == stid.hashCode());
		assertEquals(aServiceTypeID, stid);
		assertEquals(stid, aServiceTypeID);

		// should be possible to create a new instance from the string representation of the other
		createFromAnother(aServiceTypeID, stid);
		createFromAnother(stid, aServiceTypeID);
	}
	
	/*
	 * org.eclipse.ecf.discovery.identity.IServiceIDFactory.createServiceID(Namespace, String, String)
	 */
	public void testServiceIDFactory1() throws IDCreateException, SecurityException {
		String expected = "some Name";
		Namespace namespaceByName = IDFactory.getDefault().getNamespaceByName(namespace);
		IServiceID createServiceID = ServiceIDFactory.getDefault().createServiceID(namespaceByName, "_service._ecf._foo._bar._tcp.ecf.eclipse.org._IANA", expected);
		assertNotNull(createServiceID);
	}

	/*
	 * org.eclipse.ecf.discovery.identity.IServiceIDFactory.createServiceID(Namespace, String, String)
	 */
	public void testServiceIDFactory2() throws IDCreateException, SecurityException {
		Namespace namespaceByName = IDFactory.getDefault().getNamespaceByName(namespace);
		ServiceTypeID serviceTypeID = new ServiceTypeID(new TestNamespace(), "_service._ecf._foo._bar._tcp.ecf.eclipse.org._IANA");
		IServiceID createServiceID = ServiceIDFactory.getDefault().createServiceID(namespaceByName, serviceTypeID, "some Name");
		assertNotNull(createServiceID);
		
		// members should be the same
		IServiceTypeID aServiceTypeID = createServiceID.getServiceTypeID();
		assertEquals(aServiceTypeID.getNamingAuthority(), serviceTypeID.getNamingAuthority());
		assertTrue(Arrays.equals(aServiceTypeID.getServices(), serviceTypeID.getServices()));
		assertTrue(Arrays.equals(aServiceTypeID.getScopes(), serviceTypeID.getScopes()));
		assertTrue(Arrays.equals(aServiceTypeID.getProtocols(), serviceTypeID.getProtocols()));
		
		assertSame(namespaceByName, createServiceID.getNamespace());
	}

	/**
	 * Creates a new instance of IServiceTypeId with the Namespace of the second parameter and the instance of the first parameter 
	 * @param aServiceTypeID Used as a prototype
	 * @param stid Namespace to use
	 */
	private void createFromAnother(IServiceTypeID aServiceTypeID, IServiceTypeID stid) {
		final String name = aServiceTypeID.getName();
		final Namespace namespace2 = stid.getNamespace();
		IServiceID sid = null;
		try {
			sid = (IServiceID) namespace2.createInstance(new Object[] {name, null});
		} catch (final IDCreateException e) {
			fail("it should have been possible to create a new instance of " + stid.getClass().getName() + " from the string rep of " + aServiceTypeID.getClass().getName());
		}
		IServiceTypeID instance = sid.getServiceTypeID();
		assertTrue(instance.hashCode() == stid.hashCode());
		//TODO-mkuppe decide if equality should be handled by the namespace for IServiceTypeIDs?
		assertEquals(instance, stid);
		assertEquals(stid, instance);
		assertTrue(instance.hashCode() == aServiceTypeID.hashCode());
		assertEquals(instance, aServiceTypeID);
		assertEquals(aServiceTypeID, instance);
	}

	/*
	 * use case: creates the IServiceTypeID from the internal representation of the discovery provider
	 * to be implemented by subclasses
	 */
	public abstract void testCreateServiceTypeIDWithProviderSpecificString();
}

Back to the top