Added: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationhandlerImplTest.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationhandlerImplTest.java?rev=1513878&view=auto ============================================================================== --- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationhandlerImplTest.java (added) +++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationhandlerImplTest.java Wed Aug 14 13:37:47 2013 @@ -0,0 +1,80 @@ +/* + * 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.ace.agent.impl; + +import static org.easymock.EasyMock.expect; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.ace.agent.ConfigurationHandler; +import org.apache.ace.agent.IdentificationHandler; +import org.apache.ace.agent.testutil.BaseAgentTest; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +public class IdentificationhandlerImplTest extends BaseAgentTest { + + Map<String, String> m_configuration = new HashMap<String, String>(); + IdentificationHandler m_identificationHandler; + + @BeforeTest + public void setUpAgain() throws Exception { + AgentContext agentContext = addTestMock(AgentContext.class); + m_identificationHandler = new IdentificationHandlerImpl(agentContext); + ConfigurationHandler configurationHandler = addTestMock(ConfigurationHandler.class); + expect(configurationHandler.getMap()).andReturn(m_configuration).anyTimes(); + expect(agentContext.getConfigurationHandler()).andReturn(configurationHandler).anyTimes(); + replayTestMocks(); + } + + @AfterTest + public void tearDownAgain() throws Exception { + verifyTestMocks(); + } + + @Test + public void testAvailableIdentification() throws Exception { + m_configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, "qqq"); + assertEquals(m_identificationHandler.getIdentification(), "qqq"); + } + + @Test + public void testUpdatedIdentification() throws Exception { + m_configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, "qqq"); + assertEquals(m_identificationHandler.getIdentification(), "qqq"); + m_configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, "yyy"); + assertEquals(m_identificationHandler.getIdentification(), "yyy"); + } + + @Test + public void testNoIdentification() throws Exception { + m_configuration.clear(); + assertNull(m_identificationHandler.getIdentification()); + } + + @Test + public void testEmptyIdentification() throws Exception { + m_configuration.put(IdentificationHandlerImpl.IDENTIFICATION_CONFIG_KEY, " "); + assertNull(m_identificationHandler.getIdentification()); + } +}
Added: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java?rev=1513878&view=auto ============================================================================== --- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java (added) +++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseAgentTest.java Wed Aug 14 13:37:47 2013 @@ -0,0 +1,51 @@ +/* + * 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.ace.agent.testutil; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +import java.util.HashSet; +import java.util.Set; + +public class BaseAgentTest { + + Set<Object> m_mocks = new HashSet<Object>(); + + protected <T extends Object> T addTestMock(Class<T> clazz) { + T mock = createMock(clazz); + m_mocks.add(mock); + return mock; + } + + protected void replayTestMocks() { + for (Object mock : m_mocks) + replay(mock); + } + + protected void verifyTestMocks() { + for (Object mock : m_mocks) + verify(mock); + } + + protected void clearTestMocks() { + m_mocks.clear(); + } +} Added: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java?rev=1513878&view=auto ============================================================================== --- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java (added) +++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java Wed Aug 14 13:37:47 2013 @@ -0,0 +1,157 @@ +/* + * 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.ace.agent.testutil; + +import java.io.IOException; +import java.util.Date; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.Servlet; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.FilterHolder; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; + +public class TestWebServer { + + private Server m_server; + private ServletContextHandler m_contextHandler; + + public TestWebServer(int port, String contextPath, String basePath) throws Exception { + + m_server = new Server(port); + + m_contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); + m_contextHandler.setContextPath("/"); + + ServletHolder holder = new ServletHolder(new DefaultServlet()); + holder.setInitParameter("resourceBase", basePath); + holder.setInitParameter("pathInfoOnly", "true"); + holder.setInitParameter("acceptRanges", "true"); + holder.setInitParameter("dirAllowed", "true"); + + m_contextHandler.addFilter(new FilterHolder(new HttpDumpFilter()), "/*", null); + m_contextHandler.addServlet(holder, contextPath.concat(contextPath.endsWith("/") ? "*" : "/*")); + m_server.setHandler(m_contextHandler); + } + + public void start() throws Exception { + m_server.start(); + } + + public void stop() throws Exception { + m_server.stop(); + m_server.join(); + } + + public void addServlet(Servlet servlet, String pathPsec) { + m_contextHandler.addServlet(new ServletHolder(servlet), pathPsec); + } + + static class HttpDumpFilter implements Filter { + + @Override + public void init(FilterConfig arg0) throws ServletException { + } + + @Override + public void destroy() { + } + + @Override + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { + HttpServletRequest hreq = (HttpServletRequest) req; + HttpServletResponse hres = (HttpServletResponse) res; + + @SuppressWarnings("unchecked") + Enumeration<String> attrs = hreq.getHeaderNames(); + System.out.println("> " + hreq.getMethod() + " " + hreq.getRequestURI() + " " + req.getProtocol()); + while (attrs.hasMoreElements()) { + String attr = attrs.nextElement(); + System.out.println("> " + attr + ":" + hreq.getHeader(attr)); + } + ResponseInfoCollector coll = new ResponseInfoCollector(hres); + chain.doFilter(req, coll); + + // servlet API 3.0 + // System.out.println("< " + res.getStatus()); + // for (String headerName : res.getHeaderNames()) + // System.out.println("< " + headerName + ":" + res.getHeader(headerName)); + System.out.println("< " + coll.statusCode + " " + coll.statusMessage); + for (String headerName : coll.headers.keySet()) { + System.out.println("< " + headerName + ":" + coll.headers.get(headerName)); + } + } + } + + static class ResponseInfoCollector extends HttpServletResponseWrapper { + + long statusCode; + String statusMessage; + Map<String, String> headers = new HashMap<String, String>(); + + public ResponseInfoCollector(HttpServletResponse response) { + super(response); + } + + @Override + public void setHeader(String name, String value) { + headers.put(name, value); + super.setHeader(name, value); + } + + @Override + public void setDateHeader(String name, long date) { + headers.put(name, new Date(date).toString()); + super.setDateHeader(name, date); + } + + @Override + public void setIntHeader(String name, int value) { + headers.put(name, "" + value); + super.setIntHeader(name, value); + } + + @Override + public void setStatus(int sc, String sm) { + statusCode = sc; + statusMessage = sm; + super.setStatus(sc, sm); + } + + @Override + public void setStatus(int sc) { + statusCode = sc; + super.setStatus(sc); + } + } +}
