KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > sourcetestsupport > SourceTestSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.persistence.sourcetestsupport;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.PrintStream JavaDoc;
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 /**
37  * A base class for unit tests.
38  *
39  * @author Erno Mononen
40  */

41 public abstract class SourceTestSupport extends NbTestCase {
42     
43     public SourceTestSupport(String JavaDoc testName) {
44         super(testName);
45     }
46     
47     protected void setUp() throws Exception JavaDoc {
48         MockServices.setServices(FakeJavaDataLoaderPool.class, RepositoryImpl.class, ClassPathProviderImpl.class);
49         clearWorkDir();
50         initTemplates();
51     }
52     protected void tearDown() throws Exception JavaDoc{
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 JavaDoc{
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     // temporary methods for debugging
80

81     protected void print(FileObject fo) throws IOException JavaDoc {
82         print(FileUtil.toFile(fo));
83     }
84     
85     protected void print(File JavaDoc file) throws IOException JavaDoc {
86         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
87         PrintStream JavaDoc out = System.out;
88         String JavaDoc str;
89         while ((str = in.readLine()) != null) {
90             out.println(str);
91         }
92         in.close();
93     }
94     
95     protected FileObject copyStringToFileObject(FileObject fo, String JavaDoc content) throws Exception JavaDoc {
96         OutputStream JavaDoc os = fo.getOutputStream();
97         try {
98             InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(content.getBytes("UTF-8"));
99             FileUtil.copy(is, os);
100             return fo;
101         } finally {
102             os.close();
103         }
104     }
105     
106 }
Popular Tags