Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*******************************************************************************
* Copyright (c) 2015 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.batch.ui.itest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.jboss.tools.batch.core.IBatchArtifact;
import org.jboss.tools.batch.core.IBatchProperty;
import org.jboss.tools.batch.internal.core.impl.BatchProject;
import org.jboss.tools.batch.internal.core.impl.BatchProjectFactory;
import org.jboss.tools.batch.internal.core.impl.BatchUtil.NodePathTextSourceReference;
import org.jboss.tools.batch.ui.hyperlink.BatchHyperlinkDetector;
import org.jboss.tools.batch.ui.hyperlink.BatchHyperlinkMessages;
import org.jboss.tools.batch.ui.hyperlink.BatchPropertyDialog;
import org.jboss.tools.batch.ui.hyperlink.BatchPropertyHyperlink;
import org.jboss.tools.batch.ui.hyperlink.BatchPropertyHyperlinkDetector;
import org.jboss.tools.common.model.util.EclipseResourceUtil;
import org.jboss.tools.common.text.ext.hyperlink.OpenJavaElementHyperlink;
import org.jboss.tools.common.text.ext.hyperlink.xml.XMLJumpToHyperlink;
import org.jboss.tools.common.util.EclipseJavaUtil;
import org.jboss.tools.jst.jsp.test.openon.HyperlinkTestUtil;
import org.jboss.tools.jst.jsp.test.openon.HyperlinkTestUtil.TestHyperlink;
import org.jboss.tools.jst.jsp.test.openon.HyperlinkTestUtil.TestRegion;

public class BatchPropertyHyperlinkDetectorTest extends TestCase {
private static final String PROJECT_NAME = "BatchTestProject"; //$NON-NLS-1$

public IProject project = null;

protected void setUp() {
project = ResourcesPlugin.getWorkspace().getRoot().getProject(
PROJECT_NAME);
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false);
}

protected void tearDown() {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false);
}

public BatchPropertyHyperlinkDetectorTest() {
super("BatchHyperlinkDetector Test"); //$NON-NLS-1$
}

public void testDetector() throws Exception{
checkHyperlinkDetector("src/batch/SearchableBatchlet.java"); //$NON-NLS-1$
}

public void testBatchPropertyDialog() throws CoreException {
String path = "src/batch/SearchableBatchlet.java";
IFile file = project.getFile(path);
assertTrue(file.exists());

BatchProject batchProject = (BatchProject) BatchProjectFactory.getBatchProjectWithProgress(project);

assertNotNull(batchProject);

IJavaProject javaProject = EclipseResourceUtil.getJavaProject(project);
assertNotNull(javaProject);

IType type = EclipseJavaUtil.findType(javaProject, "batch.SearchableBatchlet");
assertNotNull(type);

IField field = EclipseJavaUtil.findField(type, "otherName");
assertNotNull(field);

IBatchArtifact artifact = batchProject.getArtifact(type);
assertNotNull(artifact);

IBatchProperty batchProperty = artifact.getProperty(field);
assertNotNull(batchProperty);

Display display = Display.getCurrent();
if(display == null) {
display = Display.getDefault();
}
BatchPropertyDialog dialog = new BatchPropertyDialog(display.getActiveShell(), batchProperty);
dialog.setBlockOnOpen(false);
dialog.open();

HashMap<IFile, List<NodePathTextSourceReference>> references = dialog.getDisplayedReferences();

IFile jobXMLFile = project.getFile("src/META-INF/batch-jobs/job-search.xml");
assertTrue(jobXMLFile.exists());

List<NodePathTextSourceReference> list = references.get(jobXMLFile);
assertNotNull(list);

assertEquals(1, list.size());

NodePathTextSourceReference reference = list.get(0);
assertNotNull(reference);
}

private void checkHyperlinkDetector(String pageName) throws Exception{
List<TestRegion> regionList = getTestRegionList();
HyperlinkTestUtil.checkRegions(project, pageName, regionList, new BatchPropertyHyperlinkDetector());
}

private List<TestRegion> getTestRegionList(){
ArrayList<TestRegion> regionList = new ArrayList<TestRegion>();

regionList.add(new TestRegion("SearchableBatchlet", new TestHyperlink[]{}));

regionList.add(new TestRegion("Inject", new TestHyperlink[]{ //$NON-NLS-1$
new TestHyperlink(BatchPropertyHyperlink.class, "Show All Batch Property References") //$NON-NLS-1$
}));

regionList.add(new TestRegion("BatchProperty", new TestHyperlink[]{ //$NON-NLS-1$
new TestHyperlink(BatchPropertyHyperlink.class, "Show All Batch Property References") //$NON-NLS-1$
}));

regionList.add(new TestRegion("String", new TestHyperlink[]{ //$NON-NLS-1$
new TestHyperlink(BatchPropertyHyperlink.class, BatchHyperlinkMessages.SHOW_ALL_BATCH_PROPERTY_REFERENCES) //$NON-NLS-1$
}));

regionList.add(new TestRegion("otherName", new TestHyperlink[]{ //$NON-NLS-1$
new TestHyperlink(BatchPropertyHyperlink.class, BatchHyperlinkMessages.SHOW_ALL_BATCH_PROPERTY_REFERENCES) //$NON-NLS-1$
}));

return regionList;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static Test suite() {
TestSuite suite = new TestSuite("Editor");
suite.addTestSuite(BatchEditorTest.class);
suite.addTestSuite(BatchHyperlinkDetectorTest.class);
suite.addTestSuite(BatchPropertyHyperlinkDetectorTest.class);
suite.addTestSuite(BatchELHyperlinkTest.class);
suite.addTestSuite(BatchQueryParticipantTest.class);
suite.addTestSuite(BatchRenameParticipantTest.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jboss.tools.batch.core.IBatchArtifact;
import org.jboss.tools.batch.core.IBatchProperty;
import org.jboss.tools.batch.internal.core.impl.BatchUtil.AttrReferencesRequestor;
import org.jboss.tools.batch.internal.core.impl.BatchUtil.TextSourceReference;
import org.jboss.tools.batch.internal.core.impl.definition.FieldDefinition;
import org.jboss.tools.common.java.IAnnotationDeclaration;
import org.jboss.tools.common.text.ITextSourceReference;
Expand Down Expand Up @@ -82,9 +83,9 @@ public Collection<ITextSourceReference> getReferences() {
+ "/*[name()=\"" + BatchConstants.TAG_PROPERTIES + "\"]"
+ "/*[name()=\"" + BatchConstants.TAG_PROPERTY + "\" and @" + BatchConstants.ATTR_NAME + "=\"" + getPropertyName() +"\"]"
+ "/@" + BatchConstants.ATTR_NAME;
AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression);
AttrReferencesRequestor<TextSourceReference> requestor = new AttrReferencesRequestor<TextSourceReference>(file, expression, TextSourceReference.class);
BatchUtil.scanXMLFile(file, requestor);
result.addAll(requestor.getResults());
result.addAll((Collection<? extends ITextSourceReference>) requestor.getResults());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
Expand All @@ -39,9 +41,11 @@
import org.jboss.tools.common.EclipseUtil;
import org.jboss.tools.common.text.ITextSourceReference;
import org.jboss.tools.common.zip.UnzipOperation;
import org.jboss.tools.jst.web.kb.KbQuery.Tag;
import org.jboss.tools.jst.web.kb.WebKbPlugin;
import org.osgi.framework.Bundle;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Expand Down Expand Up @@ -102,9 +106,9 @@ public static void scanXMLFile(IFile file, DocumentScanner scanner) {
* @param value
* @return
*/
public static Collection<ITextSourceReference> getAttributeReferences(IFile file, String name, String value) {
public static List<TextSourceReference> getAttributeReferences(IFile file, String name, String value) {
String expression = "//*[@" + name + "=\"" + value + "\"]/@" + name;
AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression);
AttrReferencesRequestor<TextSourceReference> requestor = new AttrReferencesRequestor<TextSourceReference>(file, expression, TextSourceReference.class);
scanXMLFile(file, requestor);
return requestor.results;
}
Expand All @@ -117,24 +121,42 @@ public static Collection<ITextSourceReference> getAttributeReferences(IFile file
* @param nameValue value of name attribute
* @return
*/
public static Collection<ITextSourceReference> getPropertyAttributeReferences(IFile file, String refValue, String propertyName) {
public static List<TextSourceReference> getPropertyAttributeReferences(IFile file, String refValue, String propertyName) {
String expression = "//*[@"+BatchConstants.ATTR_REF+"=\""+refValue+"\"]//*[@" + BatchConstants.ATTR_NAME +
"=\"" + propertyName + "\"]/@" + BatchConstants.ATTR_NAME;
AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression);
AttrReferencesRequestor<TextSourceReference> requestor = new AttrReferencesRequestor<TextSourceReference>(file, expression, TextSourceReference.class);
scanXMLFile(file, requestor);
return requestor.results;
}

public static class AttrReferencesRequestor implements DocumentScanner {
/**
* Returns collection of text source references in xml file to name attribute of property tag by refValue and nameValue
*
* @param file
* @param refValue value of ref attribute
* @param nameValue value of name attribute
* @return
*/
public static List<NodePathTextSourceReference> getNodePathPropertyAttributeReferences(IFile file, String refValue, String propertyName) {
String expression = "//*[@"+BatchConstants.ATTR_REF+"=\""+refValue+"\"]//*[@" + BatchConstants.ATTR_NAME +
"=\"" + propertyName + "\"]/@" + BatchConstants.ATTR_NAME;
AttrReferencesRequestor<NodePathTextSourceReference> requestor = new AttrReferencesRequestor<NodePathTextSourceReference>(file, expression, NodePathTextSourceReference.class);
scanXMLFile(file, requestor);
return requestor.results;
}

public static class AttrReferencesRequestor<E extends TextSourceReference> implements DocumentScanner {
IFile file;
String expression;
Collection<ITextSourceReference> results = new HashSet<ITextSourceReference>();
List<E> results = new ArrayList<E>();
Class<E> cls;

public AttrReferencesRequestor(IFile file, String expression) {
public AttrReferencesRequestor(IFile file, String expression, Class<E> cls) {
this.file = file;
this.expression = expression;
this.cls = cls;
}

@Override
public void scanDocument(Document document) {
XPath xPath = XPathFactory.newInstance().newXPath();
Expand All @@ -154,21 +176,19 @@ public void scanDocument(Document document) {
}
final int start = start0;
final int length = length0;
ITextSourceReference ref = new ITextSourceReference() {
@Override
public int getStartPosition() {
return start;
}
@Override
public IResource getResource() {
return file;
}
@Override
public int getLength() {
return length;
}
};
results.add(ref);
try{
E ref = cls.newInstance();
ref.setLength(length);
ref.setResource(file);
ref.setStartPosition(start);
ref.setNodePath(n);

results.add(ref);
}catch(InstantiationException e){

}catch(IllegalAccessException e){

}
}
}
}
Expand All @@ -177,11 +197,37 @@ public int getLength() {
}
}

public Collection<ITextSourceReference> getResults() {
public List<E> getResults() {
return results;
}
}

private static List<Tag> getNodePath(Node node){
ArrayList<Tag> tags = new ArrayList<Tag>();
Node n = node;
if(n instanceof AttrImpl){
n = ((AttrImpl) node).getOwnerElement();
}

while(!(n instanceof IDOMDocument) && n != null){
HashMap<String, String> attributes = new HashMap<String, String>();
if(n.hasAttributes()){

NamedNodeMap attrs = n.getAttributes();
for(int index = 0; index < attrs.getLength(); index++){
Node attribute = attrs.item(index);
attributes.put(attribute.getNodeName(), attribute.getNodeValue());
}

}
Tag tag = new Tag(n.getNodeName(), attributes);
tags.add(tag);
n = n.getParentNode();
}

return tags;
}

private static File TEMPLATE_FOLDER;

public static File getTemplatesFolder() throws IOException {
Expand Down Expand Up @@ -226,4 +272,57 @@ public static IPath getBatchXMLPath(IProject p) {
}
return result;
}

public static class TextSourceReference implements ITextSourceReference{
private IResource resource;
int startPosition;
int length;

public TextSourceReference(){
}

@Override
public int getStartPosition() {
return startPosition;
}

@Override
public int getLength() {
return length;
}

@Override
public IResource getResource() {
return resource;
}

public void setResource(IResource resource) {
this.resource = resource;
}

public void setStartPosition(int startPosition) {
this.startPosition = startPosition;
}

public void setLength(int length) {
this.length = length;
}

public void setNodePath(Node node){

}

}

public static class NodePathTextSourceReference extends TextSourceReference{
private List<Tag> tags;

public Collection<Tag> getNodePath(){
return tags;
}

public void setNodePath(Node node){
this.tags = BatchUtil.getNodePath(node);
}
}
}
Loading