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: * @(#)TestArchiveDownload.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.repository;
030:
031: import com.sun.jbi.management.system.ScaffoldEnvironmentContext;
032: import com.sun.jbi.management.system.ManagementContext;
033: import com.sun.jbi.management.system.Util;
034:
035: import java.io.File;
036: import java.io.FileOutputStream;
037:
038: public class TestArchiveDownload extends junit.framework.TestCase {
039: private static final String JBI_ROOT_PATH = "/bld";
040:
041: private static final String TEMP_FILE = "/tmp/download.zip";
042:
043: private static final String COMPONENT_ARCHIVE_PATH = "/testdata/component.zip";
044:
045: private static final String COMPONENT_ARCHIVE_NAME = "SunSequencingEngine";
046:
047: private ManagementContext mCtx;
048: private String mJbiRoot, mTestClassesRoot;
049: private ArchiveDownload mDownloader;
050:
051: public TestArchiveDownload(String aTestName) throws Exception {
052: super (aTestName);
053:
054: String srcroot = System.getProperty("junit.srcroot");
055: String manage = "/runtime/manage"; // open-esb build
056: String blddir = "/bld/test-classes"; // open-esb build
057:
058: java.io.File f = new java.io.File(srcroot + manage);
059: if (!f.exists()) {
060: manage = "/shasta/manage"; // mainline/whitney build
061: blddir = "/bld/regress"; // mainline/whitney build
062: }
063:
064: mJbiRoot = srcroot + manage + JBI_ROOT_PATH;
065: mTestClassesRoot = srcroot + manage + blddir;
066:
067: //this is a bad dependency on ManagementContext.
068: //This should be fixed when validation logic is removed from Archive
069: mCtx = Util.createManagementContext();
070:
071: mDownloader = new ArchiveDownload(mCtx);
072: }
073:
074: public void setUp() throws Exception {
075: super .setUp();
076:
077: mCtx.getRepository().purge();
078: }
079:
080: public void tearDown() throws Exception {
081: }
082:
083: public void testDownloadArchive() throws Exception {
084: Archive archive;
085: Object downId;
086: FileOutputStream fos;
087: byte[] buf;
088:
089: // add the archive we are about to fetch
090: archive = mCtx.getRepository().addArchive(
091: ArchiveType.COMPONENT,
092: mTestClassesRoot + COMPONENT_ARCHIVE_PATH);
093:
094: // start the download session
095: downId = mDownloader.initiateDownload(archive.getPath());
096:
097: // grab the bytes
098: fos = new FileOutputStream(mJbiRoot + TEMP_FILE);
099: buf = mDownloader.downloadBytes(downId, 4096);
100: while (buf.length > 0) {
101: fos.write(buf);
102: buf = mDownloader.downloadBytes(downId, 4096);
103: }
104:
105: mDownloader.terminateDownload(downId);
106:
107: fos.flush();
108: fos.close();
109:
110: File tmpFile = new File(mJbiRoot, TEMP_FILE);
111: assertTrue(tmpFile.exists());
112: assertTrue(tmpFile.length() > 0);
113: }
114: }
|