2016-07-17 3 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

는 아래의 샘플 코드와 같이 하나 (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; 
} 

앞으로 도움이되기를 바랍니다.