001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.test;
018
019import java.net.URI;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.camel.Endpoint;
024import org.apache.camel.impl.UriEndpointComponent;
025import org.apache.camel.util.CamelContextHelper;
026import org.apache.camel.util.ObjectHelper;
027import org.apache.camel.util.UnsafeUriCharactersEncoder;
028
029/**
030 * The <a href="http://camel.apache.org/test.html">Test Component</a> is for simplifying unit and integration tests.
031 *
032 * Component for testing by polling test messages from another endpoint on startup as the expected message bodies to
033 * receive during testing.
034 *
035 * @version 
036 */
037public class TestComponent extends UriEndpointComponent {
038
039    public TestComponent() {
040        super(TestEndpoint.class);
041    }
042
043    public Endpoint createEndpoint(String uri) throws Exception {
044        // lets not use the normal parameter handling so that all parameters are sent to the nested endpoint
045
046        ObjectHelper.notNull(getCamelContext(), "camelContext");
047        URI u = new URI(UnsafeUriCharactersEncoder.encode(uri));
048        String path = u.getSchemeSpecificPart();
049        if (path.startsWith("//")) {
050            path = path.substring(2);
051        }
052
053        return createEndpoint(uri, path, new HashMap<String, Object>());
054    }
055
056    @Override
057    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
058        Long timeout = getAndRemoveParameter(parameters, "timeout", Long.class);
059        Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(getCamelContext(), remaining);
060
061        TestEndpoint answer = new TestEndpoint(uri, this, endpoint);
062        if (timeout != null) {
063            answer.setTimeout(timeout);
064        }
065        return answer;
066    }
067
068}