1 package org.apache.maven.plugin.eclipse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.DataOutputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.net.MalformedURLException;
30 import java.util.Properties;
31
32 import org.apache.commons.io.FileUtils;
33 import org.apache.maven.plugin.eclipse.reader.ReadWorkspaceLocations;
34 import org.codehaus.plexus.util.IOUtil;
35 import org.eclipse.core.internal.localstore.ILocalStoreConstants;
36
37 public class TempEclipseWorkspace
38 {
39 private static int workspaceNumber = 0;
40
41
42
43
44
45 public static TempEclipseWorkspace getFixtureEclipseWorkspaceWithRad7Default14()
46 throws Exception
47 {
48 return new TempEclipseWorkspace( "rad7WithDefault14", new String[] { "direct-compile" } );
49 }
50
51
52
53
54
55 public static TempEclipseWorkspace getFixtureEclipseWithDefault15()
56 throws Exception
57 {
58 return new TempEclipseWorkspace( "eclipseWithDefault15", new String[] { "direct-compile" } );
59 }
60
61
62
63
64
65 public static TempEclipseWorkspace getFixtureEclipseWithDefault13()
66 throws Exception
67 {
68 return new TempEclipseWorkspace( "eclipseWithDefault13", new String[] { "direct-compile" } );
69 }
70
71
72
73
74
75 public static TempEclipseWorkspace getFixtureEclipseDynamicWorkspace()
76 throws Exception
77 {
78 return new TempEclipseWorkspace( "dynamicWorkspace", new String[] { "project-A/module-A1", "../project-O" } );
79 }
80
81 public File workspaceLocation;
82
83 public TempEclipseWorkspace( String testWorkspaceName, String[] projectsToLink )
84 throws Exception
85 {
86
87 File tempWorkspace = new File( "target/tmp-workspace" + workspaceNumber++ );
88 FileUtils.deleteDirectory( tempWorkspace );
89 FileUtils.copyDirectoryToDirectory( new File( "src/test/resources/eclipse" ), tempWorkspace );
90
91 File eclipseLocation = new File( tempWorkspace, "eclipse" ).getCanonicalFile();
92
93 File jdkLocation = new File( eclipseLocation, "dummyJDK" );
94
95 workspaceLocation = new File( eclipseLocation, testWorkspaceName + "/workspace" ).getCanonicalFile();
96
97 File propertyfile =
98 new File( workspaceLocation,
99 ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs" );
100
101 preparePropertyFile( jdkLocation, propertyfile );
102
103 if ( projectsToLink != null && projectsToLink.length != 0 )
104 {
105 for (String projectToLink : projectsToLink) {
106 writeLocationFile(projectToLink);
107 }
108 }
109
110 }
111
112
113
114
115
116
117
118
119
120
121 private void writeLocationFile( String projectToLink )
122 throws IOException
123 {
124 File projectToLinkAsRelativeFile = new File( projectToLink );
125
126 File projectWorkspaceDirectory =
127 new File( workspaceLocation, projectToLinkAsRelativeFile.getPath() ).getCanonicalFile();
128 String uriToProjectWorkspaceDirectory = "URI//" + projectWorkspaceDirectory.toURI().toURL().toString();
129
130 File metaDataPlugins =
131 new File( workspaceLocation, ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RESOURCES_PROJECTS );
132 File projectMetaDataDirectory = new File( metaDataPlugins, projectToLinkAsRelativeFile.getName() );
133 File locationFile = new File( projectMetaDataDirectory, ReadWorkspaceLocations.BINARY_LOCATION_FILE );
134
135 DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream( locationFile ) );
136
137 dataOutputStream.write( ILocalStoreConstants.BEGIN_CHUNK );
138 dataOutputStream.writeUTF( uriToProjectWorkspaceDirectory );
139 dataOutputStream.write( ILocalStoreConstants.END_CHUNK );
140 IOUtil.close( dataOutputStream );
141 }
142
143 private static void preparePropertyFile( File jdkLocation, File propertyfile )
144 throws IOException {
145 Properties properties = new Properties();
146 properties.load( new FileInputStream( propertyfile ) );
147 properties.setProperty(
148 "org.eclipse.jdt.launching.PREF_VM_XML",
149 properties.getProperty( "org.eclipse.jdt.launching.PREF_VM_XML" ).replaceAll(
150 "__replace_with_test_dir__",
151 jdkLocation.getCanonicalPath().replace(
152 '\\',
153 '/' ) ) );
154 properties.store( new FileOutputStream( propertyfile ), "" );
155 }
156
157 }