2011-06-23 27 views
1

私はApache Camelで最初のコードを書こうとしています。私はCamel in Actionの例に従おうとしますが、自分のサンプルデータを使用したいと思います。私は、CSVファイルから読み込まれ、Java Beanとして各行を取得したい私は今 Apache CamelとBindyを使用してCSVからBeanにアンマーシャリングする方法は?

をやりたい

。ここで

は、私のJUnitテストです:

@Test 
public void testCsvWithBindy() throws Exception { 
    MockEndpoint mock = getMockEndpoint("mock:queue.csv"); 
    mock.expectedMessageCount(2); 

    assertMockEndpointsSatisfied(); 

    CsvBean line1 = mock.getReceivedExchanges().get(0).getIn() 
      .getBody(CsvBean.class); 
    assertEquals("row 01", line1.getFirst()); 
} 

public RouteBuilder createRoute() { 
    return new RouteBuilder() { 
     public void configure() throws Exception { 
      context.setTracing(true); 

      from("file://src/test/resources?noop=true&fileName=test.csv") 
       .unmarshal().bindy(BindyType.Csv, "my.package.for.csvrecord") 
       .to("mock:queue.csv"); 
     } 
    }; 
} 

CSVは、この含まれています

row 01,row 02,,row 04 
row 11, row 12, row 13, row 14 

をそしてこれが私のCsvRecordです:

@CsvRecord(separator = ",") 
public class CsvBean { 
@DataField(pos = 1) 
private String first; 
@DataField(pos = 2) 
private String second; 
@DataField(pos = 3) 
private String third; 
@DataField(pos = 4) 
private String fourth; 

public String getFirst() { 
    return first; 
} 

public void setFirst(String first) { 
    this.first = first; 
} 

public String getSecond() { 
    return second; 
} 

public void setSecond(String second) { 
    this.second = second; 
} 

public String getThird() { 
    return third; 
} 

public void setThird(String third) { 
    this.third = third; 
} 

public String getFourth() { 
    return fourth; 
} 

public void setFourth(String fourth) { 
    this.fourth = fourth; 
} 
} 

私の問題

Iこのテストを実行するコンテキストが開始され、ルートがロードされます。しかし、何も通っていない。約10秒後、コンテキストは自動的に停止し、テストは失敗します。これは、スタックトレースです:

java.lang.AssertionError: mock://queue.csv Received message count. Expected: <2> but was: <0> 
at org.apache.camel.component.mock.MockEndpoint.fail(MockEndpoint.java:1086) 
at org.apache.camel.component.mock.MockEndpoint.assertEquals(MockEndpoint.java:1068) 
at org.apache.camel.component.mock.MockEndpoint.doAssertIsSatisfied(MockEndpoint.java:367) 
at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:346) 
at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:334) 
at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:172) 
at org.apache.camel.test.junit4.CamelTestSupport.assertMockEndpointsSatisfied(CamelTestSupport.java:391) 
at my.package.for.unittests.CsvToBeanWithBindyTest.testCsvWithBindy(CsvToBeanWithBindyTest.java:20) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 

は私が明らかに何か、私のCsvRecordや私のルートにテスト・セットアップとそれほどではないに関係しているかもしれない何かが欠けていると思います

とが必要です。より良いチュートリアルのヒントやURLを教えてもらえますか? 。本はここに;-)右の私の質問を投稿した後、私は自分自身を答えを見つけ、再び:-(...

答えて

2

この時点で非常に有用ではない作業JUnitテストです:

public class CsvToBeanWithBindyTest extends CamelTestSupport { 
@Test 
public void testCsv() throws Exception { 
    MockEndpoint mock = getMockEndpoint("mock:queue.csv"); 
    mock.expectedMessageCount(1); 

    assertMockEndpointsSatisfied(); 

    List line1 = (List) mock.getReceivedExchanges().get(0).getIn() 
      .getBody(); 
    Map map1 = (Map) line1.get(0); 
    CsvBean csv1 = (CsvBean) map1.get("my.package.CsvBean"); 
    assertEquals("row 01", csv1.getFirst()); 

    Map map2 = (Map) line1.get(1); 
    CsvBean csv2 = (CsvBean) map2.get("my.package.CsvBean"); 
    assertEquals("row 11", csv2.getFirst()); 
} 

@Override 
protected RouteBuilder createRouteBuilder() throws Exception { 
    return new RouteBuilder() { 
     @Override 
     public void configure() throws Exception { 
      context.setTracing(true); 

      from("file://src/test/resources?noop=true&fileName=test.csv") 
       .unmarshal(new BindyCsvDataFormat("my.package")) 
       .to("mock:queue.csv"); 
     } 
    }; 
} 
} 

私にとって予期せぬことは、Listを私のエンドポイントルートから得て、多くはMapを保持していることです。各マップにはキーmy.package.MyBeanClassがあり、値はCSVファイルの実際の非整列行に設定されています。

+0

マップの問題を回避したことがありますか?私はマップでもあります。ここではBeanのArrayListではなくBeanを抽出する必要がありますか?このページ(http://camel.apache.org/bindy.html)はマップを参照していますが、それがなぜそこにあるのかについての十分な説明を与えていませんか? – Clarkey

+0

申し訳ありませんが、地図上の更新情報はありません。私はBindyが動作する方法としてそれを受け入れました: "これは、各行が複数のオブジェクトに対応できるという理由からです。これは、1行に1つのオブジェクトが返されると予想すると混乱することがあります。 (URLから) – cringe

+0

こんにちは、私はまた、オブジェクトの変換には、同じCSVを行っていたが、私はint型またはIntegerデータ型の不正な引数の例外に直面しています。同じhttp://stackoverflow.com/questions/20627864/apache-camelbindy-illegal-argument-exceptionで私の質問をチェックしてください – vashishth

関連する問題