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