001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestFileHelper.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.util;
030:
031: import java.io.File;
032:
033: /**
034: *
035: * @author Sun Microsystems, Inc.
036: */
037:
038: public class TestFileHelper extends junit.framework.TestCase {
039: private String mConfigDir = null;
040: private String GOOD_COMP = "component.zip";
041: private String MODIFIED_GOOD_COMP = "component-modified.zip";
042:
043: public TestFileHelper(String testName) {
044: super (testName);
045: String srcroot = System.getProperty("junit.srcroot");
046: String manage = "/runtime/manage"; // open-esb build
047: mConfigDir = srcroot + manage + "/bld/test-classes/testdata/";
048:
049: java.io.File f = new java.io.File(srcroot + manage);
050: if (!f.exists()) {
051: manage = "/shasta/manage"; // mainline/whitney build
052: mConfigDir = srcroot + manage + "/bld/regress/testdata/";
053: }
054: }
055:
056: /**
057: * Test comparing a file with itself, should be identical
058: */
059: public void testAreFilesIdentical() throws Exception {
060: File testComp = new File(mConfigDir, GOOD_COMP);
061: assertTrue(FileHelper.areFilesIdentical(testComp, testComp));
062: }
063:
064: /**
065: * Test comparing with a file with a slight change to jbi.xml, should not be
066: * Identical
067: */
068: public void testAreFilesIdenticalNegative() throws Exception {
069: File testComp1 = new File(mConfigDir, GOOD_COMP);
070: File testComp2 = new File(mConfigDir, MODIFIED_GOOD_COMP);
071: assertFalse(FileHelper.areFilesIdentical(testComp1, testComp2));
072: }
073:
074: /**
075: * This method is used to test copy method in FileHelper class
076: * @throws Exception if the test could not be executed
077: */
078: public void testCopy() throws Exception {
079: String testClasses = System.getProperty("junit.srcroot")
080: + "/runtime/manage/bld/test-classes/com/sun/jbi/management/internal/support";
081: String dest = System.getProperty("junit.srcroot")
082: + "/runtime/manage/bld/test-classes/filehelper_test/";
083: File srcDir = new File(testClasses);
084: File destDir = new File(dest);
085:
086: String[] srcFiles = srcDir.list();
087:
088: FileHelper.copy(testClasses, dest);
089:
090: //verify that the dir has been created
091: assertTrue(destDir.exists());
092:
093: String[] destFiles = destDir.list();
094:
095: //verify that all the files have been copied
096: assertEquals(srcFiles.length, destFiles.length);
097:
098: //verify that all the files are copied correctly
099: for (int i = 0; i < srcFiles.length; i++) {
100: assertTrue(FileHelper.areFilesIdentical(new File(
101: testClasses, srcFiles[i]), new File(dest,
102: destFiles[i])));
103: }
104: assertTrue(FileHelper.cleanDirectory(destDir));
105: }
106:
107: /**
108: * Test reading the file into a String.
109: */
110: public void testReadFile() throws Exception {
111: File testXmlFile = new File(mConfigDir, "DOMUtil-ns-good.xml");
112:
113: String fileStr = FileHelper.readFile(testXmlFile);
114:
115: assertTrue(fileStr.indexOf("/good-namespace>") != -1);
116: }
117:
118: }
|