Author: adietish
Date: 2011-10-03 10:07:20 -0400 (Mon, 03 Oct 2011)
New Revision: 35282
Added:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/UserConfiguration.java
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/UserConfigurationTest.java
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/fakes/UserConfigurationFake.java
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/InternalUser.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/utils/StreamUtils.java
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/OpenshiftTestSuite.java
Log:
[JBIDE-9819] implemented UserConfiguration & tests
Added:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/UserConfiguration.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/UserConfiguration.java
(rev 0)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/UserConfiguration.java 2011-10-03
14:07:20 UTC (rev 35282)
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * 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.ide.eclipse.as.openshift.core;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Properties;
+
+import org.jboss.ide.eclipse.as.openshift.core.internal.utils.StreamUtils;
+
+/**
+ * @author André Dietisheim
+ */
+public class UserConfiguration {
+
+ private static final String CONFIGURATION_FOLDER = ".openshift";
+ private static final String CONFIGURATION_FILE = "express.conf";
+ private static final String PROPERTY_USERHOME = "user.home";
+ protected static final String KEY_RHLOGIN = "default_rhlogin";
+
+ private Properties properties;
+ protected File file;
+
+ public UserConfiguration() throws OpenshiftException, IOException {
+ this.file = getUserConfigurationFile();
+ this.properties = getUserProperties(file);
+ }
+
+ protected File getUserConfigurationFile() throws OpenshiftException, IOException {
+ String userHome = System.getProperty(PROPERTY_USERHOME);
+ if (userHome == null) {
+ throw new OpenshiftException("Could not read user configuration: user home
directory not found");
+ }
+ return new File(CONFIGURATION_FILE, userHome + File.separatorChar +
CONFIGURATION_FOLDER);
+ }
+
+ protected Properties getUserProperties(File userConfigurationFile) throws
FileNotFoundException, IOException {
+ FileReader reader = null;
+ try {
+ Properties userConfigurationProperties = new Properties();
+ reader = new FileReader(userConfigurationFile);
+ userConfigurationProperties.load(reader);
+ return userConfigurationProperties;
+ } finally {
+ StreamUtils.close(reader);
+ }
+ }
+
+ public String getRhlogin() {
+ return properties.getProperty(KEY_RHLOGIN);
+ }
+
+ public void setRhlogin(String rhlogin) {
+ properties.put(KEY_RHLOGIN, rhlogin);
+ }
+
+ public void store() throws IOException {
+ Writer writer = null;
+ try {
+ writer = new FileWriter(file);
+ properties.store(writer, "");
+ } finally {
+ StreamUtils.close(writer);
+ }
+ }
+}
Property changes on:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/UserConfiguration.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/InternalUser.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/InternalUser.java 2011-10-03
13:42:27 UTC (rev 35281)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/InternalUser.java 2011-10-03
14:07:20 UTC (rev 35282)
@@ -10,6 +10,7 @@
******************************************************************************/
package org.jboss.ide.eclipse.as.openshift.core.internal;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -24,6 +25,7 @@
import org.jboss.ide.eclipse.as.openshift.core.InvalidCredentialsOpenshiftException;
import org.jboss.ide.eclipse.as.openshift.core.OpenshiftException;
import org.jboss.ide.eclipse.as.openshift.core.OpenshiftService;
+import org.jboss.ide.eclipse.as.openshift.core.UserConfiguration;
/**
* @author André Dietisheim
@@ -40,6 +42,14 @@
private IOpenshiftService service;
+ public InternalUser(String password) throws OpenshiftException, IOException {
+ this(new UserConfiguration(), password);
+ }
+
+ public InternalUser(UserConfiguration configuration, String password) {
+ this(configuration.getRhlogin(), password, (ISSHPublicKey) null, new
OpenshiftService());
+ }
+
public InternalUser(String rhlogin, String password) {
this(rhlogin, password, (ISSHPublicKey) null, new OpenshiftService());
}
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/utils/StreamUtils.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/utils/StreamUtils.java 2011-10-03
13:42:27 UTC (rev 35281)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/internal/utils/StreamUtils.java 2011-10-03
14:07:20 UTC (rev 35282)
@@ -2,12 +2,14 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
+import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
+import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
@@ -79,4 +81,16 @@
}
}
+ public static void close(Reader reader) throws IOException {
+ if (reader != null) {
+ reader.close();
+ }
+ }
+
+ public static void close(Writer writer) throws IOException {
+ if (writer != null) {
+ writer.close();
+ }
+ }
+
}
Modified:
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/OpenshiftTestSuite.java
===================================================================
---
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/OpenshiftTestSuite.java 2011-10-03
13:42:27 UTC (rev 35281)
+++
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/OpenshiftTestSuite.java 2011-10-03
14:07:20 UTC (rev 35282)
@@ -16,6 +16,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
+ UserConfigurationTest.class,
ApplicationTest.class,
ApplicationLogReaderTest.class,
ListCartridgesTest.class,
Added:
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/UserConfigurationTest.java
===================================================================
---
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/UserConfigurationTest.java
(rev 0)
+++
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/UserConfigurationTest.java 2011-10-03
14:07:20 UTC (rev 35282)
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2007 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.ide.eclipse.as.openshift.test.internal.core;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.jboss.ide.eclipse.as.openshift.core.OpenshiftException;
+import org.jboss.ide.eclipse.as.openshift.core.internal.utils.StreamUtils;
+import
org.jboss.ide.eclipse.as.openshift.test.internal.core.fakes.UserConfigurationFake;
+import org.junit.Test;
+
+/**
+ * @author André Dietisheim
+ */
+public class UserConfigurationTest {
+
+ private static final String USERNAME_REGEX = "[^=]+=(.+)";
+ private static final String USERNAME = "dummyUser";
+ private static final String ANOTHER_USERNAME = "anotherUser";
+
+ @Test
+ public void canReadUsername() throws OpenshiftException, IOException {
+ UserConfigurationFake userConfiguration = new UserConfigurationFake() {
+
+ @Override
+ protected void initFile(Writer writer) throws IOException {
+ writer.append(KEY_RHLOGIN).append('=').append(USERNAME).append('\n');
+ }
+
+ };
+ assertEquals(USERNAME, userConfiguration.getRhlogin());
+ }
+
+ @Test
+ public void canStoreUsername() throws OpenshiftException, IOException {
+ UserConfigurationFake userConfiguration = new UserConfigurationFake() {
+
+ @Override
+ protected void initFile(Writer writer) throws IOException {
+ writer.append(KEY_RHLOGIN).append('=').append(USERNAME).append('\n');
+ }
+
+ };
+ userConfiguration.setRhlogin(ANOTHER_USERNAME);
+ userConfiguration.store();
+ final File userConfigurationFile = userConfiguration.getFile();
+ assertNotNull(userConfigurationFile);
+ String fileContent = StreamUtils.readToString(new FileReader(userConfigurationFile));
+ Pattern pattern = Pattern.compile(USERNAME_REGEX);
+ Matcher matcher = pattern.matcher(fileContent);
+ assertTrue(matcher.matches());
+ assertEquals(1, matcher.groupCount());
+ assertEquals(ANOTHER_USERNAME, matcher.group(1));
+ }
+
+ @Test
+ public void canStoreAndReadUsername() throws OpenshiftException, IOException {
+ UserConfigurationFake userConfiguration = new UserConfigurationFake() {
+
+ @Override
+ protected void initFile(Writer writer) throws IOException {
+ writer.append(KEY_RHLOGIN).append('=').append(USERNAME).append('\n');
+ }
+
+ };
+ userConfiguration.setRhlogin(ANOTHER_USERNAME);
+ userConfiguration.store();
+ final File userConfigurationFile = userConfiguration.getFile();
+ assertNotNull(userConfigurationFile);
+ UserConfigurationFake userConfiguration2 = new UserConfigurationFake() {
+
+ @Override
+ protected File getUserConfigurationFile() throws OpenshiftException, IOException {
+ return userConfigurationFile;
+ }
+ };
+ assertEquals(ANOTHER_USERNAME, userConfiguration2.getRhlogin());
+ }
+}
Property changes on:
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/UserConfigurationTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/fakes/UserConfigurationFake.java
===================================================================
---
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/fakes/UserConfigurationFake.java
(rev 0)
+++
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/fakes/UserConfigurationFake.java 2011-10-03
14:07:20 UTC (rev 35282)
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * 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.ide.eclipse.as.openshift.test.internal.core.fakes;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+import org.jboss.ide.eclipse.as.openshift.core.OpenshiftException;
+import org.jboss.ide.eclipse.as.openshift.core.UserConfiguration;
+import org.jboss.ide.eclipse.as.openshift.core.internal.utils.StreamUtils;
+
+/**
+ * @author André Dietisheim
+ */
+public class UserConfigurationFake extends UserConfiguration {
+
+
+ public UserConfigurationFake() throws OpenshiftException, IOException {
+ super();
+ }
+
+ @Override
+ protected File getUserConfigurationFile() throws OpenshiftException, IOException {
+ Writer writer = null;
+ try {
+ File file = File.createTempFile(createRandomString(), null);
+ writer = new FileWriter(file);
+ initFile(writer);
+ return file;
+ } finally {
+ StreamUtils.close(writer);
+ }
+ }
+
+ protected void initFile(Writer writer) throws IOException {
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ private String createRandomString() {
+ return String.valueOf(System.currentTimeMillis());
+ }
+
+}
Property changes on:
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/fakes/UserConfigurationFake.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain