001: package test.tck.factory;
002:
003: import java.text.ParseException;
004:
005: import javax.sip.address.*;
006:
007: import junit.framework.*;
008: import test.tck.*;
009:
010: public class AddressFactoryTest extends FactoryTestHarness {
011:
012: public AddressFactoryTest() {
013: super ("AddressFactoryTest");
014: }
015:
016: protected javax.sip.address.URI createTiURI(String uriString) {
017: javax.sip.address.URI uri = null;
018: try {
019: uri = tiAddressFactory.createURI(uriString);
020: assertTrue(uri != null);
021: } catch (Exception ex) {
022: throw new TiUnexpectedError(ex.getMessage());
023: }
024: return uri;
025: }
026:
027: protected javax.sip.address.URI createRiURI(String uriString) {
028: javax.sip.address.URI uri = null;
029: try {
030: uri = riAddressFactory.createURI(uriString);
031: } catch (Exception ex) {
032: throw new TckInternalError(ex.getMessage());
033: }
034: return uri;
035: }
036:
037: protected javax.sip.address.SipURI createTiSipURI(String name,
038: String address) {
039: SipURI sipUri = null;
040: try {
041: sipUri = tiAddressFactory.createSipURI(name, address);
042: assertNotNull(sipUri);
043: } catch (Exception ex) {
044: ex.printStackTrace();
045: throw new TiUnexpectedError(ex.getMessage());
046: }
047: return sipUri;
048: }
049:
050: protected javax.sip.address.SipURI createRiSipURI(String name,
051: String address) {
052: SipURI sipUri = null;
053: try {
054: sipUri = riAddressFactory.createSipURI(name, address);
055: } catch (Exception ex) {
056: throw new TiUnexpectedError(ex.getMessage());
057: }
058: return sipUri;
059: }
060:
061: protected javax.sip.address.TelURL createTiTelURL(String phoneNumber) {
062: TelURL telUrl = null;
063: try {
064: telUrl = tiAddressFactory.createTelURL(phoneNumber);
065: } catch (Exception ex) {
066: throw new TiUnexpectedError(ex.getMessage());
067: }
068: return telUrl;
069: }
070:
071: protected javax.sip.address.TelURL createRiTelURLFromTiTelURL(
072: TelURL tiTelURL) {
073: TelURL telUrl = null;
074: try {
075: // The API has a bug here - there is no way to retrieve the
076: // phone-context parameter. This will be fixed in the next release.
077: //int start = tiTelURL.toString().indexOf(':');
078: //String phoneNumber = tiTelURL.toString().substring(start+1).trim();
079: // String phoneNumber = tiTelURL.getPhoneContext(); // JvB: wrong
080: String phoneNumber = tiTelURL.getPhoneNumber();
081: telUrl = riAddressFactory.createTelURL(phoneNumber);
082: telUrl.setGlobal(tiTelURL.isGlobal());
083: // JvB: set to 'null' should remove it, fixed in impl
084: telUrl.setPhoneContext(tiTelURL.getPhoneContext());
085: } catch (Exception ex) {
086: ex.printStackTrace();
087: throw new TiUnexpectedError(ex.getMessage());
088: }
089: return telUrl;
090: }
091:
092: protected javax.sip.address.TelURL createRiTelURL(String phoneNumber) {
093: TelURL telUrl = null;
094: try {
095: telUrl = riAddressFactory.createTelURL(phoneNumber);
096: } catch (Exception ex) {
097: throw new TckInternalError(ex.getMessage());
098: }
099: return telUrl;
100: }
101:
102: protected javax.sip.address.Address createAddress(String address) {
103: javax.sip.address.Address addr = null;
104:
105: try {
106: addr = tiAddressFactory.createAddress(address);
107: assertTrue(addr != null);
108: } catch (Exception ex) {
109: throw new TiUnexpectedError(ex.getMessage());
110: }
111: return addr;
112: }
113:
114: protected javax.sip.address.Address createAddress(
115: javax.sip.address.URI uri) {
116: javax.sip.address.Address addr = null;
117: try {
118: System.out.println("Uri = " + uri);
119:
120: addr = tiAddressFactory.createAddress(uri);
121: System.out.println("addr = " + addr);
122: System.out.println("createAddress returns "
123: + tiAddressFactory.createAddress(uri.toString()));
124: assertTrue(addr.equals(tiAddressFactory.createAddress(uri
125: .toString())));
126: } catch (Exception ex) {
127: throw new TiUnexpectedError(ex.getMessage());
128: }
129: return addr;
130: }
131:
132: protected javax.sip.address.Address createRiAddress(
133: javax.sip.address.URI uri) {
134: javax.sip.address.Address addr = null;
135: try {
136: addr = riAddressFactory.createAddress(uri);
137: } catch (Exception ex) {
138: throw new TckInternalError(ex.getMessage());
139: }
140: return addr;
141: }
142:
143: protected javax.sip.address.Address createRiAddress(String address) {
144: javax.sip.address.Address addr = null;
145:
146: try {
147: addr = riAddressFactory.createAddress(address);
148: assertTrue(addr != null);
149: } catch (Exception ex) {
150: ex.printStackTrace();
151: throw new TckInternalError(ex.getMessage());
152: }
153: return addr;
154: }
155:
156: protected javax.sip.address.Address createRiAddressFromTiAddress(
157: Address tiAddress) throws TiUnexpectedError {
158: try {
159: return riAddressFactory.createAddress(tiAddress.toString());
160: } catch (Exception ex) {
161: throw new TiUnexpectedError(ex.getMessage());
162: }
163: }
164:
165: public void testStar() {
166: try {
167: Address star = tiAddressFactory.createAddress("*");
168: assertTrue(star.isWildcard());
169: assertTrue(star.getURI().isSipURI());
170: assertEquals("*", ((SipURI) star.getURI()).getUser());
171: } catch (ParseException pe) {
172: pe.printStackTrace();
173: fail(pe.getMessage());
174: }
175: }
176:
177: public void testAddressFactory() {
178: try {
179:
180: for (int i = 0; i < urls.length; i++) {
181: javax.sip.address.URI uri = this .createTiURI(urls[i]);
182: assertNotNull(uri);
183: javax.sip.address.Address tiAddress = this
184: .createAddress(uri);
185: assertNotNull(tiAddress);
186:
187: javax.sip.address.URI riUri = this .createRiURI(urls[i]);
188: javax.sip.address.Address riAddress = this
189: .createRiAddress(riUri);
190: tiAddress = this .createRiAddress(tiAddress.toString());
191:
192: assertEquals(riAddress, this
193: .createRiAddressFromTiAddress(tiAddress));
194:
195: }
196:
197: for (int i = 0; i < hosts.length; i++) {
198: javax.sip.address.SipURI tiSipURI;
199: javax.sip.address.SipURI riSipURI;
200: tiSipURI = this .createTiSipURI(null, hosts[i]);
201: assertTrue(tiSipURI != null);
202: assertTrue(tiSipURI.isSipURI());
203: assertTrue(!tiSipURI.isSecure());
204: assertTrue(((SipURI) tiAddressFactory.createURI("sip:"
205: + hosts[i])).equals(tiSipURI));
206: riSipURI = this .createRiSipURI(null, hosts[i]);
207: javax.sip.address.Address tiAddress = this
208: .createAddress(tiSipURI);
209: assertTrue(tiAddress != null);
210: javax.sip.address.Address riAddress = this
211: .createRiAddress(riSipURI);
212: assertTrue(riAddress.equals(this
213: .createRiAddressFromTiAddress(tiAddress)));
214:
215: tiSipURI = this .createTiSipURI("jaintck", hosts[i]);
216: assertTrue(tiSipURI != null);
217: assertTrue(tiSipURI.isSipURI());
218: assertTrue(!tiSipURI.isSecure());
219: assertTrue(((SipURI) tiAddressFactory
220: .createURI("sip:jaintck@" + hosts[i]))
221: .equals(tiSipURI));
222: tiAddress = this .createAddress(tiSipURI);
223: assertTrue(tiAddress != null);
224:
225: riSipURI = this .createRiSipURI("jaintck", hosts[i]);
226: riAddress = this .createRiAddress(riSipURI);
227: assertTrue(riAddress.equals(this
228: .createRiAddressFromTiAddress(tiAddress)));
229: }
230:
231: for (int i = 0; i < phoneNumbers.length; i++) {
232: javax.sip.address.TelURL tiTelUrl = this
233: .createTiTelURL(phoneNumbers[i]);
234: assertTrue(tiTelUrl != null);
235: javax.sip.address.TelURL riTelUrl = this
236: .createRiTelURL(phoneNumbers[i]);
237:
238: System.out.println("TI:" + tiTelUrl);
239: System.out.println("RI:" + riTelUrl);
240:
241: assertEquals(riTelUrl,
242: createRiTelURLFromTiTelURL(tiTelUrl));
243: }
244:
245: for (int i = 0; i < telUrls.length; i++) {
246: javax.sip.address.TelURL telUrl = (TelURL) this
247: .createTiURI(telUrls[i]);
248: assertTrue(telUrl != null);
249: int start = telUrl.toString().indexOf(':');
250: String phone = telUrl.toString().substring(start + 1)
251: .trim();
252: javax.sip.address.TelURL tiTelUrl = this
253: .createTiTelURL(phone);
254: tiTelUrl.setGlobal(telUrl.isGlobal());
255: assertTrue(telUrl.equals(tiTelUrl));
256: }
257: } catch (Exception ex) {
258: throw new TiUnexpectedError(ex.getMessage());
259: } finally {
260: logTestCompleted("testAddressFactory()");
261: }
262:
263: }
264:
265: public void testTelURL() throws Exception {
266: // JvB: This weird-looking tel: URL is actually valid, syntactically speaking
267: URI telURL = tiAddressFactory
268: .createURI("tel:0123456789ABCDEF#*-.();isub=/:-_.!~*'();phone-context=+123-.();-=[]/:");
269: assertTrue(telURL instanceof TelURL);
270: TelURL t = (TelURL) telURL;
271: assertEquals("0123456789ABCDEF#*-.()", t.getPhoneNumber());
272: assertEquals("+123-.()", t.getPhoneContext());
273: assertEquals("/:-_.!~*'()", t.getIsdnSubAddress());
274: assertEquals("[]/:", t.getParameter("-"));
275: }
276:
277: public void setUp() {
278:
279: }
280:
281: public static Test suite() {
282: return new TestSuite(AddressFactoryTest.class);
283: }
284:
285: public static void main(String[] args) {
286: junit.textui.TestRunner.run(AddressFactoryTest.class);
287: }
288:
289: }
|