View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.datacollection.adaptor;
19  
20  import junit.framework.TestCase;
21  import org.apache.hadoop.chukwa.datacollection.ChunkReceiver;
22  import org.apache.hadoop.chukwa.datacollection.agent.AdaptorManager;
23  import org.apache.hadoop.chukwa.*;
24  import java.net.*;
25  
26  
27  public class TestSyslogAdaptor extends TestCase implements ChunkReceiver {
28    volatile boolean receivedOK = false;
29    String STR = "<142>Syslog formatted message.";
30    
31    /**
32     * Test Sending syslog message through port 9095.
33     * @throws Exception
34     */
35    public void testSyslog() throws Exception {
36      SyslogAdaptor u = new SyslogAdaptor();
37      u.parseArgs("Test", "9095", AdaptorManager.NULL);
38      u.start("id", "Test", 0, this);
39      
40      DatagramSocket send = new DatagramSocket();
41      byte[] buf = STR.getBytes();
42      DatagramPacket p = new DatagramPacket(buf, buf.length);
43      p.setSocketAddress(new InetSocketAddress("127.0.0.1",u.portno));
44      send.send(p);
45      
46      synchronized(this) {
47        wait(1000);
48      }
49      assertTrue(receivedOK);
50    }
51    
52    /**
53     * Test Facility name overwrite from LOCAL1 to HADOOP.
54     */
55    public void add(Chunk c) {
56      System.out.print(c.getDataType());
57      assertTrue(c.getDataType().equals("HADOOP"));
58      assertEquals(c.getSeqID(), c.getData().length);
59      assertTrue(STR.equals(new String(c.getData())));
60      receivedOK= true;
61      synchronized(this) {
62        notify();
63      }
64     }
65  
66  }