001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jetspeed.capabilities;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Set;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
029:
030: /**
031: * Test Capability Service
032: *
033: * @author <a href="roger.ruttimann@earthlink.net">Roger Ruttimann</a>
034: * @version $Id: TestCapability.java 576719 2007-09-18 06:31:02Z woonsan $
035: */
036: public class TestCapability extends DatasourceEnabledSpringTestCase {
037: private Capabilities capabilities = null;
038:
039: /**
040: * Start the tests.
041: *
042: * @param args
043: * the arguments. Not used
044: */
045: public static void main(String args[]) {
046: junit.awtui.TestRunner.main(new String[] { TestCapability.class
047: .getName() });
048: }
049:
050: protected void setUp() throws Exception {
051: super .setUp();
052: capabilities = (Capabilities) ctx.getBean("capabilities");
053: }
054:
055: public static Test suite() {
056: // All methods starting with "test" will be executed in the test suite.
057: return new TestSuite(TestCapability.class);
058: }
059:
060: /**
061: * Tests categories
062: *
063: * @throws Exception
064: */
065: public void testCapability() throws Exception {
066: assertNotNull("capabilities component is null", capabilities);
067: int lastOrder = 0;
068: Iterator caps = capabilities.getClients();
069: while (caps.hasNext()) {
070: Client client = (Client) caps.next();
071: int evalOrder = client.getEvalOrder();
072: if (lastOrder > evalOrder) {
073: assertTrue("Client result set is not ordered!", false);
074: }
075: lastOrder = evalOrder;
076: }
077:
078: // Find specific client -- testing pattern matching
079: String userAgent;
080: System.out.println("Testing all supported Clients...");
081: userAgent = "Opera/7.0";
082: System.out.println("Find pattern: " + userAgent);
083: CapabilityMap cm = capabilities.getCapabilityMap(userAgent);
084: assertNotNull("getCapabilityMap is null", cm);
085: assertTrue("Opera", cm.getClient().getName().equals("opera7"));
086: capabilityMapReport(cm);
087:
088: userAgent = "Mozilla/4.0";
089: System.out.println("Find pattern: " + userAgent);
090: cm = capabilities.getCapabilityMap(userAgent);
091: assertNotNull("getCapabilityMap is null", cm);
092: assertTrue("Netscape/Mozilla4", cm.getClient().getName()
093: .equals("ns4"));
094: capabilityMapReport(cm);
095:
096: userAgent = "MSIE 5.0";
097: System.out.println("Find pattern: " + userAgent);
098: cm = capabilities.getCapabilityMap(userAgent);
099: assertNotNull("getCapabilityMap is null", cm);
100: assertTrue("MSIE 5", cm.getClient().getName().equals("ie5"));
101: capabilityMapReport(cm);
102:
103: userAgent = "Mozilla/5.0";
104: System.out.println("Find pattern: " + userAgent);
105: cm = capabilities.getCapabilityMap(userAgent);
106: assertNotNull("getCapabilityMap is null", cm);
107: assertTrue("Mozilla 5.0", cm.getClient().getName().equals(
108: "mozilla"));
109: capabilityMapReport(cm);
110:
111: userAgent = "Lynx";
112: System.out.println("Find pattern: " + userAgent);
113: cm = capabilities.getCapabilityMap(userAgent);
114: assertNotNull("getCapabilityMap is null", cm);
115: capabilityMapReport(cm);
116:
117: userAgent = "Nokia";
118: System.out.println("Find pattern: " + userAgent);
119: cm = capabilities.getCapabilityMap(userAgent);
120: assertNotNull("getCapabilityMap is null", cm);
121: capabilityMapReport(cm);
122:
123: userAgent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/125.5.6 (KHTML, like Gecko) Safari/125.12";
124: System.out.println("Find pattern: " + userAgent);
125: cm = capabilities.getCapabilityMap(userAgent);
126: assertNotNull("getCapabilityMap is null", cm);
127: assertTrue("found Safari", cm.getClient().getName().equals(
128: "safari"));
129: capabilityMapReport(cm);
130:
131: userAgent = "Mozilla/4.0 (compatible; MSIE 5.23; Mac_PowerPC)";
132: System.out.println("Find pattern: " + userAgent);
133: cm = capabilities.getCapabilityMap(userAgent);
134: assertNotNull("getCapabilityMap is null", cm);
135: assertTrue("IE for Mac " + cm.getClient().getName(), cm
136: .getClient().getName().equals("ie5mac"));
137: capabilityMapReport(cm);
138:
139: userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
140: System.out.println("Find pattern: " + userAgent);
141: cm = capabilities.getCapabilityMap(userAgent);
142: assertNotNull("getCapabilityMap is null", cm);
143: assertTrue("IE 6 Windows", cm.getClient().getName().equals(
144: "ie6"));
145: capabilityMapReport(cm);
146:
147: userAgent = "SonyEricssonK800i/R1CB Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1";
148: System.out.println("Find pattern: " + userAgent);
149: cm = capabilities.getCapabilityMap(userAgent);
150: assertNotNull("getCapabilityMap is null", cm);
151: assertTrue("Ericsson", cm.getClient().getName().equals(
152: "sonyericsson"));
153: capabilityMapReport(cm);
154:
155: }
156:
157: private void capabilityMapReport(CapabilityMap cm) {
158: MediaType mediaType = cm.getPreferredMediaType();
159: assertNotNull("Preferred MediaType is null", mediaType);
160:
161: MimeType mimeTypeObj = cm.getPreferredType();
162: assertNotNull("Preferred MimeType is null", mimeTypeObj);
163: String mimeType = mimeTypeObj.getName();
164:
165: String encoding = mediaType.getCharacterSet();
166:
167: System.out.println("Preferred MediaType = "
168: + mediaType.getName());
169: System.out.println("Preferred Mimetype = " + mimeType);
170: System.out.println("Encoding = " + encoding);
171: System.out.println("Supported MediaTypes");
172: Iterator cmIterator = cm.listMediaTypes();
173:
174: while (cmIterator.hasNext()) {
175: System.out.println(((MediaType) cmIterator.next())
176: .getName());
177: }
178:
179: System.out.println("Supported MimeTypes");
180: Iterator mtIterator = cm.getMimeTypes();
181:
182: while (mtIterator.hasNext()) {
183: System.out
184: .println(((MimeType) mtIterator.next()).getName());
185: }
186: }
187:
188: private HashMap getCapabilities(int howMany) {
189: Capability capability = null;
190: Iterator _it = capabilities.getCapabilities();
191: HashMap _hash = new HashMap();
192: int count = 0;
193: while (_it.hasNext()) {
194: capability = (Capability) _it.next();
195: _hash.put(capability.getName(), capability);
196: count++;
197: if (howMany > 0)
198: if (count >= howMany)
199: return _hash;
200: }
201: return _hash;
202: }
203:
204: private HashMap getMimeTypes(int howMany) {
205: MimeType mimeType = null;
206: Iterator _it = capabilities.getMimeTypes();
207: HashMap _hash = new HashMap();
208: int count = 0;
209: while (_it.hasNext()) {
210: mimeType = (MimeType) _it.next();
211: _hash.put(mimeType.getName(), mimeType);
212: count++;
213: if (howMany > 0)
214: if (count >= howMany)
215: return _hash;
216: }
217: return _hash;
218: }
219:
220: public void testNewMimeType() throws Exception {
221: MimeType mimeType = null;
222: Iterator _it = null;
223: HashMap _hash = getMimeTypes(0);
224: int count = _hash.size();
225: assertTrue("MimeTypes do not exist", (count > 0));
226:
227: _it = _hash.keySet().iterator();
228:
229: int pos = count / 2;
230:
231: for (int i = 0; i < pos; i++)
232: _it.next();
233:
234: String existingKey = (String) _it.next();
235: MimeType existingObject = (MimeType) _hash.get(existingKey);
236: assertNotNull(
237: "Couldn't identify existing mime object to run test",
238: existingObject);
239:
240: // "create" existing one
241: mimeType = capabilities.createMimeType(existingKey);
242: assertNotNull("creating 'existing' mimetype returns null",
243: mimeType);
244: assertTrue(
245: "creating 'existing' mimetype didn't return existing object",
246: (mimeType.equals(existingObject)));
247:
248: // create a new one:
249: mimeType = capabilities.createMimeType("TEST MIME TYPE");
250: assertNotNull("creating new mimetype returns null", mimeType);
251:
252: // ensure it doesn't exist in the capabilities
253: Set existing = _hash.entrySet();
254: assertTrue("creating new mimetype already in existing list",
255: (!(existing.contains(mimeType))));
256:
257: existingObject = capabilities.getMimeType("TEST MIME TYPE");
258: assertNull(
259: "creating new mimetype already in existing capabilities",
260: existingObject);
261:
262: capabilities.storeMimeType(mimeType);
263: existingObject = capabilities.getMimeType("TEST MIME TYPE");
264: assertNotNull(
265: "creating and saving new mimetype didn't store object",
266: existingObject);
267:
268: capabilities.deleteMimeType(mimeType);
269: existingObject = capabilities.getMimeType("TEST MIME TYPE");
270: assertNull(
271: "creating new mimetype delete from storage didn't work",
272: existingObject);
273:
274: }
275:
276: public void testNewCapability() throws Exception {
277: Capability capability = null;
278: Iterator _it = null;
279: HashMap _hash = getCapabilities(0);
280: int count = _hash.size();
281: assertTrue("Capabilitys do not exist", (count > 0));
282:
283: _it = _hash.keySet().iterator();
284:
285: int pos = count / 2;
286:
287: for (int i = 0; i < pos; i++)
288: _it.next();
289:
290: String existingKey = (String) _it.next();
291: Capability existingObject = (Capability) _hash.get(existingKey);
292: assertNotNull(
293: "Couldn't identify existing mime object to run test",
294: existingObject);
295:
296: // "create" existing one
297: capability = capabilities.createCapability(existingKey);
298: assertNotNull("creating 'existing' capability returns null",
299: capability);
300: assertTrue(
301: "creating 'existing' capability didn't return existing object",
302: (capability.equals(existingObject)));
303:
304: // create a new one:
305: capability = capabilities
306: .createCapability("TEST CAPABILITY TYPE");
307: assertNotNull("creating new capability returns null",
308: capability);
309:
310: // ensure it doesn't exist in the capabilities
311: Set existing = _hash.entrySet();
312: assertTrue("creating new capability already in existing list",
313: (!(existing.contains(capability))));
314:
315: existingObject = capabilities
316: .getCapability("TEST CAPABILITY TYPE");
317: assertNull(
318: "creating new capability already in existing capabilities",
319: existingObject);
320:
321: capabilities.storeCapability(capability);
322: existingObject = capabilities
323: .getCapability("TEST CAPABILITY TYPE");
324: assertNotNull(
325: "creating and saving new capability didn't store object",
326: existingObject);
327:
328: capabilities.deleteCapability(capability);
329: existingObject = capabilities
330: .getCapability("TEST CAPABILITY TYPE");
331: assertNull(
332: "creating new capability delete from storage didn't work",
333: existingObject);
334:
335: }
336:
337: public void testNewMediaType() throws Exception {
338: MediaType mediaType = null;
339: Iterator _it = capabilities.getMediaTypes();
340: HashMap _hash = new HashMap();
341: int count = 0;
342: while (_it.hasNext()) {
343: mediaType = (MediaType) _it.next();
344: _hash.put(mediaType.getName(), mediaType);
345: count++;
346: }
347: assertTrue("Mediatypes do not exist", (count > 0));
348:
349: _it = _hash.keySet().iterator();
350:
351: int pos = count / 2;
352:
353: for (int i = 0; i < pos; i++)
354: _it.next();
355:
356: String existingKey = (String) _it.next();
357: MediaType existingObject = (MediaType) _hash.get(existingKey);
358: assertNotNull("Couldn't identify existing object to run test",
359: existingObject);
360:
361: // "create" existing one
362: mediaType = capabilities.createMediaType(existingKey);
363: assertNotNull("creating 'existing' mediatype returns null",
364: mediaType);
365: assertTrue(
366: "creating 'existing' mediatype didn't return existing object",
367: (mediaType.equals(existingObject)));
368:
369: // setting fields
370: String name = "TEST MEDIA TYPE";
371: String utf = "UTF-8";
372: String title = "TEST MEDIA TYPE - Title";
373: String description = "TEST MEDIA TYPE - Description";
374:
375: int numCapabilities = 2;
376: int numMimeTypes = 3;
377:
378: HashMap someCapabilities = getCapabilities(numCapabilities);
379: HashMap someMimeTypes = getMimeTypes(numMimeTypes);
380:
381: // create a new one:
382: mediaType = capabilities.createMediaType(name);
383: assertNotNull("creating new mediatype returns null", mediaType);
384:
385: // ensure it doesn't exist in the capabilities
386: Set existing = _hash.entrySet();
387: assertTrue("creating new mediaType already in existing list",
388: (!(existing.contains(mediaType))));
389:
390: existingObject = capabilities.getMediaType(name);
391: assertNull(
392: "creating new mediaType already in existing capabilities",
393: existingObject);
394:
395: // set object fields
396: mediaType.setCharacterSet(utf);
397: mediaType.setTitle(title);
398: mediaType.setDescription(description);
399:
400: _it = someMimeTypes.values().iterator();
401: int added = 0;
402: while (_it.hasNext()) {
403: mediaType.addMimetype((MimeType) _it.next());
404: added++;
405: }
406: assertTrue("number of Mimetypes added (" + added
407: + ") not the same as expected (" + numMimeTypes + ")",
408: (added == numMimeTypes));
409:
410: // setting links:
411:
412: ArrayList set = new ArrayList(someCapabilities.values());
413: mediaType.setCapabilities(set);
414: assertTrue("number of Capabilities added (" + set.size()
415: + ") not the same as expected (" + numCapabilities
416: + ")", (set.size() == numCapabilities));
417:
418: capabilities.storeMediaType(mediaType);
419: existingObject = capabilities.getMediaType(name);
420: assertNotNull(
421: "creating and saving new mediaType didn't store object",
422: existingObject);
423:
424: capabilities.deleteMediaType(mediaType);
425: existingObject = capabilities.getMediaType(name);
426: assertNull(
427: "creating new mediaType delete from storage didn't work",
428: existingObject);
429:
430: }
431:
432: public void testNewClient() throws Exception {
433: Client client = null;
434: Iterator _it = capabilities.getClients();
435: HashMap _hash = new HashMap();
436: int count = 0;
437: while (_it.hasNext()) {
438: client = (Client) _it.next();
439: _hash.put(client.getName(), client);
440: count++;
441: }
442: assertTrue("Clients do not exist", (count > 0));
443:
444: _it = _hash.keySet().iterator();
445:
446: int pos = count / 2;
447:
448: for (int i = 0; i < pos; i++)
449: _it.next();
450:
451: String existingKey = (String) _it.next();
452: Client existingObject = (Client) _hash.get(existingKey);
453: assertNotNull("Couldn't identify existing object to run test",
454: existingObject);
455:
456: // "create" existing one
457: client = capabilities.createClient(existingKey);
458: assertNotNull("creating 'existing' client returns null", client);
459: assertTrue(
460: "creating 'existing' client didn't return existing object",
461: (client.equals(existingObject)));
462:
463: // setting fields
464:
465: String name = "TEST CLIENT";
466: int numCapabilities = 3;
467: int numMimeTypes = 4;
468:
469: HashMap someCapabilities = getCapabilities(numCapabilities);
470: HashMap someMimeTypes = getMimeTypes(numMimeTypes);
471:
472: // create a new one:
473: client = capabilities.createClient(name);
474: assertNotNull("creating new client returns null", client);
475:
476: // ensure it doesn't exist in the capabilities
477: Set existing = _hash.entrySet();
478: assertTrue("creating new client already in existing list",
479: (!(existing.contains(client))));
480:
481: existingObject = capabilities.getClient(name);
482: assertNull(
483: "creating new client already in existing capabilities",
484: existingObject);
485:
486: String userAgentPattern = "TEST.*|TESTBROWSER.*";
487: String manufacturer = "Test Manufacturer";
488: String model = "XYZ";
489:
490: // set object fields
491: client.setUserAgentPattern(userAgentPattern);
492: client.setManufacturer(manufacturer);
493: client.setModel(model);
494:
495: ArrayList set = new ArrayList(someCapabilities.values());
496: client.setCapabilities(set);
497: assertTrue("number of Capabilities added (" + set.size()
498: + ") not the same as expected (" + numCapabilities
499: + ")", (set.size() == numCapabilities));
500:
501: set = new ArrayList(someMimeTypes.values());
502: client.setCapabilities(set);
503: assertTrue("number of MimeTypes added (" + set.size()
504: + ") not the same as expected (" + numCapabilities
505: + ")", (set.size() == numMimeTypes));
506:
507: // setting links:
508:
509: capabilities.storeClient(client);
510: existingObject = capabilities.getClient(name);
511: assertNotNull(
512: "creating and saving new client didn't store object",
513: existingObject);
514:
515: capabilities.deleteClient(client);
516: existingObject = capabilities.getClient(name);
517: assertNull(
518: "creating new client delete from storage didn't work",
519: existingObject);
520:
521: }
522:
523: public void testCapabilityRepeat() throws Exception {
524: capabilities.deleteCapabilityMapCache();
525: testCapability();
526: }
527:
528: protected String[] getConfigurations() {
529: return new String[] { "capabilities.xml", "transaction.xml" };
530: }
531:
532: }
|