Package com.peterhi.obsolete

Source Code of com.peterhi.obsolete.ModelIOTest

package com.peterhi.obsolete;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;

import com.peterhi.obsolete.ModelInputStream;
import com.peterhi.obsolete.ModelOutputStream;
import com.peterhi.obsolete.ModelUtilities;

public class ModelIOTest {

  public static void main(String[] args) throws Exception {
    Person jason = ModelUtilities.newInstance(Person.class);
    Person mary = ModelUtilities.newInstance(Person.class);
//    Pet kitty = ModelUtilities.newLocalInstance(Pet.class);
//    Pet benben = ModelUtilities.newLocalInstance(Pet.class);
    Pet kitty = new Pet();
    Pet benben = new Pet();
   
    jason.setName("Jason");
    jason.setAge(32);
    jason.aliases().add("JASON");
    jason.aliases().add("YZW");
    jason.setSpouse(mary);
   
    mary.setName("Mary");
    mary.setAge(27);
    mary.aliases().add("Mars");
    mary.aliases().add("Daisy");
    mary.aliases().add("Phi");
    mary.setSpouse(jason);
    mary.pets().add(kitty);
    mary.pets().add(benben);
   
    kitty.setName("Kitty");
    kitty.setAge(2);
    kitty.setOwner(mary);
    kitty.aliases().add("K");
   
    benben.setName("Benben");
    benben.setAge(6);
    benben.setOwner(mary);
    benben.aliases().add("SB");
    benben.aliases().add("B");
   
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ModelOutputStream mos = new ModelOutputStream(baos);
    mos.writeModel(jason);
   
    byte[] data = baos.toByteArray();
    System.out.println(baos.size());
   
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    ModelInputStream mis = new ModelInputStream(bais);
    Person person = mis.readModel();
    dumpResults(person);
  }
 
  private static void dumpResults(Person jason) {
    System.out.println(jason.getName());
    System.out.println(jason.getAge());
    System.out.println(jason.aliases());
    System.out.println(jason.getSpouse().getName());
   
    Person mary = jason.getSpouse();
    System.out.println(mary.getName());
    System.out.println(mary.getAge());
    System.out.println(mary.aliases());
    System.out.println(mary.getSpouse().getName());
   
    Iterator<Pet> iterator = mary.pets().iterator();
    Pet one = iterator.next();
    Pet two = iterator.next();
   
    System.out.println(one.getName());
    System.out.println(one.getAge());
    System.out.println(one.aliases());
    System.out.println(one.getOwner().getName());
   
    System.out.println(two.getName());
    System.out.println(two.getAge());
    System.out.println(two.aliases());
    System.out.println(two.getOwner().getName());
  }
}
TOP

Related Classes of com.peterhi.obsolete.ModelIOTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.