|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.cloud.network.security; |
| 5 | + |
| 6 | +import java.util.Set; |
| 7 | +import java.util.TreeSet; |
| 8 | + |
| 9 | +import javax.inject.Inject; |
| 10 | + |
| 11 | +import junit.framework.TestCase; |
| 12 | + |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.springframework.test.context.ContextConfiguration; |
| 17 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 18 | + |
| 19 | +import com.cloud.network.security.SecurityGroupManagerImpl.CidrComparator; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author daan |
| 23 | + * |
| 24 | + */ |
| 25 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 26 | +@ContextConfiguration(locations = "classpath:/SecurityGroupManagerTestContext.xml") |
| 27 | +public class SecurityGroupManagerImplTest extends TestCase { |
| 28 | + @Inject |
| 29 | + SecurityGroupManagerImpl2 _sgMgr = null; |
| 30 | + Set<String> cidrs; |
| 31 | + @Before |
| 32 | + public void setup() throws Exception { |
| 33 | + cidrs = new TreeSet<String>(new CidrComparator()); |
| 34 | + } |
| 35 | + @Test (expected=NumberFormatException.class) |
| 36 | + public void emptyCidrCompareTest() { |
| 37 | + cidrs.add(""); |
| 38 | + cidrs.add(""); |
| 39 | + } |
| 40 | + @Test (expected=NumberFormatException.class) |
| 41 | + public void faultyCidrCompareTest() { |
| 42 | + cidrs.add("111.222.333.444"); |
| 43 | + cidrs.add("111.222.333.444"); |
| 44 | + } |
| 45 | + @Test |
| 46 | + public void sameCidrCompareTest() { |
| 47 | + cidrs.add("1.2.3.4/5"); |
| 48 | + cidrs.add("1.2.3.4/5"); |
| 49 | + assertEquals("only one element expected",1,cidrs.size()); |
| 50 | + CidrComparator cmp = new CidrComparator(); |
| 51 | + assertEquals("should be 0",0,cmp.compare("1.2.3.4/5","1.2.3.4/5")); |
| 52 | + } |
| 53 | + @Test |
| 54 | + public void CidrCompareTest() { |
| 55 | + cidrs.add("1.2.3.4/5"); |
| 56 | + cidrs.add("1.2.3.4/6"); |
| 57 | + assertEquals("two element expected",2,cidrs.size()); |
| 58 | + CidrComparator cmp = new CidrComparator(); |
| 59 | + assertEquals("should be 1",1,cmp.compare("1.2.3.4/5","1.2.3.4/6")); |
| 60 | + assertEquals("should be -2",-2,cmp.compare("1.2.3.4/5","1.2.3.4/3")); |
| 61 | + } |
| 62 | +} |
0 commit comments