1 19 package org.netbeans.modules.j2ee.persistence.sourcetestsupport; 20 21 import java.io.BufferedReader ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.PrintStream ; 29 import org.netbeans.junit.MockServices; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.Repository; 35 36 41 public abstract class SourceTestSupport extends NbTestCase { 42 43 public SourceTestSupport(String testName) { 44 super(testName); 45 } 46 47 protected void setUp() throws Exception { 48 MockServices.setServices(FakeJavaDataLoaderPool.class, RepositoryImpl.class, ClassPathProviderImpl.class); 49 clearWorkDir(); 50 initTemplates(); 51 } 52 protected void tearDown() throws Exception { 53 super.tearDown(); 54 getSystemFs().reset(); 55 } 56 57 private RepositoryImpl.MultiFileSystemImpl getSystemFs(){ 58 return (RepositoryImpl.MultiFileSystemImpl)Repository.getDefault().getDefaultFileSystem(); 59 } 60 61 private void initTemplates() throws Exception { 62 RepositoryImpl.MultiFileSystemImpl systemFS = getSystemFs(); 63 FileObject interfaceTemplate = systemFS.getRoot().getFileObject("Templates/Classes/Interface.java"); 64 copyStringToFileObject(interfaceTemplate, 65 "package Templates.Classes;" + 66 "public interface Interface {\n" + 67 "}"); 68 FileObject classTemplate = systemFS.getRoot().getFileObject("Templates/Classes/Class.java"); 69 copyStringToFileObject(classTemplate, 70 "package Templates.Classes;" + 71 "public class Class {\n" + 72 "}"); 73 } 74 75 protected void assertFile(FileObject result){ 76 assertFile( getGoldenFile(), FileUtil.toFile(result)); 77 } 78 79 81 protected void print(FileObject fo) throws IOException { 82 print(FileUtil.toFile(fo)); 83 } 84 85 protected void print(File file) throws IOException { 86 BufferedReader in = new BufferedReader (new FileReader (file)); 87 PrintStream out = System.out; 88 String str; 89 while ((str = in.readLine()) != null) { 90 out.println(str); 91 } 92 in.close(); 93 } 94 95 protected FileObject copyStringToFileObject(FileObject fo, String content) throws Exception { 96 OutputStream os = fo.getOutputStream(); 97 try { 98 InputStream is = new ByteArrayInputStream (content.getBytes("UTF-8")); 99 FileUtil.copy(is, os); 100 return fo; 101 } finally { 102 os.close(); 103 } 104 } 105 106 } | Popular Tags |