View Javadoc

    1   /* CandidateURITest.java
    2    *
    3    * $Id: CandidateURITest.java 3771 2005-08-29 21:52:36Z stack-sf $
    4    *
    5    * Created Jun 23, 2005
    6    *
    7    * Copyright (C) 2005 Internet Archive.
    8    *
    9    * This file is part of the Heritrix web crawler (crawler.archive.org).
    10   *
    11   * Heritrix is free software; you can redistribute it and/or modify
    12   * it under the terms of the GNU Lesser Public License as published by
    13   * the Free Software Foundation; either version 2.1 of the License, or
    14   * any later version.
    15   *
    16   * Heritrix is distributed in the hope that it will be useful,
    17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19   * GNU Lesser Public License for more details.
    20   *
    21   * You should have received a copy of the GNU Lesser Public License
    22   * along with Heritrix; if not, write to the Free Software
    23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24   */
    25  package org.archive.crawler.datamodel;
    26
    27  import java.io.ByteArrayInputStream;
    28  import java.io.ByteArrayOutputStream;
    29  import java.io.IOException;
    30  import java.io.ObjectInputStream;
    31  import java.io.ObjectOutputStream;
    32
    33  import junit.framework.TestCase;
    34
    35  import org.archive.net.UURIFactory;
    36
    37  /***
    38   * Test  CandidateURI serialization.
    39   * @author stack
    40   */
    41  public class CandidateURITest extends TestCase {
    42      public void testSerialization()
    43      throws IOException, ClassNotFoundException {
    44          doOneSerialization("http://www.archive.org/");
    45          doOneSerialization("http://www.archive.org/a?" +
    46              "sch=%2E%2F%3Faction%3Dsearch");
    47      }
    48
    49      private void doOneSerialization(final String urlStr)
    50      throws IOException, ClassNotFoundException {
    51          CandidateURI cauri =
    52              new CandidateURI(UURIFactory.getInstance(urlStr));
    53          cauri = serialize(cauri);
    54          assertEquals(urlStr + " doesn't serialize", urlStr,
    55              cauri.getUURI().toString());
    56      }
    57
    58      private CandidateURI serialize(CandidateURI cauri)
    59      throws IOException, ClassNotFoundException {
    60          ByteArrayOutputStream baos = new ByteArrayOutputStream();
    61          ObjectOutputStream oos = new ObjectOutputStream(baos);
    62          oos.writeObject(cauri);
    63          oos.flush();
    64          oos.close();
    65          ByteArrayInputStream bais =
    66              new ByteArrayInputStream(baos.toByteArray());
    67          return (CandidateURI)(new ObjectInputStream(bais)).readObject();
    68      }
    69  }