2016-07-17 10 views
0

私はJNDIを使ってDNSレコードを照会するプロジェクトを持っています。プロジェクト自体は大きく動作していますが、jUnitを使用してJNDI依存コンポーネントをテストする簡単で独立した方法を見つけることはできませんでした。JNDI DNSインターフェイスを模擬しています

コードはロケット科学とは異なり、典型的なバニラJNDI DNSリクエストによく似ています。

現在、私はテストユニットを公開DNSレコード(A、MX、TXTレコード)に向けていますが、これは一種のものです。

... 
    Hashtable env = new Hashtable(); 
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); 
    env.put("com.sun.jndi.dns.timeout.initial", timeOut); 
    env.put("com.sun.jndi.dns.timeout.retries", retries); 
    env.put("java.naming.provider.url", dns:); 
    } 

    Attributes attrs; 

    try { 
     DirContext ictx = new InitialDirContext(env); 
     attrs = ictx.getAttributes(queryInput, new String[]{queryType}); 
     return attrs; 
    } catch (NameNotFoundException e) { 
     getLogger().debug("Resolution for domain {} failed due to {}", new Object[]{queryInput, e}); 
     attrs = new BasicAttributes(queryType, "NXDOMAIN",true); 
     return attrs; 

TXTとA応答をJNDIに注入する方法はありますか?

答えて

0

これは、以下のサンプルコードで見られるように、1つは(Mockito.whenを使用してのような)伝統的なJNDIモック戦略を使用していますが、getAttributesメソッドに条件を爪することができますことが判明:

@Before 
public void setupTest() throws Exception { 
    this.queryDNS = new QueryDNS(); 
    this.queryDNSTestRunner = TestRunners.newTestRunner(queryDNS); 

    Hashtable env = new Hashtable<String, String>(); 
    env.put(Context.INITIAL_CONTEXT_FACTORY, FakeDNSInitialDirContextFactory.class.getName()); 

    this.queryDNS.initializeContext(env); 

    final DirContext mockContext = FakeDNSInitialDirContextFactory.getLatestMockContext(); 

    // Capture JNDI's getAttibutes method containing the (String) queryValue and (String[]) queryType 
    Mockito.when(mockContext.getAttributes(Mockito.anyString(), Mockito.any(String[].class))) 
      .thenAnswer(new Answer() { 
       public Object answer(InvocationOnMock invocation) throws Throwable { 
        // Craft a false DNS response 
        // Note the DNS response will not make use of any of the mocked 
        // query contents (all input is discarded and replies synthetically 
        // generated 
        return craftResponse(invocation); 
       } 
      }); 
} 

そして

// Dummy pseudo-DNS responder 
private Attributes craftResponse(InvocationOnMock invocation) { 
    Object[] arguments = invocation.getArguments(); 
    String querySubject = arguments[0].toString(); 
    String[] queryType = (String[]) arguments[1]; 

    // Create attribute 
    Attributes attrs = new BasicAttributes(true); 
    BasicAttribute attr; 

    switch (queryType[0]) { 
     case "AAAA": 
      attr = new BasicAttribute("AAAA"); 
      attrs.put(attr); 
      break; 
     case "TXT": 
      attr = new BasicAttribute("TXT", "666 | 123.123.123.123/32 | Apache-NIFI | AU | nifi.org | Apache NiFi"); 
      attrs.put(attr); 
      break; 
     case "PTR": 
      attr = new BasicAttribute("PTR"); 
      attr.add(0, "eg-apache.nifi.org."); 
      attr.add(1, "apache.nifi.org."); 
      attrs.put(attr); 
      break; 
    } 
    return attrs; 
} 

これは将来の人々に役立つことを願っています。

関連する問題