1 package org.apache.bcel.verifier.tests; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.OutputStream; 7 import java.net.URISyntaxException; 8 9 /* 10 * Licensed to the Apache Software Foundation (ASF) under one or more 11 * contributor license agreements. See the NOTICE file distributed with 12 * this work for additional information regarding copyright ownership. 13 * The ASF licenses this file to You under the Apache License, Version 2.0 14 * (the "License"); you may not use this file except in compliance with 15 * the License. You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, software 20 * distributed under the License is distributed on an "AS IS" BASIS, 21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 * See the License for the specific language governing permissions and 23 * limitations under the License. 24 * 25 */ 26 public abstract class TestCreator { 27 28 // Common package base name for generated test classes 29 protected static final String TEST_PACKAGE = TestCreator.class.getPackage().getName(); 30 create()31 public void create() throws IOException { 32 final File classFile = new File(getPackageFolder(), getClassName()); 33 try (FileOutputStream out = new FileOutputStream(classFile)) { 34 create(out); 35 } 36 } 37 getClassName()38 private String getClassName() { 39 final String name = getClass().getName(); 40 return name.substring(name.lastIndexOf('.')+1).replace("Creator", ".class"); 41 } 42 getPackageFolder()43 private File getPackageFolder() throws IOException { 44 return new File(getClassesFolder(), getPackageName()); 45 } 46 getPackageName()47 protected String getPackageName() { 48 return getClass().getPackage().getName().replace('.', '/'); 49 } 50 getClassesFolder()51 private File getClassesFolder() throws IOException { 52 try { 53 return new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); 54 } catch (final URISyntaxException e) { 55 throw new IOException(e); 56 } 57 } 58 create(OutputStream out)59 public abstract void create(OutputStream out) throws IOException; 60 } 61