source: src/test/java/negotiator/BidTest.java@ 127

Last change on this file since 127 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 2.3 KB
RevLine 
[1]1package negotiator;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.ByteArrayInputStream;
6import java.io.ByteArrayOutputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.UnsupportedEncodingException;
10import java.nio.charset.StandardCharsets;
11import java.util.Random;
12
13import javax.xml.bind.JAXBContext;
14import javax.xml.bind.JAXBException;
15import javax.xml.bind.Marshaller;
16import javax.xml.bind.Unmarshaller;
17
18import org.junit.Before;
19import org.junit.Test;
20
21import genius.core.Bid;
22import genius.core.Domain;
23import genius.core.DomainImpl;
24
25/**
26 * Test if Bid works properly
27 *
28 */
29public 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}
Note: See TracBrowser for help on using the repository browser.