Skip to content

Commit

Permalink
JBIDE-14990 : check existing source validity
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <fbricon@gmail.com>
  • Loading branch information
fbricon committed Jun 25, 2013
1 parent 80e1bd8 commit dcd07cb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 19 deletions.
Expand Up @@ -11,7 +11,12 @@
package org.jboss.tools.maven.sourcelookup.ui.actions;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.codehaus.plexus.util.IOUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
Expand All @@ -24,8 +29,10 @@
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
import org.eclipse.jface.action.IAction;
Expand Down Expand Up @@ -71,19 +78,27 @@ public void setActiveEditor(IAction action, IEditorPart targetEditor) {
}
IClassFileEditorInput input = (IClassFileEditorInput) targetEditor.getEditorInput();
IJavaElement element = input.getClassFile();

String className = element.getElementName();
boolean isMavenProject = isMavenProject(element.getJavaProject());
if (isMavenProject) {
return;
}

String packagePath = null;
while (element.getParent() != null) {
element = element.getParent();
if (element instanceof IPackageFragment) {
packagePath = element.getElementName().replace(".", "/")+"/"+className.replace(".class", ".java");
} else
if (element instanceof IPackageFragmentRoot) {
final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;

IPath attachmentPath = fragment.getSourceAttachmentPath();
if (attachmentPath != null && !attachmentPath.isEmpty() && attachmentPath.toFile().exists()) {
if ((attachmentPath == null || attachmentPath.isEmpty()) && isMavenProject) {
//Let m2e do its stuff for missing attachments only
break;
}

if (attachmentPath != null && !attachmentPath.isEmpty()) {
File attachementSource = attachmentPath.toFile();
if (attachementSource.exists() && hasSource(attachementSource, packagePath)) {
break;
}
}
if (fragment.isArchive()) {
IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath());
Expand Down Expand Up @@ -116,13 +131,39 @@ public void done(IJobChangeEvent event) {
break;
}
}
element = element.getParent();
}
} catch (Exception e) {
SourceLookupUIActivator.log(e);
}
}
}

private boolean hasSource(File attachementSource, String className) {
if (className == null) {
return false;
}
if (attachementSource.isDirectory()) {
return new File(attachementSource, className).exists();
}
//we assume it's a jar :
ZipFile jar = null;
try {
jar = new ZipFile(attachementSource);
ZipEntry entry = jar.getEntry(className);//$NON-NLS-1$
return entry != null;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (jar != null) jar.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}

private void postIdentification(final IPackageFragmentRoot fragment,
File file, final ArtifactKey artifact) {
if (artifact != null) {
Expand Down
Expand Up @@ -17,7 +17,7 @@

@Suite.SuiteClasses({
SourceLookupTest.class,
OpenBinaryFile.class
OpenBinaryFileTest.class
})

@RunWith(Suite.class)
Expand Down
Expand Up @@ -70,7 +70,7 @@
import org.junit.Test;
import org.osgi.framework.Bundle;

public class OpenBinaryFile {
public class OpenBinaryFileTest {

private static final String ORG_APACHE_COMMONS_LANG_STRING_UTILS = "org.apache.commons.lang.StringUtils";

Expand All @@ -91,7 +91,7 @@ public String queryOverwrite(String pathString) {
@BeforeClass
public static void init() throws Exception {
File file = getProjectFile();
project = importTestProject(file);
project = importTestProject(PROJECT_NAME, file);
project.build(IncrementalProjectBuilder.FULL_BUILD, null);
JobUtils.waitForIdle();
IEclipsePreferences preferences = SourceLookupActivator
Expand Down Expand Up @@ -140,7 +140,7 @@ public void run() {

@Test
public void testInvalidAttachment() throws Exception {
invalidateAttachment();
invalidateAttachment("/NON-EXISTING");
IDocument document = getDocument();
String text = document.get();
assertEquals(268703, text.length());
Expand All @@ -149,7 +149,7 @@ public void testInvalidAttachment() throws Exception {

@Test
public void testPreference() throws Exception {
invalidateAttachment();
invalidateAttachment("/NON-EXISTING");
String oldValue = SourceLookupActivator.getDefault().getAutoAddSourceAttachment();
IEclipsePreferences preferences = SourceLookupActivator
.getPreferences();
Expand All @@ -167,8 +167,18 @@ public void testPreference() throws Exception {
closeAllEditors(false);
}
}

public void invalidateAttachment() throws JavaModelException {

@Test
public void testJBIDE14990_InvalidExistingAttachment() throws Exception {
invalidateAttachment("/" + PROJECT_NAME +"/lib/commons-lang-2.6.jar");
IDocument document = getDocument();
String text = document.get();
assertEquals(268703, text.length());
closeAllEditors(false);
}


public void invalidateAttachment(String path) throws JavaModelException {
IJavaProject javaProject = JavaCore.create(project);
assertTrue(javaProject != null);
IType type = javaProject.findType(ORG_APACHE_COMMONS_LANG_STRING_UTILS);
Expand All @@ -179,12 +189,12 @@ public void invalidateAttachment() throws JavaModelException {
element = element.getParent();
if (element instanceof IPackageFragmentRoot) {
final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element;
SourceLookupUtil.attachSource(fragment, new Path("/NON-EXISTING"));
SourceLookupUtil.attachSource(fragment, new Path(path));
break;
}
}
}

private static void closeAllEditors(boolean save) {
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
for (IWorkbenchWindow window:windows) {
Expand All @@ -195,10 +205,9 @@ private static void closeAllEditors(boolean save) {
}
}

public static IProject importTestProject(File file) throws CoreException,
public static IProject importTestProject(String projectName, File file) throws CoreException,
ZipException, IOException, InvocationTargetException,
InterruptedException {
final String projectName = PROJECT_NAME;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject project = workspace.getRoot().getProject(projectName);
createProject(project, new NullProgressMonitor());
Expand Down Expand Up @@ -241,8 +250,13 @@ public static IProject importTestProject(File file) throws CoreException,

public static File getProjectFile() throws IOException,
FileNotFoundException {
return getProjectFile("projects/test13848.zip");
}

public static File getProjectFile(String projectPath) throws IOException,
FileNotFoundException {
Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
URL url = bundle.getEntry("projects/test13848.zip");
URL url = bundle.getEntry(projectPath);
InputStream in = null;
OutputStream out = null;
File file = null;
Expand Down

0 comments on commit dcd07cb

Please sign in to comment.