Author: koen.aers(a)jboss.com
Date: 2011-07-04 08:57:07 -0400 (Mon, 04 Jul 2011)
New Revision: 32549
Modified:
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferences.java
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferencesTest.java
Log:
implemented ForgeRuntimesPreferences
Modified:
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferences.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferences.java 2011-07-04
12:09:06 UTC (rev 32548)
+++
trunk/forge/plugins/org.jboss.tools.forge.core/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferences.java 2011-07-04
12:57:07 UTC (rev 32549)
@@ -2,6 +2,7 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@@ -11,11 +12,18 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+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.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.jboss.tools.forge.core.ForgeCorePlugin;
import org.jboss.tools.forge.core.process.ForgeEmbeddedRuntime;
+import org.jboss.tools.forge.core.process.ForgeExternalRuntime;
import org.jboss.tools.forge.core.process.ForgeRuntime;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -29,8 +37,8 @@
public static final ForgeRuntimesPreferences INSTANCE = new ForgeRuntimesPreferences();
- List<ForgeRuntime> runtimes = null;
- ForgeRuntime defaultRuntime = null;
+ private List<ForgeRuntime> runtimes = null;
+ private ForgeRuntime defaultRuntime = null;
private ForgeRuntimesPreferences() {}
@@ -80,6 +88,10 @@
ForgeRuntime runtime = null;
if ("embedded".equals(type)) {
runtime = ForgeEmbeddedRuntime.INSTANCE;
+ } else if ("external".equals(type)) {
+ String name = element.getAttribute("name");
+ String location = element.getAttribute("location");
+ runtime = new ForgeExternalRuntime(name, location);
}
if (runtime == null) continue;
runtimes.add(runtime);
@@ -120,73 +132,67 @@
return null;
}
}
+
+ public void setRuntimes(ForgeRuntime[] runtimes, ForgeRuntime defaultRuntime) {
+ this.runtimes.clear();
+ for (ForgeRuntime runtime : runtimes) {
+ this.runtimes.add(runtime);
+ }
+ this.defaultRuntime = defaultRuntime;
+ saveRuntimes();
+ }
-// private static Document createEmptyDocument() {
-// DocumentBuilder documentBuilder = newDocumentBuilder();
-// if (documentBuilder == null) {
-// return null;
-// } else {
-// return documentBuilder.newDocument();
-// }
-// }
-//
-// private static String serializeDocument(Document doc) throws TransformerException,
IOException {
-// ByteArrayOutputStream s = new ByteArrayOutputStream();
-// TransformerFactory factory = TransformerFactory.newInstance();
-// Transformer transformer = factory.newTransformer();
-// transformer.setOutputProperty(OutputKeys.METHOD, "xml");
-// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-// DOMSource source = new DOMSource(doc);
-// StreamResult outputTarget = new StreamResult(s);
-// transformer.transform(source, outputTarget);
-// return s.toString("UTF8");
-// }
-//
-// private static void createInitialInstallations() {
-// try {
-// File file = FileLocator.getBundleFile(ForgeUIPlugin.getDefault().getBundle());
-// defaultInstallation = new ForgeInstallation("embedded",
file.getAbsolutePath());
-// installations = new ArrayList<ForgeInstallation>();
-// installations.add(defaultInstallation);
-// saveInstallations();
-// } catch (IOException e) {
-// ForgeUIPlugin.log(e);
-// }
-// }
-//
-// public static void setInstallations(ForgeInstallation[] installs, ForgeInstallation
defaultInstall) {
-// installations.clear();
-// for (ForgeInstallation install : installs) {
-// installations.add(install);
-// }
-// defaultInstallation = defaultInstall;
-// saveInstallations();
-// }
-//
-// private static void saveInstallations() {
-// try {
-// String xml = serializeDocument(createInstallationsDocument());
-// ForgeUIPlugin.getDefault().getPreferenceStore().setValue(PREF_FORGE_INSTALLATIONS,
xml);
-// } catch (IOException e) {
-// ForgeUIPlugin.log(e);
-// } catch (TransformerException e) {
-// ForgeUIPlugin.log(e);
-// }
-// }
-//
-// private static Document createInstallationsDocument() {
-// Document document = createEmptyDocument();
-// if (document == null) return null;
-// Element main = document.createElement("forgeInstallations");
-// document.appendChild(main);
-// for (ForgeInstallation installation : installations) {
-// Element element = document.createElement("installation");
-// element.setAttribute("name", installation.getName());
-// element.setAttribute("location", installation.getLocation());
-// main.appendChild(element);
-// }
-// main.setAttribute("default", defaultInstallation.getName());
-// return document;
-// }
+ private void saveRuntimes() {
+ try {
+ String xml = serializeDocument(createRuntimesDocument());
+ InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID).put(PREF_FORGE_RUNTIMES,
xml);
+ } catch (IOException e) {
+ ForgeCorePlugin.log(e);
+ } catch (TransformerException e) {
+ ForgeCorePlugin.log(e);
+ }
+ }
+
+ private Document createRuntimesDocument() {
+ Document document = createEmptyDocument();
+ if (document == null)
+ return null;
+ Element main = document.createElement("forgeRuntimes");
+ document.appendChild(main);
+ for (ForgeRuntime runtime : runtimes) {
+ Element element = document.createElement("runtime");
+ element.setAttribute("name", runtime.getName());
+ if (!(runtime instanceof ForgeEmbeddedRuntime)) {
+ element.setAttribute("location", runtime.getLocation());
+ }
+ element.setAttribute("type", runtime.getType());
+ main.appendChild(element);
+ }
+ if (defaultRuntime != null) {
+ main.setAttribute("default", defaultRuntime.getName());
+ }
+ return document;
+ }
+
+ private Document createEmptyDocument() {
+ DocumentBuilder documentBuilder = newDocumentBuilder();
+ if (documentBuilder == null) {
+ return null;
+ } else {
+ return documentBuilder.newDocument();
+ }
+ }
+
+ private static String serializeDocument(Document doc) throws TransformerException,
IOException {
+ ByteArrayOutputStream s = new ByteArrayOutputStream();
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ DOMSource source = new DOMSource(doc);
+ StreamResult outputTarget = new StreamResult(s);
+ transformer.transform(source, outputTarget);
+ return s.toString("UTF8");
+ }
}
Modified:
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferencesTest.java
===================================================================
---
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferencesTest.java 2011-07-04
12:09:06 UTC (rev 32548)
+++
trunk/forge/tests/org.jboss.tools.forge.core.test/src/org/jboss/tools/forge/core/preferences/ForgeRuntimesPreferencesTest.java 2011-07-04
12:57:07 UTC (rev 32549)
@@ -1,8 +1,16 @@
package org.jboss.tools.forge.core.preferences;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.jboss.tools.forge.core.ForgeCorePlugin;
import org.jboss.tools.forge.core.process.ForgeEmbeddedRuntime;
+import org.jboss.tools.forge.core.process.ForgeExternalRuntime;
+import org.jboss.tools.forge.core.process.ForgeRuntime;
import org.junit.Test;
public class ForgeRuntimesPreferencesTest {
@@ -13,7 +21,7 @@
" <runtime name=\"embedded\"
type=\"embedded\"/>" +
" <runtime name=\"foo\" location=\"foofoo\"
type=\"external\"/>" +
" <runtime name=\"bar\" location=\"barbar\"
type=\"external\"/>" +
- "</forgeInstallations>";
+ "</forgeRuntimes>";
@Test
public void testGetDefaultInitialCase() {
@@ -25,18 +33,144 @@
assertEquals(1, ForgeRuntimesPreferences.INSTANCE.getRuntimes().length);
}
+ @Test
+ public void testSetRuntimes() {
+ ForgeRuntime[] runtimes = new ForgeRuntime[3];
+ runtimes[0] = ForgeEmbeddedRuntime.INSTANCE;
+ runtimes[1] = new ForgeExternalRuntime("foo", "foofoo");
+ runtimes[2] = new ForgeExternalRuntime("bar", "barbar");
+ ForgeRuntimesPreferences.INSTANCE.setRuntimes(runtimes, runtimes[1]);
+ assertArrayEquals(runtimes, ForgeRuntimesPreferences.INSTANCE.getRuntimes());
+ assertEquals(runtimes[1], ForgeRuntimesPreferences.INSTANCE.getDefault());
+ verifyForgeRuntimesPreferencesString();
+ }
+
+ private void verifyForgeRuntimesPreferencesString() {
+ String forgeRuntimesPrefs =
+ InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID).get(
+ ForgeRuntimesPreferences.PREF_FORGE_RUNTIMES, null);
+ verifyDefaultRuntime(forgeRuntimesPrefs);
+ verifyRuntimes(forgeRuntimesPrefs);
+ }
+
+ private void verifyDefaultRuntime(String xml) {
+ String searchString = "default=\"";
+ int start = xml.indexOf(searchString);
+ assertTrue(start != -1);
+ start = start + searchString.length();
+ assertTrue(start < xml.length());
+ int end = xml.indexOf("\">", start);
+ assertTrue(end != -1);
+ assertEquals("foo", xml.substring(start, end));
+ }
+
+ private void verifyRuntimes(String xml) {
+ int startIndex = 0;
+ for (int i = 0; i < 3; i++) {
+ int start = xml.indexOf("<runtime ", startIndex);
+ assertTrue(start != -1);
+ int end = xml.indexOf("/>", start);
+ assertTrue(end != -1);
+ verifyRuntime(xml.substring(start, end + 2));
+ startIndex = end + 2;
+ }
+ }
+
+ private void verifyRuntime(String xml) {
+ int start = xml.indexOf("name=\"");
+ assertTrue(start != -1);
+ int end = xml.indexOf("\"", start);
+ assertTrue(end != -1);
+ String name = xml.substring(start, end);
+ if ("embedded".equals(name)) {
+ verifyEmbedded(xml);
+ } else if ("foo".equals(name)) {
+ verifyFoo(xml);
+ } else if ("bar".equals(name)) {
+ verifyBar(xml);
+ }
+ }
+
+ private void verifyEmbedded(String xml) {
+ verifyAttribute("name", "embedded", xml);
+ verifyAttribute("type", "embedded", xml);
+ verifyAttribute("location", null, xml);
+ }
+
+ private void verifyFoo(String xml) {
+ verifyAttribute("name", "foo", xml);
+ verifyAttribute("type", "external", xml);
+ verifyAttribute("location", "foofoo", xml);
+ }
+
+ private void verifyBar(String xml) {
+ verifyAttribute("name", "bar", xml);
+ verifyAttribute("type", "external", xml);
+ verifyAttribute("location", "barbar", xml);
+ }
+
+ private void verifyAttribute(String name, String expectedValue, String string) {
+ int start = string.indexOf(name + "=\"");
+ if (start == -1) {
+ assertNull(expectedValue);
+ } else {
+ start += name.length() + 2;
+ int end = string.indexOf('\"', start);
+ assertTrue(end != -1);
+ assertEquals(expectedValue, string.substring(start, end));
+ }
+ }
+
+ @Test
+ public void testGetDefaultAndGetInstallationsAlternativeCase() {
+ // trick the preferences into initializing from the alternative preference string
+ ForgeRuntimesPreferences.INSTANCE.setRuntimes(new ForgeRuntime[0], null);
+ InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID).put(
+ ForgeRuntimesPreferences.PREF_FORGE_RUNTIMES,
+ ALTERNATIVE_FORGE_RUNTIMES);
+ // getDefault() will now trigger the initialization
+ ForgeRuntime runtime = ForgeRuntimesPreferences.INSTANCE.getDefault();
+ assertEquals("foo", runtime.getName());
+ assertEquals("foofoo", runtime.getLocation());
+ assertEquals("external", runtime.getType());
+ // getRuntimes() will return the list of runtimes from the alternative preference
string
+ ForgeRuntime[] runtimes = ForgeRuntimesPreferences.INSTANCE.getRuntimes();
+ verifyEmbedded(runtimes);
+ verifyFoo(runtimes);
+ verifyBar(runtimes);
+ }
+
+ private ForgeRuntime getRuntimeToVerify(String name, ForgeRuntime[] runtimes) {
+ ForgeRuntime result = null;
+ for (int i = 0; i < runtimes.length; i++) {
+ if (name.equals(runtimes[i].getName())) {
+ result = runtimes[i];
+ }
+ }
+ return result;
+ }
+
+ private void verifyEmbedded(ForgeRuntime[] runtimes) {
+ assertEquals(ForgeEmbeddedRuntime.INSTANCE, getRuntimeToVerify("embedded",
runtimes));
+ }
+
+ private void verifyFoo(ForgeRuntime[] runtimes) {
+ ForgeRuntime runtimeToVerify = getRuntimeToVerify("foo", runtimes);
+ assertNotNull(runtimeToVerify);
+ assertEquals("foo", runtimeToVerify.getName());
+ assertEquals("foofoo", runtimeToVerify.getLocation());
+ assertEquals("external", runtimeToVerify.getType());
+ }
+
+ private void verifyBar(ForgeRuntime[] runtimes) {
+ ForgeRuntime runtimeToVerify = getRuntimeToVerify("bar", runtimes);
+ assertNotNull(runtimeToVerify);
+ assertEquals("bar", runtimeToVerify.getName());
+ assertEquals("barbar", runtimeToVerify.getLocation());
+ assertEquals("external", runtimeToVerify.getType());
+ }
+
// @Test
-// public void testGetDefaultAlternativeCase() {
-// ForgeInstallations.INSTANCE.defaultInstallation = null;
-// InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID).put(
-// ForgeInstallations.PREF_FORGE_INSTALLATIONS,
-// ALTERNATIVE_FORGE_INSTALLATIONS);
-// ForgeRuntime runtime = ForgeInstallations.INSTANCE.getDefault();
-// assertNotNull(ForgeInstallations.INSTANCE.defaultInstallation);
-// assertEquals("foo", runtime.getName());
-// }
-//
-// @Test
// public void testGetInstallationsAlternativeCase() {
// ForgeInstallations.INSTANCE.installations = null;
// InstanceScope.INSTANCE.getNode(ForgeCorePlugin.PLUGIN_ID).put(