您的位置:首页 > 编程语言 > Java开发

Java序列化(Serializable)、Junit及断言使用

2011-06-10 11:31 435 查看
ClassA
package com.seri;

import java.io.Serializable;

/**
* 实现了Serializable接口的对象,可将它们转换成一系列字节,并可在以后完全恢复回原来的样子。
* 这一过程亦可通过网络进行。这意味着序列化机制能自动补偿操作系统间的差异。
* 换句话说,可以先在Windows机器上创建一个对象,对其序列化,然后通过网络发给一台Unix机器,然后在那里准确无误地重新“装配”。
* 不必关心数据在不同机器上如何表示,也不必关心字节的顺序或者其他任何细节。
*
* 实际上,序列化的思想是 “冻结” 对象状态,传输对象状态(写到磁盘、通过网络传输等等),然后 “解冻” 状态,重新获得可用的 Java 对象。
* 所有这些事情的发生有点像是魔术,这要归功于 ObjectInputStream/ObjectOutputStream 类、完全保真的元数据以及程序员愿意用
* Serializable 标识接口标记他们的类,从而 “参与” 这个过程。
*/
public class ClassA implements Serializable {

/**
* 为保证 serialVersionUID 值跨不同 java 编译器实现的一致性,序列化类必须声明一个明确的 serialVersionUID 值
*/
private static final long serialVersionUID = 6013572251564847381L;
private String name = "My name is a";
private ClassB b = null;

ClassA() {
b = new ClassB();
}

public String show() {

System.out.println("a.toString <a.name=\"" + this.name
+ "\" a.b.name=\"" + this.b.getName() + "\">");

// return "a.toString <a.name=\""+this.name+"\"
// a.b.name=\""+this.b.getName()+"\">";
// result:write obj:a.toString <a.name="My name is a" a.b.name="My name
// is b">
return "a.toString <a.name=" + this.name + "a.b.name="
+ this.b.getName() + ">";
// result:write obj:a.toString <a.name=My name is aa.b.name=My name is
// b>
// \ 作用是转义 "(双引号)
// \' '(单引号)
// \\ 反斜线
}

public int add(int a, int b) {
return a + b;
}

public int divide(int a, int b) throws Exception {
if (0 == b) {
throw new Exception("除数不能为零");
}
return a / b;
}

public int exception(int a, int b) throws Exception {
if (a == 2) {
throw new Exception("进入我设置的异常啦!");
}
return a + b;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public ClassB getB() {
return b;
}

public void setB(ClassB b) {
this.b = b;
}

}
ClassBpackage com.seri;

import java.io.Serializable;

public class ClassB implements Serializable{

private static final long serialVersionUID = -4324044767844361076L;

private String name="My name is b";

ClassB(){}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

WriteSeripackage com.seri;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class WriteSeri {

public static void main(String args[]){
ObjectOutputStream outObj=null;
try {
FileOutputStream outStr=new FileOutputStream("obj.txt");
outObj=new ObjectOutputStream(outStr);
ClassA a=new ClassA();
outObj.writeObject(a);
System.out.println("write obj:"+a.show());
outObj.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (outObj != null) {
outObj.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

ReadSeripackage com.seri;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ReadSeri {

public static void main(String args[]) {
ObjectInputStream inObj = null;
try {
FileInputStream inStr = new FileInputStream("obj.txt");
inObj = new ObjectInputStream(inStr);
ClassA a = (ClassA) inObj.readObject();
System.out.println("read object:" + a.show());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (inObj != null) {
try {
inObj.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

ClassATestpackage com.seri;

import junit.framework.Assert;
import junit.framework.TestCase;

public class ClassATest extends TestCase {

ClassA a = new ClassA();

public void testShow() {
// 覆盖原来的name
a.setName("hello TestA");
a.getB().setName("hello TestB");
a.show();
}

public void testAdd() {
// boolean b=false;
// Assert.assertEquals(true, b);
int result = 0;
result = a.add(2, 5);
Assert.assertEquals(7, result);
}

public void testDivide() {
int result = 0;
try {
result = a.divide(10, 5);
} catch (Exception e) {
e.printStackTrace(); // 如果进入到catch中说明执行divide失败
System.out.println("执行上面那句话了");
Assert.fail();
}
Assert.assertEquals(2, result);
}

public void testException() {
int result = 0;
try {
result = a.exception(3, 4);
} catch (Exception e) {
e.printStackTrace();
System.out.println("执行上面那句话了");
Assert.fail();
}
Assert.assertEquals(7, result);
}
}

本文出自 “每天进步一点点” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: