class ZipExporterTest

Public Instance Methods

setup() click to toggle source
# File ../../../../../test/test_zip_exporter.rb, line 10
  def setup
    @tmpdir = Dir.mktmpdir
    @epubdir = "#{@tmpdir}/epubdir"
    FileUtils.mkdir_p("#{@epubdir}/META-INF")
    FileUtils.mkdir_p("#{@epubdir}/OEBPS")
    File.write("#{@epubdir}/mimetype", "application/epub+zip")

    container_xml = <<-EOB
    <?xml version="1.0" encoding="UTF-8"?>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
  <rootfiles>
    <rootfile full-path="OEBPS/book.opf" media-type="application/oebps-package+xml" />
  </rootfiles>
</container>
    EOB

    book_opf = <<-EOB
<?xml version="1.0"?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookID" version="2.0">
    <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
        <dc:title>sample epub</dc:title>
        <dc:creator opf:role="aut">AUTHOR</dc:creator>
        <dc:language>ja</dc:language>
        <dc:identifier id="BookID" opf:scheme="URL">http://example.com/epub/sample/sample1.epub</dc:identifier>
    </metadata>
    <manifest>
        <item id="ch1.xhtml" href="OEBPS/ch1.xhtml" media-type="application/xhtml+xml"/>
    </manifest>
    <spine toc="ncx">
        <itemref idref="ch1.xhtml"/>
    </spine>
    <guide>
        <reference type="cover" title="Cover Page" href="OEPBS/ch1.xhtml"/>
    </guide>
</package>
    EOB

    ch1_xhtml = <<-EOB
<html>
  <head>
    <title>test</title>
  </head>
  <body>
  <p>hello, world!</p>
  </body>
</html>
    EOB

    File.write("#{@epubdir}/META-INF/container.xml", container_xml)
    File.write("#{@epubdir}/OEBPS/book.opf", book_opf)
    File.write("#{@epubdir}/OEBPS/ch1.xhtml", ch1_xhtml)
  end
teardown() click to toggle source
# File ../../../../../test/test_zip_exporter.rb, line 108
def teardown
  FileUtils.remove_entry_secure(@tmpdir)
end
test_export_rubyzip() click to toggle source
# File ../../../../../test/test_zip_exporter.rb, line 87
def test_export_rubyzip
  if !defined?(Zip)
    ## skip test
    return
  end
  params = {"epubmaker"=>{}}
  epubfile = File.join(@tmpdir, "test.epub")
  exporter = ZipExporter.new(@epubdir, params)
  exporter.export_zip_rubyzip(epubfile)
  assert_true(File.exist?(epubfile))

  File.open(epubfile) do |f|
    ::Zip::InputStream.open(f) do |fzip|
      ## get first entry
      entry = fzip.get_next_entry
      assert_equal "mimetype", entry.name
      assert_equal "application/epub+zip", fzip.read
    end
  end
end
test_export_zipcmd() click to toggle source
# File ../../../../../test/test_zip_exporter.rb, line 63
def test_export_zipcmd
  if Gem.win_platform?
    ## skip this test
    return
  end

  params = {"epubmaker"=>{}}
  epubfile = File.join(@tmpdir, "test.epub")
  exporter = ZipExporter.new(@epubdir, params)
  exporter.export_zip_extcmd(epubfile)
  assert_true(File.exist?(epubfile))

  if defined?(Zip)
    File.open(epubfile) do |f|
      ::Zip::InputStream.open(f) do |fzip|
        ## get first entry
        entry = fzip.get_next_entry
        assert_equal "mimetype", entry.name
        assert_equal "application/epub+zip", fzip.read
      end
    end
  end
end