objectsavetest.java

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

JAVA
808
字号

			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:testArrayList(): restoring .....");
				restored = false;
				restored_obj = ObjectStateUtils.readArrayList(inObjStream,
						"testObject:ArrayList");
				inObjStream.close();
				inStream.close();

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

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

			// 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(restored);

			if (restored_obj != null) {
				int restored_size = restored_obj.size();
				if (restored_size == (initial_size - 1)) {
					comparesOK = true;
				}
			}

			// TODO: check for exact entries

			assertTrue(comparesOK);

			// 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:testArrayList():  END ---------------");
	}

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

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

		// ---------------------------------------------------------
		// setup the object to use
		// ---------------------------------------------------------
		HashMap obj = new HashMap();
		obj.put(new String("key1"), new Integer(1));
		obj.put(new String("key2"), new Integer(2));
		obj.put(new String("key3"), new String("value1"));
		obj.put(new String("key4"), System.out);
		obj.put(new String("key5"), new Integer(3));
		obj.put(new String("key6"), new Integer(4));
		obj.put(new String("key7"), System.err);
		obj.put(new String("key8"), new Integer(5));
		obj.put(new String("key9"), new Integer(6));
		obj.put(new NotSerializableObject("TestForHashMapKey"), new Integer(7));
		obj.put(new String("key10"), new Integer(8));

		int initial_size = obj.size();

		// ---------------------------------------------------------
		// setup a temporary file to use
		// ---------------------------------------------------------
		try {
			theFile = File.createTempFile("hashmapTest", null);
			theFilename = theFile.getName();
            log.debug("ObjectSaveTest:testHashMap(): temp file = ["
                    + theFilename + "]");
		} catch (Exception ex) {
            log.debug("ObjectSaveTest:testHashMap(): 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:testHashMap(): saving .....");
				saved = false;
				ObjectStateUtils.writeHashMap(outObjStream, obj,
						"testObject:HashMap");

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

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

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

			assertTrue(saved);

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

			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:testHashMap(): restoring .....");
				restored = false;
				restored_obj = ObjectStateUtils.readHashMap(inObjStream,
						"testObject:HashMap");
				inObjStream.close();
				inStream.close();

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

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

			// 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(restored);

			if (restored_obj != null) {
				int restored_size = restored_obj.size();
				if (restored_size == (initial_size - 3)) {
					// there are entries in the map that are not serializable
					comparesOK = true;
				}
			}

			// TODO: check for exact entries

			assertTrue(comparesOK);

			// 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:testHashMap():  END ---------------");
	}

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

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

		// ---------------------------------------------------------
		// setup the object to use
		// ---------------------------------------------------------
		LinkedList obj = new LinkedList();
		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.in);
		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("linkedlistTest", null);
			theFilename = theFile.getName();
            log.debug("ObjectSaveTest:testLinkedList(): temp file = ["
                    + theFilename + "]");
		} catch (Exception ex) {
            log.debug("ObjectSaveTest:testLinkedList(): 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:testLinkedList(): saving .....");
				saved = false;
				ObjectStateUtils.writeLinkedList(outObjStream, obj,
						"testObject:LinkedList");

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

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

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

			assertTrue(saved);

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

			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:testLinkedList(): restoring .....");
				restored = false;
				restored_obj = ObjectStateUtils.readLinkedList(inObjStream,
						"testObject:LinkedList");
				inObjStream.close();
				inStream.close();

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

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

			// 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(restored);

			if (restored_obj != null) {
				int restored_size = restored_obj.size();
				if (restored_size == (initial_size - 1)) {
					comparesOK = true;
				}
			}

			// TODO: check for exact entries

			assertTrue(comparesOK);

			// 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:testLinkedList():  END ---------------");
	}

	public class NotSerializableObject implements Externalizable {
		private String label = "TestObject";

		private String ID = null;

		// make sure we have some objects that don't serialize
		private PrintStream ps = System.out;

		// default constructor needed for Externalizable interface
		public NotSerializableObject() {
		}

		public NotSerializableObject(String identifier) {
			ID = identifier;
			ps = System.out;
		}

		public void setID(String s) {
			ID = s;
		}

		public String getID() {
			return ID;
		}

		public void writeExternal(java.io.ObjectOutput out) throws IOException {
			throw new NotSerializableException(
					"Test Object is not serializable");
		}

		public void readExternal(java.io.ObjectInput in) throws IOException,
				ClassNotFoundException {
			throw new IOException("Test object is not serializable");
		}

	}

}

⌨️ 快捷键说明

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