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