⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testboardencoder.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * MegaMek - Copyright (C) 2004 Ben Mazur (bmazur@sev.org) *  *  This program is free software; you can redistribute it and/or modify it  *  under the terms of the GNU General Public License as published by the Free  *  Software Foundation; either version 2 of the License, or (at your option)  *  any later version. *  *  This program is distributed in the hope that it will be useful, but  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License  *  for more details. */package megamek.test.xml;import gd.xml.*;import gd.xml.tiny.*;import java.io.*;import java.util.Enumeration;import java.util.zip.GZIPOutputStream;import megamek.common.*;import megamek.common.xml.BoardEncoder;/** * This class will confirm that the <code>BoardEncoder</code> is equivalent * to the serialization of a <code>Board</code> object. * * TODO: integrate JUnit into this class. */public class TestBoardEncoder {    /**     * Create a <code>Board</code>, encode it to XML, decode it from the     * XML, and compare the serialized versions from before and after the     * encoding.     */    public static void main( String[] args ) {        // The Game containing the Board.        IGame game = new Game();        IBoard board = game.getBoard();        Coords coords = null;        boolean success = true;        // Try to conduct the test.        try {            // The serialized board *before* it is encoded.            ByteArrayOutputStream before = new ByteArrayOutputStream();            GZIPOutputStream bzos = new GZIPOutputStream(before);            ObjectOutputStream boos = new ObjectOutputStream(bzos);            // The serialized board *after* it is encoded            ByteArrayOutputStream after = new ByteArrayOutputStream();            GZIPOutputStream azos = new GZIPOutputStream(after);            ObjectOutputStream aoos = new ObjectOutputStream(azos);            // The character writer for encoding.            CharArrayWriter to = new CharArrayWriter();            // Load the test board.            board.load( TestBoardEncoder.getTestInputStream() );            // Add some infernos and fires.            ITerrainFactory f = Terrains.getTerrainFactory();                        coords = new Coords( 5, 3 );            board.addInfernoTo( coords, InfernoTracker.STANDARD_ROUND, 1 );            board.getHex( coords ).addTerrain( f.createTerrain(Terrains.FIRE, 1) );            coords = new Coords( 8, 6 );            board.addInfernoTo( coords, InfernoTracker.STANDARD_ROUND, 1 );            board.getHex( coords ).addTerrain( f.createTerrain(Terrains.FIRE, 2) );            coords = new Coords( 4, 10 );            board.getHex( coords ).addTerrain( f.createTerrain(Terrains.FIRE, 2) );            coords = new Coords( 7, 13 );            board.addInfernoTo( coords, InfernoTracker.STANDARD_ROUND, 2 );            board.getHex( coords ).addTerrain( f.createTerrain(Terrains.FIRE, 2) );            coords = new Coords( 11, 14 );            board.getHex( coords ).addTerrain( f.createTerrain(Terrains.FIRE, 2) );            // Save a copy of the board before XML encoding.            boos.writeObject( board );            boos.close();            // Encode the board in XML.            BoardEncoder.encode( board, to );            to.close();            // Decode the board from XML.            ParsedXML root = TinyParser.parseXML                 ( new ByteArrayInputStream(to.toString().getBytes()) );            Enumeration rootChildren = root.elements();            if ( !rootChildren.hasMoreElements() ) {                throw new ParseException( "No children of the root." );            }            ParsedXML rootNode = (ParsedXML)rootChildren.nextElement();            board = BoardEncoder.decode( rootNode, game );            // Save a copy of the board before XML encoding.            aoos.writeObject( board );            aoos.close();            // Walk through the before and after, comparing each in turn.            byte[] beforeBuf = before.toByteArray();            byte[] afterBuf  = after.toByteArray();            if ( beforeBuf.length != afterBuf.length ) {                System.out.print( "Different lengths!!!  Before: " );                System.out.print( beforeBuf.length );                System.out.print( ", After: " );                System.out.print( afterBuf.length );                System.out.println( "." );                success = false;            } else {                System.out.print( "Comparing " );                System.out.print( beforeBuf.length );                System.out.println( " bytes." );                for ( int index = 0;                      success && index < beforeBuf.length;                      index++ ) {                    if ( beforeBuf[index] != afterBuf[index] ) {                        System.out.print( "Different bytes at index " );                        System.out.print( index );                        System.out.print( "!!!  Before: " );                        System.out.print( beforeBuf[index] );                        System.out.print( ", After: " );                        System.out.print( afterBuf[index] );                        System.out.println( "." );                        success = false;                    }                }            }        } catch ( Throwable err ) {            err.printStackTrace();            success = false;        }        if ( success ) {            System.out.println( "Success!!!" );        }    }    /**     * Create a test <code>Board</code> in an <code>InputStream</code>.     *     * @return  an <code>InputStream</code> containing a <code>Board</code>.     */    public static InputStream getTestInputStream() {        StringWriter buffer = new StringWriter();        PrintWriter writer = new PrintWriter( buffer );        // Write a test board to the writer.        writer.println( "size 16 17" );        writer.println( "hex 0101 0 \"\" \"\"" );        writer.println( "hex 0201 0 \"\" \"\"" );        writer.println( "hex 0301 0 \"\" \"\"" );        writer.println( "hex 0401 0 \"\" \"\"" );        writer.println( "hex 0501 0 \"\" \"\"" );        writer.println( "hex 0601 0 \"\" \"\"" );        writer.println( "hex 0701 0 \"\" \"\"" );        writer.println( "hex 0801 0 \"\" \"\"" );        writer.println( "hex 0901 0 \"\" \"\"" );        writer.println( "hex 1001 0 \"\" \"\"" );        writer.println( "hex 1101 0 \"\" \"\"" );        writer.println( "hex 1201 0 \"\" \"\"" );        writer.println( "hex 1301 0 \"\" \"\"" );        writer.println( "hex 1401 0 \"\" \"\"" );        writer.println( "hex 1501 0 \"\" \"\"" );        writer.println( "hex 1601 0 \"\" \"\"" );        writer.println( "hex 0102 0 \"\" \"\"" );        writer.println( "hex 0202 0 \"woods:1\" \"\"" );        writer.println( "hex 0302 0 \"woods:1\" \"\"" );        writer.println( "hex 0402 0 \"woods:1\" \"\"" );        writer.println( "hex 0502 0 \"\" \"\"" );        writer.println( "hex 0602 0 \"\" \"\"" );        writer.println( "hex 0702 0 \"\" \"\"" );        writer.println( "hex 0802 0 \"\" \"\"" );        writer.println( "hex 0902 0 \"woods:1\" \"\"" );        writer.println( "hex 1002 0 \"rough:1\" \"\"" );        writer.println( "hex 1102 1 \"\" \"\"" );        writer.println( "hex 1202 2 \"\" \"\"" );        writer.println( "hex 1302 1 \"\" \"\"" );        writer.println( "hex 1402 2 \"\" \"\"" );        writer.println( "hex 1502 0 \"\" \"\"" );        writer.println( "hex 1602 0 \"\" \"\"" );        writer.println( "hex 0103 0 \"\" \"\"" );        writer.println( "hex 0203 0 \"\" \"\"" );        writer.println( "hex 0303 0 \"woods:2\" \"\"" );        writer.println( "hex 0403 0 \"woods:1\" \"\"" );        writer.println( "hex 0503 0 \"\" \"\"" );        writer.println( "hex 0603 0 \"\" \"\"" );        writer.println( "hex 0703 0 \"\" \"\"" );        writer.println( "hex 0803 0 \"\" \"\"" );        writer.println( "hex 0903 0 \"\" \"\"" );        writer.println( "hex 1003 0 \"\" \"\"" );        writer.println( "hex 1103 0 \"rough:1\" \"\"" );        writer.println( "hex 1203 0 \"\" \"\"" );        writer.println( "hex 1303 2 \"woods:1\" \"\"" );        writer.println( "hex 1403 3 \"\" \"\"" );        writer.println( "hex 1503 0 \"\" \"\"" );        writer.println( "hex 1603 0 \"\" \"\"" );        writer.println( "hex 0104 0 \"\" \"\"" );        writer.println( "hex 0204 0 \"\" \"\"" );        writer.println( "hex 0304 0 \"\" \"\"" );        writer.println( "hex 0404 0 \"\" \"\"" );        writer.println( "hex 0504 0 \"\" \"\"" );        writer.println( "hex 0604 0 \"\" \"\"" ); //Inferno hex        writer.println( "hex 0704 0 \"\" \"\"" );        writer.println( "hex 0804 0 \"\" \"\"" );        writer.println( "hex 0904 0 \"\" \"\"" );        writer.println( "hex 1004 0 \"\" \"\"" );        writer.println( "hex 1104 0 \"\" \"\"" );        writer.println( "hex 1204 2 \"\" \"\"" );        writer.println( "hex 1304 3 \"\" \"\"" );        writer.println( "hex 1404 0 \"\" \"\"" );        writer.println( "hex 1504 0 \"\" \"\"" );        writer.println( "hex 1604 0 \"\" \"\"" );        writer.println( "hex 0105 0 \"\" \"\"" );

⌨️ 快捷键说明

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