f024bb4bdac53e736292953ab177566df14cf622
[jpim.git] / src / test / java / de / j32 / jpim / data / AddressbookTest.java
1 package de.j32.jpim.data;
2
3 import static org.junit.Assert.assertEquals;
4
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Test;
8
9 import de.j32.jpim.data.Addressbook;
10 import de.j32.jpim.data.Attribute;
11 import de.j32.jpim.data.Entry;
12 import de.j32.jpim.data.EntryConsumer;
13 import de.j32.util.Util;
14
15 public class AddressbookTest
16 {
17     Addressbook ab;
18
19     @Before
20     public void setUp() throws Exception
21     {
22         ab = new Addressbook();
23     }
24
25     @After
26     public void tearDown() throws Exception
27     {}
28
29     @Test
30     public final void testAdd()
31     {
32         Entry in = new Entry();
33         in.name("John Doe");
34         in.id(123);
35         in.attribute("phone", "work", "+12 345 67890");
36
37         ab.add(in);
38
39         Entry out = Util.first(ab);
40         assertEquals(in.name(), out.name());
41         assertEquals(in.id(), out.id());
42         Attribute inattr = Util.first(in.attributes());
43         Attribute outattr = Util.first(out.attributes());
44         assertEquals(inattr.type, outattr.type);
45         assertEquals(inattr.rel, outattr.rel);
46         assertEquals(inattr.value, outattr.value);
47         assertEquals(inattr.index, outattr.index);
48     }
49
50     @Test
51     public final void testConsume()
52     {
53         Entry in = new Entry();
54         in.name("John Doe");
55         in.id(124);
56         in.attribute("phone", "work", "+12 345 67890");
57
58         ab.consume(in);
59
60         Entry out = Util.first(ab);
61         assertEquals(in.name(), out.name());
62         assertEquals(in.id(), out.id());
63         Attribute inattr = Util.first(in.attributes());
64         Attribute outattr = Util.first(out.attributes());
65         assertEquals(inattr.type, outattr.type);
66         assertEquals(inattr.rel, outattr.rel);
67         assertEquals(inattr.value, outattr.value);
68         assertEquals(inattr.index, outattr.index);
69     }
70
71     @Test
72     public final void testSendTo()
73     {
74         Entry in = new Entry();
75         in.name("John Doe");
76         in.id(125);
77         in.attribute("phone", "work", "+12 345 67890");
78         ab.add(in);
79
80         final Entry[] out = { new Entry() };
81         ab.sendTo(new EntryConsumer() {
82             public void consume(Entry entry)
83             {
84                 out[0] = entry;
85             }
86         });
87
88         assertEquals(in.name(), out[0].name());
89         assertEquals(in.id(), out[0].id());
90         Attribute inattr = Util.first(in.attributes());
91         Attribute outattr = Util.first(out[0].attributes());
92         assertEquals(inattr.type, outattr.type);
93         assertEquals(inattr.rel, outattr.rel);
94         assertEquals(inattr.value, outattr.value);
95         assertEquals(inattr.index, outattr.index);
96     }
97
98     @Test
99     public final void testIterator()
100     {
101         Entry in = new Entry();
102         in.name("John Doe");
103         in.id(126);
104         in.attribute("phone", "work", "+12 345 67890");
105         ab.add(in);
106
107         for (Entry out : ab) {
108             assertEquals(in.name(), out.name());
109             assertEquals(in.id(), out.id());
110             Attribute inattr = Util.first(in.attributes());
111             Attribute outattr = Util.first(out.attributes());
112             assertEquals(inattr.type, outattr.type);
113             assertEquals(inattr.rel, outattr.rel);
114             assertEquals(inattr.value, outattr.value);
115             assertEquals(inattr.index, outattr.index);
116         }
117     }
118
119 }