Author: adietish
Date: 2012-01-12 14:04:21 -0500 (Thu, 12 Jan 2012)
New Revision: 37800
Added:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/OpenShiftProfile.java
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftProfileTests.java
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftTestSuite.java
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java
Log:
[JBIDE-10479] implemented class (and tests) to handle openshift profile in poms
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java 2012-01-12
18:57:07 UTC (rev 37799)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/utils/FileUtils.java 2012-01-12
19:04:21 UTC (rev 37800)
@@ -14,10 +14,12 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.PrintWriter;
import org.eclipse.core.runtime.Assert;
@@ -148,10 +150,15 @@
Assert.isLegal(source != null);
Assert.isLegal(destination != null);
- InputStream in = null;
writeTo(new BufferedInputStream(new FileInputStream(source)), destination);
}
+ public static final void writeTo(String content, File destination) throws
FileNotFoundException {
+ PrintWriter writer = new PrintWriter(destination);
+ writer.flush();
+ writer.close();
+ }
+
private static final void writeTo(InputStream in, File destination) throws IOException
{
Assert.isLegal(in != null);
Assert.isLegal(destination != null);
Added:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/OpenShiftProfile.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/OpenShiftProfile.java
(rev 0)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/OpenShiftProfile.java 2012-01-12
19:04:21 UTC (rev 37800)
@@ -0,0 +1,301 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.openshift.express.internal.ui.wizard.appimport;
+
+import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.text.MessageFormat;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.osgi.util.NLS;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * @author André Dietisheim
+ */
+public class OpenShiftProfile {
+
+ private static final String ID_OPENSHIFT = "openshift";
+ private static final String ELEMENT_PROJECT = "project";
+ private static final String ELEMENT_PROFILES = "profiles";
+ private static final String ELEMENT_PROFILE = "profile";
+ private static final String ELEMENT_ID = "id";
+
+ private static final String OPENSHIFT_PROFILE =
+ "<profile>\n"
+ + "<!-- When built in OpenShift the 'openshift' profile will be used
when invoking mvn. -->\n"
+ + "<!-- Use this profile for any OpenShift specific customization your app
will need. -->\n"
+ + "<!-- By default that is to put the resulting archive into the
'deployments' folder. -->\n"
+ + "<!--
http://maven.apache.org/guides/mini/guide-building-for-different-environm...
-->\n"
+ + "<id>openshift</id>\n"
+ + "<build>\n"
+ + " <finalName>{0}</finalName>\n"
+ + " <plugins>\n"
+ + " <plugin>\n"
+ + " <artifactId>maven-war-plugin</artifactId>\n"
+ + " <version>2.1.1</version>\n"
+ + " <configuration>\n"
+ + " <outputDirectory>deployments</outputDirectory>\n"
+ + " <warName>ROOT</warName>\n"
+ + " </configuration>\n"
+ + " </plugin>\n"
+ + " </plugins>\n"
+ + " </build>\n"
+ + "</profile>\n";
+
+ private IFile pomFile;
+ private String pluginId;
+ private Document document;
+
+ /**
+ * Creates an openshift profile that will allow you to deal with the
+ * openshift profile in a given pom.
+ *
+ * @param pomFile
+ * @param pluginId
+ */
+ public OpenShiftProfile(IFile pomFile, String pluginId) {
+ this.pomFile = pomFile;
+ this.pluginId = pluginId;
+ }
+
+ /**
+ * Checks the pom (that was given at constructin time) for presence of the
+ * OpenShift profile. Returns <code>true</code> if this pom has the
+ * OpenShift profile, <code>false</code> otherwise.
+ *
+ * @return <code>true</code> if the pom has the openshift profile.
+ * @throws CoreException
+ */
+ public boolean existsInPom() throws CoreException {
+ if (!exists(pomFile)) {
+ return false;
+ }
+
+ Element openShiftProfileElement = getOpenShiftProfileElement(getDocument());
+ return openShiftProfileElement != null;
+ }
+
+ /**
+ * Adds the openshift profile to the pom this is instance is bound to.
+ * Returns <code>true</code> if it was added, <code>false</code>
otherwise.
+ *
+ * @return true if the profile was added to the pom this instance is bound
+ * to.
+ * @throws CoreException
+ */
+ public boolean addToPom(String finalName) throws CoreException {
+ try {
+ if (existsInPom()) {
+ return false;
+ }
+ Document document = getDocument();
+ Element profilesElement = getOrCreateProfilesElement(document);
+ Node profileNode = document.importNode(createOpenShiftProfileElement(finalName),
true);
+ profilesElement.appendChild(profileNode);
+ return true;
+ } catch (SAXException e) {
+ throw new CoreException(createStatus(e));
+ } catch (IOException e) {
+ throw new CoreException(createStatus(e));
+ } catch (ParserConfigurationException e) {
+ throw new CoreException(createStatus(e));
+ }
+ }
+
+ private Element createOpenShiftProfileElement(String finalName) throws
ParserConfigurationException, SAXException, IOException {
+ DocumentBuilder documentBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ String openShiftProfile = MessageFormat.format(OPENSHIFT_PROFILE, finalName);
+ Document document = documentBuilder.parse(new
ByteArrayInputStream(openShiftProfile.getBytes()));
+ return document.getDocumentElement();
+ }
+
+ private Element getOrCreateProfilesElement(Document document) throws CoreException {
+ Element profilesElement = getProfilesElement(document);
+ if (profilesElement == null) {
+ profilesElement = createProfilesElement(document);
+ }
+ return profilesElement;
+ }
+
+ private Element createProfilesElement(Document document) throws CoreException {
+ Element profilesElement;
+ profilesElement = document.createElement(ELEMENT_PROFILES);
+ Element projectElement = getProjectElement(document);
+ if (projectElement == null) {
+ throw new CoreException(
+ createStatus(NLS.bind("Could not find <project> tag in pom {0}",
pomFile.toString())));
+ }
+ projectElement.appendChild(profilesElement);
+ return profilesElement;
+ }
+
+ private boolean exists(IFile file) {
+ return file != null
+ && file.exists();
+ }
+
+ private Element getProfilesElement(Document document) {
+ return getFirstElement(ELEMENT_PROFILES, document);
+ }
+
+ private Element getProjectElement(Document document) {
+ return getFirstElement(ELEMENT_PROJECT, document);
+ }
+
+ private Element getOpenShiftProfileElement(Document document) {
+ return getOpenShiftProfileElement(getProfilesElement(document));
+ }
+
+ private Element getOpenShiftProfileElement(Element element) {
+ Element openshiftProfile = getFirstElementByMatcher(ELEMENT_PROFILE, new IMatcher() {
+
+ @Override
+ public boolean isMatch(Element element) {
+ if (element == null) {
+ return false;
+ }
+
+ Element idElement = getFirstElement(ELEMENT_ID, element);
+ if (idElement == null) {
+ return false;
+ }
+ if (idElement.hasChildNodes()) {
+ return ID_OPENSHIFT.equals(idElement.getFirstChild().getTextContent());
+ }
+ return false;
+ }
+ }, element);
+ return openshiftProfile;
+ }
+
+ private Element getFirstElement(String elementName, Document document) {
+ NodeList elements = document.getElementsByTagName(elementName);
+ if (elements != null
+ && elements.getLength() > 0) {
+ return (Element) elements.item(0);
+ }
+ return null;
+ }
+
+ protected Element getFirstElement(String elementName, Element element) {
+ NodeList children = element.getElementsByTagName(elementName);
+ if (children == null
+ || children.getLength() == 0) {
+ return null;
+ }
+ return (Element) children.item(0);
+ }
+
+ protected Element getFirstElementByMatcher(String elementName, IMatcher matcher, Element
element) {
+ if (element == null) {
+ return null;
+ }
+
+ NodeList children = element.getElementsByTagName(elementName);
+ if (children == null
+ || children.getLength() == 0) {
+ return null;
+ }
+ for (int i = 0; i < children.getLength(); i++) {
+ Element child = (Element) children.item(i);
+ if (matcher.isMatch(child)) {
+ return child;
+ }
+ }
+ return null;
+ }
+
+ private Document getDocument() throws CoreException {
+ return getDocument(pomFile);
+ }
+
+ private Document getDocument(IFile file) throws CoreException {
+ return getDocument(file.getContents());
+ }
+
+ private Document getDocument(InputStream inputStream) throws CoreException {
+ try {
+ if (document == null) {
+ DocumentBuilder documentBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ this.document = documentBuilder.parse(inputStream);
+ }
+ return document;
+ } catch (ParserConfigurationException e) {
+ throw new CoreException(createStatus(e));
+ } catch (SAXException e) {
+ throw new CoreException(createStatus(e));
+ } catch (IOException e) {
+ throw new CoreException(createStatus(e));
+ }
+ }
+
+ private interface IMatcher {
+ public boolean isMatch(Element element);
+ }
+
+ private IStatus createStatus(Throwable e) {
+ return new Status(IStatus.ERROR, pluginId, e.getMessage(), e);
+ }
+
+ private IStatus createStatus(String message) {
+ return new Status(IStatus.ERROR, pluginId, message);
+ }
+
+ public void savePom() throws CoreException {
+ try {
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ transformerFactory.setAttribute("indent-number", new Integer(4));
+
+ Transformer transformer = transformerFactory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+
+ Result out =
+ new StreamResult(
+ new OutputStreamWriter(new FileOutputStream(pomFile.getLocation().toString()),
"UTF-8"));
+ transformer.transform(new DOMSource(getDocument()), out);
+ } catch (TransformerConfigurationException e) {
+ throw new CoreException(createStatus(e));
+ } catch (UnsupportedEncodingException e) {
+ throw new CoreException(createStatus(e));
+ } catch (TransformerException e) {
+ throw new CoreException(createStatus(e));
+ } catch (FileNotFoundException e) {
+ throw new CoreException(createStatus(e));
+ }
+ }
+}
Property changes on:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/OpenShiftProfile.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftProfileTests.java
===================================================================
---
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftProfileTests.java
(rev 0)
+++
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftProfileTests.java 2012-01-12
19:04:21 UTC (rev 37800)
@@ -0,0 +1,189 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.openshift.express.test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.jboss.tools.openshift.express.internal.ui.wizard.appimport.OpenShiftProfile;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Andre Dietisheim
+ */
+public class OpenShiftProfileTests {
+
+ private static final String PLUGIN_ID =
"org.jboss.tools.openshift.express.test";
+
+ private static final String POM_FILENAME = "pom.xml";
+
+ private static final String POM_WITHOUT_OPENSHIFT =
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ + "<project
xmlns=\"http://maven.apache.org/POM/4.0.0\" "
+ + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ + "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
+ + " <modelVersion>4.0.0</modelVersion>\n"
+ + " <groupId>org.jboss.tools.openshift.tests</groupId>\n"
+ +
" <artifactId>org.jboss.tools.openshift.express.test</artifactId>\n"
+ + " <packaging>eclipse-test-plugin</packaging>\n"
+ + "</project>\n";
+
+ private static final String OPENSHIFT_PROFILE =
+ "<!-- When built in OpenShift the 'openshift' profile will be used
when invoking mvn. -->\n"
+ + "<!-- Use this profile for any OpenShift specific customization your app
will need. -->\n"
+ + "<!-- By default that is to put the resulting archive into the
'deployments' folder. -->\n"
+ + "<!--
http://maven.apache.org/guides/mini/guide-building-for-different-environm...
-->\n"
+ + "<id>openshift</id>\n"
+ + "<build>\n"
+ + " <finalName>as22</finalName>\n"
+ + " <plugins>\n"
+ + " <plugin>\n"
+ + " <artifactId>maven-war-plugin</artifactId>\n"
+ + " <version>2.1.1</version>\n"
+ + " <configuration>\n"
+ + " <outputDirectory>deployments</outputDirectory>\n"
+ + " <warName>ROOT</warName>\n"
+ + " </configuration>\n"
+ + " </plugin>\n"
+ + " </plugins>\n"
+ + "</build>\n";
+
+ private static final String POM_WITH_OPENSHIFT =
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ + "<project
xmlns=\"http://maven.apache.org/POM/4.0.0\" "
+ + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ + "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
+ + " <modelVersion>4.0.0</modelVersion>\n"
+ + " <groupId>org.jboss.tools.openshift.tests</groupId>\n"
+ +
" <artifactId>org.jboss.tools.openshift.express.test</artifactId>\n"
+ + " <packaging>eclipse-test-plugin</packaging>\n"
+ + " <profiles>\n"
+ + " <profile>\n"
+
+ + OPENSHIFT_PROFILE
+
+ + " </profile>\n"
+ + " </profiles>\n"
+ + "</project>\n";
+
+ private IProject nonOpenShiftProject;
+ private IFile pomWithoutOpenShiftProfile;
+ private IProject openShiftProject;
+ private IFile pomWithOpenShiftProfile;
+
+ @Before
+ public void setUp() throws CoreException {
+ this.openShiftProject = createTmpProject();
+ this.pomWithOpenShiftProfile = createPomFile(POM_WITH_OPENSHIFT, openShiftProject);
+ this.nonOpenShiftProject = createTmpProject();
+ this.pomWithoutOpenShiftProfile = createPomFile(POM_WITHOUT_OPENSHIFT,
nonOpenShiftProject);
+ }
+
+ @After
+ public void tearDown() throws CoreException {
+ deleteProject(openShiftProject);
+ deleteProject(nonOpenShiftProject);
+ }
+
+ private void deleteProject(final IProject project) throws CoreException {
+ if (project == null
+ || !project.isAccessible()) {
+ return;
+ }
+ project.getWorkspace().run(new IWorkspaceRunnable() {
+
+ @Override
+ public void run(IProgressMonitor monitor) throws CoreException {
+ project.close(null);
+ project.delete(true, null);
+ }
+ }, null);
+ }
+
+ @Test
+ public void canDetectOpenShiftProfileNotPresent() throws CoreException {
+ OpenShiftProfile profile = new OpenShiftProfile(pomWithoutOpenShiftProfile,
PLUGIN_ID);
+ assertFalse(profile.existsInPom());
+ }
+
+ @Test
+ public void canDetectOpenShiftProfilePresent() throws CoreException {
+ OpenShiftProfile profile = new OpenShiftProfile(pomWithOpenShiftProfile, PLUGIN_ID);
+ assertTrue(profile.existsInPom());
+ }
+
+ @Test
+ public void canAddOpenShiftProfile() throws CoreException {
+ OpenShiftProfile profile = new OpenShiftProfile(pomWithoutOpenShiftProfile,
PLUGIN_ID);
+ boolean added = profile.addToPom(nonOpenShiftProject.getName());
+ assertTrue(added);
+ }
+
+ @Test
+ public void pomHasOpenShiftProfileAfterAdd() throws CoreException {
+ OpenShiftProfile profile = new OpenShiftProfile(pomWithoutOpenShiftProfile,
PLUGIN_ID);
+ profile.addToPom(nonOpenShiftProject.getName());
+ profile.savePom();
+ profile = new OpenShiftProfile(pomWithoutOpenShiftProfile, PLUGIN_ID);
+ assertTrue(profile.existsInPom());
+ }
+
+ @Test
+ public void doesNotAddOpenShiftProfileIfAlreadyPresent() throws CoreException {
+ OpenShiftProfile profile = new OpenShiftProfile(pomWithOpenShiftProfile, PLUGIN_ID);
+ boolean added = profile.addToPom(openShiftProject.getName());
+ assertFalse(added);
+ }
+
+ private IFile createPomFile(final String content, final IProject project) throws
CoreException {
+ final IFile pomFile = project.getFile(POM_FILENAME);
+ IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
+
+ @Override
+ public void run(IProgressMonitor monitor) throws CoreException {
+ pomFile.create(
+ new ByteArrayInputStream(content.getBytes())
+ , true
+ , new NullProgressMonitor());
+ pomFile.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
+ }
+ };
+ project.getWorkspace().run(runnable, new NullProgressMonitor());
+ return pomFile;
+ }
+
+ private IProject createTmpProject() throws CoreException {
+ String name = String.valueOf(System.currentTimeMillis());
+ final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
+ IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
+
+ @Override
+ public void run(IProgressMonitor monitor) throws CoreException {
+ project.create(new NullProgressMonitor());
+ project.open(new NullProgressMonitor());
+ }
+ };
+ project.getWorkspace().run(runnable, new NullProgressMonitor());
+ return project;
+ }
+}
Property changes on:
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftProfileTests.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftTestSuite.java
===================================================================
---
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftTestSuite.java
(rev 0)
+++
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftTestSuite.java 2012-01-12
19:04:21 UTC (rev 37800)
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.openshift.express.test;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+(a)RunWith(Suite.class)
+@SuiteClasses({
+ OpenShiftProfileTests.class
+})
+/**
+ * @author Andre Dietisheim
+ */
+public class OpenShiftTestSuite {
+}
Property changes on:
trunk/openshift/tests/org.jboss.tools.openshift.express.test/src/org/jboss/tools/openshift/express/test/OpenShiftTestSuite.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain