/*******************************************************************************
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package test.apache.kato.hprof.image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import javax.tools.diagnostics.image.Image;
import javax.tools.diagnostics.image.ImageAddressSpace;
import javax.tools.diagnostics.image.ImageProcess;
import javax.tools.diagnostics.runtime.java.JavaHeap;
import javax.tools.diagnostics.runtime.java.JavaRuntime;
import junit.framework.TestCase;
import org.apache.kato.hprof.HProfView;
import org.apache.kato.hprof.datalayer.HProfFactory;
import org.apache.kato.hprof.datalayer.HProfFile;
import org.apache.kato.hprof.image.ImageFactoryImpl;
public abstract class AbstractHProfTestCase extends TestCase {
public File getFile(String fileName) {
URL u=getClass().getResource(fileName);
String path=u.getPath();
path=java.net.URLDecoder.decode(path);
File realFile=new File(path);
return realFile;
}
protected File getEmptyHProfFile() {
File realFile = getFile("/data/empty.hprof");
return realFile;
}
protected File getHeaderOnlyHProfFile() {
File realFile = getFile("/data/headeronly.hprof");
return realFile;
}
protected File getPopulatedHProfFile() {
File realFile = getFile("/data/populated.hprof");
return realFile;
}
protected File getMinimalHProfFile() {
File realFile = getFile("/data/minimal.hprof");
return realFile;
}
public HProfView getMinimalHProfView() throws IOException {
File m=getMinimalHProfFile();
HProfFile hprofFile=HProfFactory.createReader(m);
hprofFile.open();
return new HProfView(hprofFile);
}
public Image getMinimalImage() throws IOException {
ImageFactoryImpl factory=new ImageFactoryImpl();
return factory.getImage(getMinimalHProfFile());
}
public Image getPopulatedImage() throws IOException {
ImageFactoryImpl factory=new ImageFactoryImpl();
return factory.getImage(getPopulatedHProfFile());
}
protected ImageAddressSpace getFirstAddressSpace() throws IOException {
Image minimal=getMinimalImage();
ImageAddressSpace space=(ImageAddressSpace)minimal.getAddressSpaces().get(0);
return space;
}
protected ImageProcess getCurrentProcess() throws IOException {
ImageAddressSpace space=getFirstAddressSpace();
return space.getCurrentProcess();
}
protected JavaRuntime getJavaRuntime() throws IOException {
return (JavaRuntime) getCurrentProcess().getRuntimes().get(0);
}
protected JavaRuntime getPopulatedJavaRuntime() throws IOException {
return (JavaRuntime) getCurrentProcess().getRuntimes().get(0);
}
protected JavaHeap getPopulatedJavaHeap() throws IOException {
Image populated=getPopulatedImage();
ImageAddressSpace space=(ImageAddressSpace)populated.getAddressSpaces().get(0);
ImageProcess process=space.getCurrentProcess();
JavaRuntime rt=(JavaRuntime) process.getRuntimes().get(0);
return (JavaHeap) rt.getHeaps().get(0);
}
}