objectsavetest.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 808 行 · 第 1/2 页

JAVA
808
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.axis2.engine;

import junit.framework.TestCase;

import org.apache.axis2.util.MetaDataEntry;
import org.apache.axis2.util.ObjectStateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.xml.namespace.QName;
import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

public class ObjectSaveTest extends TestCase {
    protected static final Log log = LogFactory.getLog(ObjectSaveTest.class);
    
    private String testArg = null;

	// simple constructor needed for nested class Externalizable interface
	public ObjectSaveTest() {
	}

	public ObjectSaveTest(String arg0) {
		super(arg0);
		testArg = new String(arg0);
	}

	protected void setUp() throws Exception {
		// org.apache.log4j.BasicConfigurator.configure();
	}

	public void testObjectSerializable() throws Exception {
		File theFile = null;
		String theFilename = null;
		boolean saved = false;
		boolean restored = false;
		boolean done = false;

        log.debug("ObjectSaveTest:testObjectSerializable():  BEGIN ---------------");

		// ---------------------------------------------------------
		// setup an object to use
		// ---------------------------------------------------------
		MetaDataEntry obj = new MetaDataEntry("object_1", "object_1");

		// ---------------------------------------------------------
		// setup a temporary file to use
		// ---------------------------------------------------------
		try {
			theFile = File.createTempFile("objectTest", null);
			theFilename = theFile.getName();
            log.debug("ObjectSaveTest:testObjectSerializable(): temp file = ["
                    + theFilename + "]");
		} catch (Exception ex) {
            log.debug("ObjectSaveTest:testObjectSerializable(): error creating temp file = ["
                    + ex.getMessage() + "]");
			theFile = null;
		}

		if (theFile != null) {
			// ---------------------------------------------------------
			// save to the temporary file
			// ---------------------------------------------------------
			try {
				// setup an output stream to a physical file
				FileOutputStream outStream = new FileOutputStream(theFile);

				// attach a stream capable of writing objects to the
				// stream connected to the file
				ObjectOutputStream outObjStream = new ObjectOutputStream(
						outStream);

				// try to save
                log.debug("ObjectSaveTest:testObjectSerializable(): saving .....");
				saved = false;
				ObjectStateUtils.writeObject(outObjStream, obj,
						"testObject:Serializable");

				// close out the streams
				outObjStream.flush();
				outObjStream.close();
				outStream.flush();
				outStream.close();

				saved = true;
                log.debug(
                        "ObjectSaveTest:testObjectSerializable(): ....save operation completed.....");

				long filesize = theFile.length();
                log.debug("ObjectSaveTest:testObjectSerializable(): file size after save ["
                        + filesize
                        + "]   temp file = ["
                        + theFilename
                        + "]");
			} catch (Exception ex2) {
                log.debug("ObjectSaveTest:testObjectSerializable(): error during save ["
                        + ex2.getClass().getName()
                        + " : "
                        + ex2.getMessage() + "]");
				ex2.printStackTrace();
			}

			assertTrue(saved);

			// ---------------------------------------------------------
			// restore from the temporary file
			// ---------------------------------------------------------
			try {
				// setup an input stream to the file
				FileInputStream inStream = new FileInputStream(theFile);

				// attach a stream capable of reading objects from the
				// stream connected to the file
				ObjectInputStream inObjStream = new ObjectInputStream(inStream);

				// try to restore the options
                log.debug("ObjectSaveTest:testObjectSerializable(): restoring .....");
				restored = false;
				MetaDataEntry restored_obj = (MetaDataEntry) ObjectStateUtils
						.readObject(inObjStream, "testObject:serializable");
				inObjStream.close();
				inStream.close();

				restored = true;
                log.debug(
                        "ObjectSaveTest:testObjectSerializable(): ....restored operation completed.....");

			} catch (Exception ex2) {
                log.debug("ObjectSaveTest:testObjectSerializable(): error during restore ["
                        + ex2.getClass().getName()
                        + " : "
                        + ex2.getMessage() + "]");
				ex2.printStackTrace();
			}

			assertTrue(restored);

			// if the save/restore of the object succeeded,
			// then don't keep the temporary file around
			boolean removeTmpFile = saved && restored;
			if (removeTmpFile) {
				try {
					theFile.delete();
				} catch (Exception e) {
					// just absorb it
				}
			}

			// indicate that the temp file was created ok
			done = true;
		}

		// this is false when there are problems with the temporary file
		assertTrue(done);

        log.debug("ObjectSaveTest:testObjectSerializable():  END ---------------");
	}

	public void testObjectNotSerializable() throws Exception {
		File theFile = null;
		String theFilename = null;
		boolean saved = false;
		boolean restored = false;
		boolean expected_exception = false;
		boolean done = false;

        log.debug("ObjectSaveTest:testObjectNotSerializable():  BEGIN ---------------");

		// ---------------------------------------------------------
		// setup an object to use
		// ---------------------------------------------------------
		NotSerializableObject obj = new NotSerializableObject("nso_1");

		// ---------------------------------------------------------
		// setup a temporary file to use
		// ---------------------------------------------------------
		try {
			theFile = File.createTempFile("objectTest", null);
			theFilename = theFile.getName();
            log.debug("ObjectSaveTest:testObjectNotSerializable(): temp file = ["
                    + theFilename + "]");
		} catch (Exception ex) {
            log.debug("ObjectSaveTest:testObjectNotSerializable(): error creating temp file = ["
                    + ex.getMessage() + "]");
			theFile = null;
		}

		if (theFile != null) {
			// ---------------------------------------------------------
			// save to the temporary file
			// ---------------------------------------------------------
			FileOutputStream outStream = null;
			ObjectOutputStream outObjStream = null;
			try {
				// setup an output stream to a physical file
				outStream = new FileOutputStream(theFile);

				// attach a stream capable of writing objects to the
				// stream connected to the file
				outObjStream = new ObjectOutputStream(outStream);

				// try to save
                log.debug("ObjectSaveTest:testObjectNotSerializable(): saving .....");
				saved = false;
				ObjectStateUtils.writeObject(outObjStream, obj,
						"testObject:NotSerializable");

				saved = true;
                log.debug(
                        "ObjectSaveTest:testObjectNotSerializable(): ....save operation completed.....");

				long filesize = theFile.length();
                log.debug("ObjectSaveTest:testObjectNotSerializable(): file size after save ["
                        + filesize
                        + "]   temp file = ["
                        + theFilename
                        + "]");
			} catch (Exception ex2) {
				// expect an error here
				// ObjectStateUtils catches the NotSerializableException and
				// logs it
				if (ex2 instanceof NotSerializableException) {
					expected_exception = true;
				} else {
                    log.debug("ObjectSaveTest:testObjectNotSerializable():  save ["
                            + ex2.getClass().getName()
                            + " : "
                            + ex2.getMessage() + "]");
				}
			}
			// close out the streams
			if (outObjStream != null)
				outObjStream.close();
			if (outStream != null)
				outStream.close();

			// ---------------------------------------------------------
			// restore from the temporary file
			// ---------------------------------------------------------
			try {
				// setup an input stream to the file
				FileInputStream inStream = new FileInputStream(theFile);

				// attach a stream capable of reading objects from the
				// stream connected to the file
				ObjectInputStream inObjStream = new ObjectInputStream(inStream);

				// try to restore the options
                log.debug("ObjectSaveTest:testObjectSerializable(): restoring .....");
				restored = false;
				Object restored_obj = ObjectStateUtils.readObject(inObjStream,
						"testObject:NotSerializable");
				inObjStream.close();
				inStream.close();

				restored = true;
                log.debug(
                        "ObjectSaveTest:testObjectNotSerializable(): ....restored operation completed.....");

			} catch (Exception ex) {
                log.debug("ObjectSaveTest:testObjectNotSerializable(): error during restore ["
                        + ex.getClass().getName()
                        + " : "
                        + ex.getMessage() + "]");
				ex.printStackTrace();
			}

			assertTrue(restored);

			// if the save/restore of the object succeeded,
			// then don't keep the temporary file around
			boolean removeTmpFile = saved && restored;
			if (removeTmpFile) {
				try {
					theFile.delete();
				} catch (Exception e) {
					// just absorb it
				}
			}

			assertTrue(expected_exception);
		}

        log.debug("ObjectSaveTest:testObjectNotSerializable():  END ---------------");
	}

	public void testArrayList() throws Exception {
		File theFile = null;
		String theFilename = null;
		boolean saved = false;
		boolean restored = false;
		boolean done = false;
		boolean comparesOK = false;

        log.debug("ObjectSaveTest:testArrayList():  BEGIN ---------------");

		// ---------------------------------------------------------
		// setup the object to use
		// ---------------------------------------------------------
		ArrayList obj = new ArrayList();
		obj.add(new Integer(1));
		obj.add(new Integer(2));
		obj.add(new Integer(3));
		obj.add(new String("string1"));
		obj.add(new String("string2"));
		obj.add(System.out);
		obj.add(new Integer(4));
		obj.add(new Integer(5));
		obj.add(new Integer(6));

		int initial_size = obj.size();

		// ---------------------------------------------------------
		// setup a temporary file to use
		// ---------------------------------------------------------
		try {
			theFile = File.createTempFile("arraylistTest", null);
			theFilename = theFile.getName();
            log.debug("ObjectSaveTest:testArrayList(): temp file = ["
                    + theFilename + "]");
		} catch (Exception ex) {
            log.debug("ObjectSaveTest:testArrayList(): error creating temp file = ["
                    + ex.getMessage() + "]");
			theFile = null;
		}

		if (theFile != null) {
			// ---------------------------------------------------------
			// save to the temporary file
			// ---------------------------------------------------------
			try {
				// setup an output stream to a physical file
				FileOutputStream outStream = new FileOutputStream(theFile);

				// attach a stream capable of writing objects to the
				// stream connected to the file
				ObjectOutputStream outObjStream = new ObjectOutputStream(
						outStream);

				// try to save
                log.debug("ObjectSaveTest:testArrayList(): saving .....");
				saved = false;
				ObjectStateUtils.writeArrayList(outObjStream, obj,
						"testObject:ArrayList");

				// close out the streams
				outObjStream.flush();
				outObjStream.close();
				outStream.flush();
				outStream.close();

				saved = true;
                log.debug("ObjectSaveTest:testArrayList(): ....save operation completed.....");

				long filesize = theFile.length();
                log.debug("ObjectSaveTest:testArrayList(): file size after save ["
                        + filesize
                        + "]   temp file = ["
                        + theFilename
                        + "]");
			} catch (Exception ex2) {
                log.debug("ObjectSaveTest:testArrayList(): error during save ["
                        + ex2.getClass().getName()
                        + " : "
                        + ex2.getMessage() + "]");
				ex2.printStackTrace();
			}

			assertTrue(saved);

			// ---------------------------------------------------------
			// restore from the temporary file
			// ---------------------------------------------------------
			ArrayList restored_obj = null;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?