001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.BuildFileTest;
022: import org.apache.tools.ant.util.FileUtils;
023: import org.apache.tools.bzip2.CBZip2InputStream;
024:
025: import java.io.BufferedInputStream;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.InputStream;
029: import java.io.IOException;
030:
031: /**
032: */
033: public class BZip2Test extends BuildFileTest {
034:
035: /** Utilities used for file operations */
036: private static final FileUtils FILE_UTILS = FileUtils
037: .getFileUtils();
038:
039: public BZip2Test(String name) {
040: super (name);
041: }
042:
043: public void setUp() {
044: configureProject("src/etc/testcases/taskdefs/bzip2.xml");
045: executeTarget("prepare");
046: }
047:
048: public void tearDown() {
049: executeTarget("cleanup");
050: }
051:
052: public void testRealTest() throws IOException {
053: executeTarget("realTest");
054:
055: // doesn't work: Depending on the compression engine used,
056: // compressed bytes may differ. False errors would be
057: // reported.
058: // assertTrue("File content mismatch",
059: // FILE_UTILS.contentEquals(project.resolveFile("expected/asf-logo-huge.tar.bz2"),
060: // project.resolveFile("asf-logo-huge.tar.bz2")));
061:
062: // We have to compare the decompressed content instead:
063:
064: File originalFile = project
065: .resolveFile("expected/asf-logo-huge.tar.bz2");
066: File actualFile = project.resolveFile("asf-logo-huge.tar.bz2");
067:
068: InputStream originalIn = new BufferedInputStream(
069: new FileInputStream(originalFile));
070: assertEquals((byte) 'B', originalIn.read());
071: assertEquals((byte) 'Z', originalIn.read());
072:
073: InputStream actualIn = new BufferedInputStream(
074: new FileInputStream(actualFile));
075: assertEquals((byte) 'B', actualIn.read());
076: assertEquals((byte) 'Z', actualIn.read());
077:
078: originalIn = new CBZip2InputStream(originalIn);
079: actualIn = new CBZip2InputStream(actualIn);
080:
081: while (true) {
082: int expected = originalIn.read();
083: int actual = actualIn.read();
084: if (expected >= 0) {
085: if (expected != actual) {
086: fail("File content mismatch");
087: }
088: } else {
089: if (actual >= 0) {
090: fail("File content mismatch");
091: }
092: break;
093: }
094: }
095:
096: originalIn.close();
097: actualIn.close();
098: }
099:
100: public void testResource() {
101: executeTarget("realTestWithResource");
102: }
103:
104: public void testDateCheck() {
105: executeTarget("testDateCheck");
106: String log = getLog();
107: assertTrue(
108: "Expecting message ending with 'asf-logo.gif.bz2 is up to date.' but got '"
109: + log + "'", log
110: .endsWith("asf-logo.gif.bz2 is up to date."));
111: }
112:
113: }
|