Stuart McCulloch | 798b456 | 2009-04-16 09:32:05 +0000 | [diff] [blame] | 1 | package org.apache.felix.das.util; |
| 2 | |
| 3 | |
| 4 | import java.util.Properties; |
| 5 | |
| 6 | import org.apache.felix.das.OSGiMock; |
| 7 | import org.apache.felix.das.Utils; |
| 8 | import org.junit.Before; |
| 9 | import org.junit.Test; |
| 10 | import org.mockito.Mock; |
| 11 | import org.mockito.Mockito; |
| 12 | import org.mockito.MockitoAnnotations; |
| 13 | import org.osgi.framework.ServiceReference; |
| 14 | import org.osgi.service.device.Constants; |
| 15 | import org.osgi.service.device.Driver; |
| 16 | import org.osgi.service.log.LogService; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * |
| 21 | * Tests the Driver Analyzer. |
| 22 | * |
| 23 | * Nothing fancy is being tested, but if something is changed this |
| 24 | * validates that at least the most basic feedback can be expected. |
| 25 | * |
| 26 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 27 | * |
| 28 | */ |
| 29 | public class DriverAnalyzerTest { |
| 30 | |
| 31 | |
| 32 | |
| 33 | private DriverAnalyzer m_analyzer; |
| 34 | |
| 35 | |
| 36 | @Mock |
| 37 | private LogService m_log; |
| 38 | |
| 39 | private OSGiMock m_osgi; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() throws Exception { |
| 43 | |
| 44 | MockitoAnnotations.initMocks(this); |
| 45 | |
| 46 | m_osgi = new OSGiMock(); |
| 47 | m_analyzer = new DriverAnalyzer(); |
| 48 | |
| 49 | Utils.inject(m_analyzer, LogService.class, m_log); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | @Test |
| 54 | public void VerifyCorrectDriverIsIgnored() { |
| 55 | |
| 56 | |
| 57 | Properties p = new Properties(); |
| 58 | p.put(Constants.DRIVER_ID, "a-driver-id"); |
| 59 | |
| 60 | |
| 61 | ServiceReference ref = m_osgi.createReference(new String[]{Driver.class.getName()}, p); |
| 62 | |
| 63 | m_analyzer.driverAdded(ref); |
| 64 | |
| 65 | Mockito.verifyZeroInteractions(m_log); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void VerifyIncorrectDriverNoDriverId() { |
| 71 | |
| 72 | |
| 73 | Properties p = new Properties(); |
| 74 | |
| 75 | ServiceReference ref = m_osgi.createReference(new String[]{Driver.class.getName()}, p); |
| 76 | |
| 77 | m_analyzer.driverAdded(ref); |
| 78 | |
| 79 | Mockito.verify(m_log).log(Mockito.eq(LogService.LOG_ERROR), Mockito.anyString()); |
| 80 | Mockito.verifyNoMoreInteractions(m_log); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void VerifyIncorrectDriverInvalidDriverId() { |
| 86 | |
| 87 | Properties p = new Properties(); |
| 88 | p.put(Constants.DRIVER_ID, new Object()); |
| 89 | |
| 90 | ServiceReference ref = m_osgi.createReference(new String[]{Driver.class.getName()}, p); |
| 91 | |
| 92 | m_analyzer.driverAdded(ref); |
| 93 | |
| 94 | Mockito.verify(m_log).log(Mockito.eq(LogService.LOG_ERROR), Mockito.anyString()); |
| 95 | Mockito.verifyNoMoreInteractions(m_log); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void VerifyIncorrectDriverEmptyDriverId() { |
| 101 | |
| 102 | Properties p = new Properties(); |
| 103 | p.put(Constants.DRIVER_ID, ""); |
| 104 | |
| 105 | ServiceReference ref = m_osgi.createReference(new String[]{Driver.class.getName()}, p); |
| 106 | |
| 107 | m_analyzer.driverAdded(ref); |
| 108 | |
| 109 | Mockito.verify(m_log).log(Mockito.eq(LogService.LOG_ERROR), Mockito.anyString()); |
| 110 | Mockito.verifyNoMoreInteractions(m_log); |
| 111 | |
| 112 | } |
| 113 | } |