View Javadoc

1   package org.itracker.model;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertNotNull;
6   import static org.junit.Assert.assertTrue;
7   import static org.junit.Assert.fail;
8   
9   import org.junit.After;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  public class NameValuePairTest {
14  	private NameValuePair nvp;
15  
16  	@Test
17  	public void testSetName() {
18  		nvp.setName("jerry");
19  		assertEquals("name#jerry", "jerry", nvp.getName());
20  		nvp.setName(null);
21  		assertEquals("name is empty", "", nvp.getName());
22  	}
23  
24  	@Test
25  	public void testCompareKeyTo() {
26  		NameValuePair nvpCopy = new NameValuePair("name", "value");
27  		assertEquals(0, nvp.compareTo(nvpCopy));
28  
29  		nvpCopy.setName("name1");
30  		assertEquals(-1, nvp.compareKeyTo(nvpCopy));
31  
32  		nvpCopy.setName(null);
33  		assertEquals(4, nvp.compareKeyTo(nvpCopy));
34  
35  		// test when NameValuePair is null
36  		nvpCopy = null;
37  		try {
38  			nvp.compareKeyTo(nvpCopy);
39  			fail("did not throw NullpointerException");
40  		} catch (NullPointerException e) {
41  			assertTrue(true);
42  		}
43  	}
44  
45  	@Test
46  	public void testCompareValueTo() {
47  		NameValuePair nvpCopy = new NameValuePair("name", "value");
48  		assertEquals(0, nvp.compareValueTo(nvpCopy));
49  
50  		nvpCopy.setValue("value1");
51  		assertEquals(-1, nvp.compareValueTo(nvpCopy));
52  
53  		nvpCopy.setValue(null);
54  		try {
55  			assertEquals(0, nvp.compareValueTo(nvpCopy));
56  			fail("did not throw NullPointerException");
57  		} catch (NullPointerException e) {
58  			assertTrue(true);
59  		}
60  		// test when NameValuePair is null
61  		nvpCopy = null;
62  		try {
63  			nvp.compareValueTo(nvpCopy);
64  			fail("did not throw NullPointerException");
65  		} catch (NullPointerException e) {
66  			assertTrue(true);
67  		}
68  	}
69  
70  	@Test
71  	public void testEquals() {
72  		NameValuePair nvpCopy = nvp;
73  		assertTrue("nvp equals nvpCopy", nvp.equals(nvpCopy));
74  
75  		nvpCopy = new NameValuePair("name", "value");
76  		nvpCopy.setId(nvp.getId());
77  		assertFalse("nvp not equals nvpCopy", nvp.equals(nvpCopy));
78  
79  		nvpCopy = new NameValuePair("name", "value");
80  		assertFalse("nvp not equals nvpCopy", nvp.equals(nvpCopy));
81  
82  		assertFalse("nvp not equals nvpCopy", nvp.equals(new User()));
83  		assertFalse("nvp not equals nvpCopy", nvp.equals(null));
84  	}
85  
86  
87  	@Test
88  	public void testToString() {
89  		assertNotNull("toString", nvp.toString());
90  	}
91  
92  	@Before
93  	public void setUp() throws Exception {
94  		nvp = new NameValuePair("name", "value");
95  	}
96  
97  	@After
98  	public void tearDown() throws Exception {
99  		nvp = null;
100 	}
101 
102 }