Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.driver; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 22 | import java.util.Iterator; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 23 | |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | |
| 27 | /** |
| 28 | * Tests of the XML driver loader implementation. |
| 29 | */ |
| 30 | public class XmlDriverLoaderTest { |
| 31 | |
| 32 | @Test |
| 33 | public void basics() throws IOException { |
| 34 | XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); |
| 35 | InputStream stream = getClass().getResourceAsStream("drivers.1.xml"); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 36 | DriverProvider provider = loader.loadDrivers(stream, null); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 37 | System.out.println(provider); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 38 | assertEquals("incorrect driver count", 2, provider.getDrivers().size()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 39 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 40 | Iterator<Driver> iterator = provider.getDrivers().iterator(); |
| 41 | Driver driver = iterator.next(); |
| 42 | if (!driver.name().equals("foo.1")) { |
| 43 | driver = iterator.next(); |
| 44 | } |
| 45 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 46 | assertEquals("incorrect driver name", "foo.1", driver.name()); |
| 47 | assertEquals("incorrect driver mfg", "Circus", driver.manufacturer()); |
| 48 | assertEquals("incorrect driver hw", "1.2a", driver.hwVersion()); |
| 49 | assertEquals("incorrect driver sw", "2.2", driver.swVersion()); |
| 50 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 51 | assertEquals("incorrect driver behaviours", 1, driver.behaviours().size()); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 52 | assertTrue("incorrect driver behaviour", driver.hasBehaviour(TestBehaviour.class)); |
| 53 | |
| 54 | assertEquals("incorrect driver properties", 2, driver.properties().size()); |
| 55 | assertTrue("incorrect driver property", driver.properties().containsKey("p1")); |
| 56 | } |
| 57 | |
| 58 | @Test(expected = IOException.class) |
| 59 | public void badXML() throws IOException { |
| 60 | XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 61 | loader.loadDrivers(getClass().getResourceAsStream("drivers.bad.xml"), null); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test(expected = IllegalArgumentException.class) |
| 65 | public void noClass() throws IOException { |
| 66 | XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 67 | loader.loadDrivers(getClass().getResourceAsStream("drivers.noclass.xml"), null); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Test(expected = IllegalArgumentException.class) |
| 71 | public void noConstructor() throws IOException { |
| 72 | XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader()); |
| 73 | InputStream stream = getClass().getResourceAsStream("drivers.noconstructor.xml"); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 74 | DriverProvider provider = loader.loadDrivers(stream, null); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 75 | Driver driver = provider.getDrivers().iterator().next(); |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 76 | driver.createBehaviour(new DefaultDriverData(driver), TestBehaviour.class); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } |