http://git-wip-us.apache.org/repos/asf/karaf/blob/d33e0955/config/src/test/java/org/apache/karaf/config/command/EditCommandTest.java ---------------------------------------------------------------------- diff --git a/config/src/test/java/org/apache/karaf/config/command/EditCommandTest.java b/config/src/test/java/org/apache/karaf/config/command/EditCommandTest.java new file mode 100644 index 0000000..09dc000 --- /dev/null +++ b/config/src/test/java/org/apache/karaf/config/command/EditCommandTest.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.karaf.config.command; + +import java.util.Dictionary; +import java.util.Properties; + +import junit.framework.TestCase; +import org.apache.karaf.config.core.impl.ConfigRepositoryImpl; +import org.apache.karaf.shell.api.console.Session; +import org.osgi.framework.BundleContext; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; + +/** + * Test cases for {@link EditCommand} + */ +public class EditCommandTest extends TestCase { + + private static final String PID = "my.test.persistent.id"; + + private EditCommand command; + private BundleContext context; + private ConfigurationAdmin admin; + private Session session; + + @Override + protected void setUp() throws Exception { + command = new EditCommand(); + + admin = createMock(ConfigurationAdmin.class); + command.setConfigRepository(new ConfigRepositoryImpl(admin)); + + session = new MockCommandSession(); + command.setSession(session); + } + + public void testExecuteOnExistingPid() throws Exception { + Configuration config = createMock(Configuration.class); + expect(admin.getConfiguration(PID)).andReturn(config); + replay(admin); + + // the ConfigAdmin service returns a Dictionary for an existing PID + Dictionary props = new Properties(); + expect(config.getProperties()).andReturn(props); + replay(config); + + command.pid = PID; + command.execute(); + + // the PID and Dictionary should have been set on the session + assertEquals("The PID should be set on the session", + PID, session.get(ConfigCommandSupport.PROPERTY_CONFIG_PID)); + assertSame("The Dictionary returned by the ConfigAdmin service should be set on the session", + props, session.get(ConfigCommandSupport.PROPERTY_CONFIG_PROPS)); + } + + @SuppressWarnings("rawtypes") + public void testExecuteOnNewPid() throws Exception { + Configuration config = createMock(Configuration.class); + expect(admin.getConfiguration(PID)).andReturn(config); + replay(admin); + + // the ConfigAdmin service does not return a Dictionary for a new PID + expect(config.getProperties()).andReturn(null); + replay(config); + + command.pid = PID; + command.execute(); + + // the PID and an empty Dictionary should have been set on the session + assertEquals("The PID should be set on the session", + PID, session.get(ConfigCommandSupport.PROPERTY_CONFIG_PID)); + Dictionary props = (Dictionary) session.get(ConfigCommandSupport.PROPERTY_CONFIG_PROPS); + assertNotNull("Should have a Dictionary on the session", props); + assertTrue("Should have an empty Dictionary on the session", props.isEmpty()); + } + +}
http://git-wip-us.apache.org/repos/asf/karaf/blob/d33e0955/config/src/test/java/org/apache/karaf/config/command/MockCommandSession.java ---------------------------------------------------------------------- diff --git a/config/src/test/java/org/apache/karaf/config/command/MockCommandSession.java b/config/src/test/java/org/apache/karaf/config/command/MockCommandSession.java new file mode 100644 index 0000000..7fa430b --- /dev/null +++ b/config/src/test/java/org/apache/karaf/config/command/MockCommandSession.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.karaf.config.command; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.HashMap; +import java.util.Map; + +import org.apache.karaf.shell.api.console.History; +import org.apache.karaf.shell.api.console.Registry; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.api.console.SessionFactory; +import org.apache.karaf.shell.api.console.Terminal; + +/* + * A mock CommandSession implementation that only cares about the properties set on the session + */ +class MockCommandSession implements Session { + + private Map<String, Object> properties = new HashMap<String, Object>(); + + public void close() { + // not implemented + } + + public Object execute(CharSequence commandline) throws Exception { + // not implemented + return null; + } + + public Object get(String name) { + return properties.get(name); + } + + public PrintStream getConsole() { + // not implemented + return null; + } + + public InputStream getKeyboard() { + // not implemented + return null; + } + + public void put(String name, Object value) { + properties.put(name, value); + } + + @Override + public String readLine(String prompt, Character mask) throws IOException { + return null; + } + + @Override + public Terminal getTerminal() { + return null; + } + + @Override + public History getHistory() { + return null; + } + + @Override + public Registry getRegistry() { + return null; + } + + @Override + public SessionFactory getFactory() { + return null; + } + + @Override + public String resolveCommand(String name) { + return null; + } + + @Override + public void run() { + + } +} http://git-wip-us.apache.org/repos/asf/karaf/blob/d33e0955/config/src/test/java/org/apache/karaf/config/command/UpdateCommandTest.java ---------------------------------------------------------------------- diff --git a/config/src/test/java/org/apache/karaf/config/command/UpdateCommandTest.java b/config/src/test/java/org/apache/karaf/config/command/UpdateCommandTest.java new file mode 100644 index 0000000..e3f58b5 --- /dev/null +++ b/config/src/test/java/org/apache/karaf/config/command/UpdateCommandTest.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.karaf.config.command; + + +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; + +import java.util.Dictionary; +import java.util.Hashtable; + +import junit.framework.TestCase; + +import org.apache.karaf.config.core.ConfigRepository; +import org.easymock.EasyMock; + +/** + * Test cases for {@link EditCommand} + */ +public class UpdateCommandTest extends TestCase { + + private static final String FACTORY_PID = "myFactoryPid"; + private static final String PID = "myPid"; + + public void testupdateRegularConfig() throws Exception { + Dictionary<String, Object> props = new Hashtable<String, Object>(); + + UpdateCommand command = new UpdateCommand(); + ConfigRepository configRepo = EasyMock.createMock(ConfigRepository.class); + configRepo.update(EasyMock.eq(PID), EasyMock.eq(props)); + EasyMock.expectLastCall(); + command.setConfigRepository(configRepo); + + MockCommandSession session = createMockSessionForFactoryEdit(PID, false, props); + command.setSession(session); + replay(configRepo); + + command.execute(); + EasyMock.verify(configRepo); + } + + public void testupdateOnNewFactoryPid() throws Exception { + Dictionary<String, Object> props = new Hashtable<String, Object>(); + + UpdateCommand command = new UpdateCommand(); + ConfigRepository configRepo = EasyMock.createMock(ConfigRepository.class); + expect(configRepo.createFactoryConfiguration(EasyMock.eq(FACTORY_PID), EasyMock.eq(props))) + .andReturn(PID + ".35326647"); + command.setConfigRepository(configRepo); + + MockCommandSession session = createMockSessionForFactoryEdit(FACTORY_PID, true, props); + command.setSession(session); + replay(configRepo); + + command.execute(); + EasyMock.verify(configRepo); + } + + private MockCommandSession createMockSessionForFactoryEdit(String pid, boolean isFactory, + Dictionary<String, Object> props) { + MockCommandSession session = new MockCommandSession(); + session.put(ConfigCommandSupport.PROPERTY_CONFIG_PID, pid); + session.put(ConfigCommandSupport.PROPERTY_FACTORY, isFactory); + session.put(ConfigCommandSupport.PROPERTY_CONFIG_PROPS, props); + return session; + } + +}