1 | package negotiator;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.ByteArrayInputStream;
|
---|
6 | import java.io.ByteArrayOutputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.UnsupportedEncodingException;
|
---|
10 | import java.nio.charset.StandardCharsets;
|
---|
11 | import java.util.Random;
|
---|
12 |
|
---|
13 | import javax.xml.bind.JAXBContext;
|
---|
14 | import javax.xml.bind.JAXBException;
|
---|
15 | import javax.xml.bind.Marshaller;
|
---|
16 | import javax.xml.bind.Unmarshaller;
|
---|
17 |
|
---|
18 | import org.junit.Before;
|
---|
19 | import org.junit.Test;
|
---|
20 |
|
---|
21 | import genius.core.Bid;
|
---|
22 | import genius.core.Domain;
|
---|
23 | import genius.core.DomainImpl;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Test if Bid works properly
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class BidTest {
|
---|
30 | private final static String RESOURCES = "src/test/resources/";
|
---|
31 | private static final String DISCRETEDOMAIN = RESOURCES + "partydomain/party_domain.xml";
|
---|
32 | private static final String INTEGERDOMAIN = RESOURCES + "IntegerDomain/IntegerDomain.xml";
|
---|
33 | private static final String REALDOMAIN = RESOURCES + "2nd_hand_car/car_domain.xml";
|
---|
34 | private static final String NONLINEARDOMAIN = RESOURCES + "S-1NIKFRT-1/S-1NIKFRT-1-domain.xml";
|
---|
35 | private Bid bid;
|
---|
36 |
|
---|
37 | @Before
|
---|
38 | public void before() throws IOException {
|
---|
39 | Domain domain = new DomainImpl(DISCRETEDOMAIN);
|
---|
40 | bid = domain.getRandomBid(new Random());
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Test
|
---|
45 | public void testSerializeXML() throws IOException, JAXBException {
|
---|
46 | System.out.println(serialize());
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Test
|
---|
50 | public void testDeserializeXML() throws JAXBException, UnsupportedEncodingException {
|
---|
51 | assertEquals(bid, deSerialize(serialize()));
|
---|
52 | }
|
---|
53 |
|
---|
54 | /***************** support funcs ***************/
|
---|
55 |
|
---|
56 | private String serialize() throws JAXBException {
|
---|
57 | ByteArrayOutputStream out = new ByteArrayOutputStream();
|
---|
58 | JAXBContext jaxbContext = JAXBContext.newInstance(Bid.class);
|
---|
59 | Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
---|
60 | jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
---|
61 | jaxbMarshaller.marshal(bid, out);
|
---|
62 | return new String(out.toByteArray());
|
---|
63 | }
|
---|
64 |
|
---|
65 | private Bid deSerialize(String string) throws JAXBException, UnsupportedEncodingException {
|
---|
66 | InputStream in = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8.name()));
|
---|
67 |
|
---|
68 | JAXBContext jaxbContext = JAXBContext.newInstance(Bid.class);
|
---|
69 |
|
---|
70 | Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
---|
71 | return (Bid) jaxbUnmarshaller.unmarshal(in);
|
---|
72 | }
|
---|
73 |
|
---|
74 | }
|
---|