001: package visualdebugger;
002:
003: import java.io.File;
004: import java.net.URI;
005: import java.net.URISyntaxException;
006: import java.util.LinkedList;
007:
008: import org.eclipse.core.resources.*;
009: import org.eclipse.core.runtime.CoreException;
010: import org.eclipse.core.runtime.IPath;
011: import org.eclipse.core.runtime.IProgressMonitor;
012: import org.eclipse.core.runtime.Path;
013: import org.eclipse.jdt.core.IClasspathEntry;
014: import org.eclipse.jdt.core.IJavaProject;
015: import org.eclipse.jdt.core.JavaCore;
016: import org.eclipse.jdt.core.JavaModelException;
017: import org.eclipse.jdt.launching.JavaRuntime;
018:
019: import de.uka.ilkd.key.proof.ListOfNode;
020: import de.uka.ilkd.key.unittest.ModelGenerator;
021: import de.uka.ilkd.key.unittest.UnitTestBuilder;
022: import de.uka.ilkd.key.visualdebugger.VisualDebugger;
023:
024: public class VBTBuilder {
025:
026: VisualDebugger vd = VisualDebugger.getVisualDebugger();
027: ListOfNode nodes;
028: private boolean error = false;
029: private String file = null;
030: IProject testGenProject = null;
031: private String fileName;
032: private String path;
033: private boolean projectCreated = false;
034: private int modelGenerator;
035:
036: public VBTBuilder(ListOfNode nodes, int modelGenerator) {
037: this .nodes = nodes;
038: this .modelGenerator = modelGenerator;
039: this .createTestCase();
040: this .findTestProjectInWorkspace();
041:
042: if (testGenProject != null) {
043: try {
044: testGenProject.refreshLocal(IResource.DEPTH_INFINITE,
045: null);
046: } catch (CoreException e) {
047: error = true;
048: e.printStackTrace();
049: }
050: } else {
051: try {
052: projectCreated = true;
053: testGenProject = createTestCaseProject(path);
054: } catch (URISyntaxException e) {
055: error = true;
056: e.printStackTrace();
057: } catch (CoreException e) {
058: error = true;
059: e.printStackTrace();
060: }
061: }
062: VisualDebugger.print("------- Test Cases --------");
063: VisualDebugger.getVisualDebugger().printTestCases();
064:
065: }
066:
067: private void findTestProjectInWorkspace() {
068:
069: final IProject[] projects = ResourcesPlugin.getWorkspace()
070: .getRoot().getProjects();
071:
072: for (int i = 0; i < projects.length; i++) {
073: if (file.startsWith(projects[i].getLocation().toOSString()))
074: testGenProject = projects[i];
075: }
076:
077: VisualDebugger.print("Testgenproject " + testGenProject);
078:
079: }
080:
081: public void createTestCase() {
082: ModelGenerator.decProdForTestGen = this .modelGenerator;
083: UnitTestBuilder testBuilder = new UnitTestBuilder(vd
084: .getMediator().getServices(), vd.getMediator()
085: .getProof());
086:
087: try {
088: file = (testBuilder.createTestForNodes(nodes));
089: } catch (Exception e) {
090: this .error = true;
091: e.printStackTrace();
092: }
093: if (file.lastIndexOf(File.separator) > 0) {
094: int last = file.lastIndexOf(File.separator);
095: fileName = file.substring(last, file.length());
096: path = file.substring(0, last);
097: }
098:
099: }
100:
101: private IProject createTestCaseProject(String testFilePath)
102: throws URISyntaxException, CoreException {
103: String projectName = "TestCases";
104: VisualDebugger.print("Creating new Project in path: "
105: + testFilePath);
106:
107: IWorkspace workspace = ResourcesPlugin.getWorkspace();
108: IProject project2 = workspace.getRoot().getProject(projectName);
109: IProjectDescription projectDescription = null;
110: if (!project2.exists()) {
111: URI projectLocationURI = null;
112: projectLocationURI = new URI("file:" + testFilePath);
113: projectDescription = ResourcesPlugin.getWorkspace()
114: .newProjectDescription(projectName);
115:
116: if (projectLocationURI != null) {
117: projectDescription.setLocationURI(new java.net.URI(
118: projectLocationURI.toString()));
119: }
120: project2.create(projectDescription, null);
121: }
122:
123: if (!project2.isOpen()) {
124: project2.open(null);
125:
126: }
127: IJavaProject javaProject = JavaCore.create(project2);
128:
129: addNatureToProject(project2, JavaCore.NATURE_ID, null);
130:
131: javaProject.setRawClasspath(new IClasspathEntry[0], null);
132: IPath projectPath = javaProject.getProject().getFullPath();
133: VisualDebugger.print("ProjectPath " + projectPath);
134: try {
135: addContainerEntry(javaProject, new Path(
136: JavaRuntime.JRE_CONTAINER));
137: IClasspathEntry cpe;
138: Path[] dir = getDirectories(new File(VisualDebugger.tempDir));
139:
140: for (int i = 0; i < dir.length; i++) {
141: String s = dir[i].toOSString();
142: //System.out.println("S "+ s.substring(1, s.length()));
143: cpe = JavaCore.newSourceEntry(projectPath
144: .append(new Path(s.substring(1, s.length()))));
145: addToClasspath(javaProject, cpe);
146: }
147:
148: // IPath[] ps = new Path[1];
149: // ps[0] = (new Path("home/**"));
150:
151: IPath[] ps = getExcludingDirectories(new File(testFilePath));
152: cpe = JavaCore.newSourceEntry(projectPath.append(new Path(
153: "")), ps);
154: addToClasspath(javaProject, cpe);
155: addContainerEntry(javaProject, new Path(
156: "org.eclipse.jdt.junit.JUNIT_CONTAINER/3.8.1"));
157: } catch (JavaModelException e1) {
158: // TODO Auto-generated catch block
159: e1.printStackTrace();
160: }
161: IProject pro = javaProject.getProject();
162:
163: return pro;
164:
165: // <?xml version="1.0" encoding="UTF-8"?>
166: // <classpath>
167: // <classpathentry excluding="home/baum/tmp/visualdebugger/NaturalNumberWrapper/" kind="src" path=""/>
168: // <classpathentry kind="src" path="home/baum/tmp/visualdebugger/NaturalNumberWrapper"/>
169: // <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
170: // <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3.8.1"/>
171: // <classpathentry kind="output" path="bin"/>
172: // </classpath>
173:
174: }
175:
176: private static Path[] getExcludingDirectories(File path) {
177: LinkedList dir = new LinkedList();
178: String[] files = path.list();
179:
180: for (int i = 0; i < files.length; i++) {
181: //System.out.println("File"+files[i]);
182: if (new File(path.toString() + File.separator + files[i])
183: .isDirectory()) {
184: dir.addLast(new Path(files[i] + File.separator + "**"));
185: //System.out.println("dir "+files[i]);
186: }
187:
188: }
189: Path[] result = new Path[dir.size()];
190: result = (Path[]) dir.toArray(new Path[dir.size()]);
191:
192: return result;
193: }
194:
195: private static Path[] getDirectories(File path) {
196: LinkedList dir = new LinkedList();
197: String[] files = path.list();
198:
199: for (int i = 0; i < files.length; i++) {
200: if (new File(path.toString() + File.separator + files[i])
201: .isDirectory()) {
202: dir.addLast(new Path(path.toString() + File.separator
203: + files[i]));
204:
205: }
206:
207: }
208: Path[] result = new Path[dir.size()];
209: result = (Path[]) dir.toArray(new Path[dir.size()]);
210:
211: return result;
212: }
213:
214: private static void addToClasspath(IJavaProject jproject,
215: IClasspathEntry cpe) throws JavaModelException {
216: IClasspathEntry[] oldEntries = jproject.getRawClasspath();
217: for (int i = 0; i < oldEntries.length; i++) {
218: if (oldEntries[i].equals(cpe)) {
219: return;
220: }
221: }
222: int nEntries = oldEntries.length;
223: IClasspathEntry[] newEntries = new IClasspathEntry[nEntries + 1];
224: System.arraycopy(oldEntries, 0, newEntries, 0, nEntries);
225: newEntries[nEntries] = cpe;
226: jproject.setRawClasspath(newEntries, null);
227: }
228:
229: public static void addContainerEntry(IJavaProject project,
230: IPath container) throws JavaModelException {
231: IClasspathEntry cpe = JavaCore.newContainerEntry(container,
232: false);
233: addToClasspath(project, cpe);
234: }
235:
236: private static void addNatureToProject(IProject proj,
237: String natureId, IProgressMonitor monitor)
238: throws CoreException {
239: IProjectDescription description = proj.getDescription();
240: String[] prevNatures = description.getNatureIds();
241: String[] newNatures = new String[prevNatures.length + 1];
242: System.arraycopy(prevNatures, 0, newNatures, 0,
243: prevNatures.length);
244: newNatures[prevNatures.length] = natureId;
245: description.setNatureIds(newNatures);
246: proj.setDescription(description, monitor);
247: }
248:
249: public boolean succesful() {
250: return !this .error;
251: }
252:
253: public boolean newProjectCreated() {
254: return projectCreated;
255: }
256:
257: public IProject getTestGenProject() {
258: return testGenProject;
259: }
260:
261: public String getFileName() {
262: return fileName;
263: }
264:
265: }
|