Example usage for java.lang.reflect Method setAccessible

List of usage examples for java.lang.reflect Method setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Method setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:com.adobe.acs.commons.email.EmailServiceImplTest.java

@Test
public final void testSendEmailAttachment() throws Exception {

    final String expectedMessage = "This is just a message";
    final String expectedSenderName = "John Smith";
    final String expectedSenderEmailAddress = "john@smith.com";
    String attachment = "This is a attachment.";
    String attachmentName = "attachment.txt";
    //Subject is provided inside the HtmlTemplate directly
    final String expectedSubject = "Greetings";

    final Map<String, String> params = new HashMap<String, String>();
    params.put("message", expectedMessage);
    params.put("senderName", expectedSenderName);
    params.put("senderEmailAddress", expectedSenderEmailAddress);

    final String recipient = "upasanac@acs.com";

    Map<String, DataSource> attachments = new HashMap();
    attachments.put(attachmentName, new ByteArrayDataSource(attachment, "text/plain"));

    ArgumentCaptor<HtmlEmail> captor = ArgumentCaptor.forClass(HtmlEmail.class);

    List<String> failureList = emailService.sendEmail(emailTemplateAttachmentPath, params, attachments,
            recipient);//from  ww w. j a  v  a2  s.  c o  m

    verify(messageGatewayHtmlEmail, times(1)).send(captor.capture());

    assertEquals(expectedSenderEmailAddress, captor.getValue().getFromAddress().getAddress());
    assertEquals(expectedSenderName, captor.getValue().getFromAddress().getPersonal());
    assertEquals(expectedSubject, captor.getValue().getSubject());
    assertEquals(recipient, captor.getValue().getToAddresses().get(0).toString());
    Method getContainer = captor.getValue().getClass().getSuperclass().getDeclaredMethod("getContainer");
    getContainer.setAccessible(true);
    MimeMultipart mimeMultipart = (MimeMultipart) getContainer.invoke(captor.getValue());
    getContainer.setAccessible(false);
    assertEquals(attachment, mimeMultipart.getBodyPart(0).getContent().toString());

    //If email is sent to the recipient successfully, the response is an empty failureList
    assertTrue(failureList.isEmpty());
}

From source file:com.onesignal.TrackGooglePurchase.java

void trackIAP() {
    if (mServiceConn == null) {
        mServiceConn = new ServiceConnection() {
            @Override// w  w  w.jav  a  2 s .c o  m
            public void onServiceDisconnected(ComponentName name) {
                iapEnabled = -99;
                mIInAppBillingService = null;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                try {
                    Class<?> stubClass = Class.forName("com.android.vending.billing.IInAppBillingService$Stub");
                    Method asInterfaceMethod = getAsInterfaceMethod(stubClass);

                    asInterfaceMethod.setAccessible(true);
                    mIInAppBillingService = asInterfaceMethod.invoke(null, service);

                    QueryBoughtItems();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        };

        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");

        appContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else if (mIInAppBillingService != null)
        QueryBoughtItems();
}

From source file:com.taobao.adfs.util.Utilities.java

public static void addResource(URLClassLoader classLoader, File file) throws IOException {
    try {//w w  w  . j  a  v  a 2s . c  om
        Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        addURL.setAccessible(true);
        addURL.invoke(classLoader, new Object[] { file.toURI().toURL() });
    } catch (Throwable t) {
        throw new IOException(t);
    }
}

From source file:com.app.test.mail.DefaultMailSenderTest.java

@Test
public void testPopulateContactMessage() throws Exception {
    Method populateContactMessage = _clazz.getDeclaredMethod("_populateContactMessage", String.class,
            String.class, Session.class);

    populateContactMessage.setAccessible(true);

    Message message = (Message) populateContactMessage.invoke(_classInstance, "user@test.com",
            "Sample contact message", _session);

    Assert.assertEquals("user@test.com", message.getFrom()[0].toString());
    Assert.assertEquals("You Have A New Message From user@test.com", message.getSubject());
    Assert.assertEquals("Sample contact message", message.getContent());
}

From source file:com.chiorichan.event.EventBus.java

private HandlerList getEventListeners(Class<? extends Event> type) {
    try {// w ww .j a v a  2s. c  om
        Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList");
        method.setAccessible(true);
        return (HandlerList) method.invoke(null);
    } catch (Exception e) {
        throw new IllegalCreatorAccessException(e.toString());
    }
}

From source file:com.app.test.mail.DefaultMailSenderTest.java

@Test
public void testPopulateAccountDeletionMessage() throws Exception {
    _initializeVelocityTemplate(_clazz, _classInstance);

    Method populateMessage = _clazz.getDeclaredMethod("_populateMessage", String.class, String.class,
            String.class, Session.class);

    populateMessage.setAccessible(true);

    Message message = (Message) populateMessage.invoke(_classInstance, "user@test.com",
            "Account Deletion Successful", "account_deletion_email.vm", _session);

    Assert.assertEquals("Auction Alert <test@test.com>", message.getFrom()[0].toString());
    Assert.assertEquals("Account Deletion Successful", message.getSubject());
    Assert.assertEquals(_ACCOUNT_DELETION_EMAIL, message.getContent());

    InternetAddress[] internetAddresses = new InternetAddress[1];

    internetAddresses[0] = new InternetAddress("user@test.com");

    Assert.assertArrayEquals(internetAddresses, message.getRecipients(Message.RecipientType.TO));
}

From source file:com.app.test.mail.DefaultMailSenderTest.java

@Test
public void testPopulateCancellationMessage() throws Exception {
    _initializeVelocityTemplate(_clazz, _classInstance);

    Method populateMessage = _clazz.getDeclaredMethod("_populateMessage", String.class, String.class,
            String.class, Session.class);

    populateMessage.setAccessible(true);

    Message message = (Message) populateMessage.invoke(_classInstance, "user@test.com",
            "Cancellation Successful", "cancellation_email.vm", _session);

    Assert.assertEquals("Auction Alert <test@test.com>", message.getFrom()[0].toString());
    Assert.assertEquals("Cancellation Successful", message.getSubject());
    Assert.assertEquals(_CANCELLATION_EMAIL, message.getContent());

    InternetAddress[] internetAddresses = new InternetAddress[1];

    internetAddresses[0] = new InternetAddress("user@test.com");

    Assert.assertArrayEquals(internetAddresses, message.getRecipients(Message.RecipientType.TO));
}

From source file:com.app.test.mail.DefaultMailSenderTest.java

@Test
public void testPopulateCardDetailsMessage() throws Exception {
    _initializeVelocityTemplate(_clazz, _classInstance);

    Method populateMessage = _clazz.getDeclaredMethod("_populateMessage", String.class, String.class,
            String.class, Session.class);

    populateMessage.setAccessible(true);

    Message message = (Message) populateMessage.invoke(_classInstance, "user@test.com", "Card Details Updated",
            "card_details_email.vm", _session);

    Assert.assertEquals("Auction Alert <test@test.com>", message.getFrom()[0].toString());
    Assert.assertEquals("Card Details Updated", message.getSubject());
    Assert.assertEquals(_CARD_DETAILS_EMAIL, message.getContent());

    InternetAddress[] internetAddresses = new InternetAddress[1];

    internetAddresses[0] = new InternetAddress("user@test.com");

    Assert.assertArrayEquals(internetAddresses, message.getRecipients(Message.RecipientType.TO));
}

From source file:com.app.test.mail.DefaultMailSenderTest.java

@Test
public void testPopulatePaymentFailedMessage() throws Exception {
    _initializeVelocityTemplate(_clazz, _classInstance);

    Method populateMessage = _clazz.getDeclaredMethod("_populateMessage", String.class, String.class,
            String.class, Session.class);

    populateMessage.setAccessible(true);

    Message message = (Message) populateMessage.invoke(_classInstance, "user@test.com", "Payment Failed",
            "payment_failed_email.vm", _session);

    Assert.assertEquals("Auction Alert <test@test.com>", message.getFrom()[0].toString());
    Assert.assertEquals("Payment Failed", message.getSubject());
    Assert.assertEquals(_PAYMENT_FAILED_EMAIL, message.getContent());

    InternetAddress[] internetAddresses = new InternetAddress[1];

    internetAddresses[0] = new InternetAddress("user@test.com");

    Assert.assertArrayEquals(internetAddresses, message.getRecipients(Message.RecipientType.TO));
}

From source file:com.app.test.mail.DefaultMailSenderTest.java

@Test
public void testPopulateResubscribeMessage() throws Exception {
    _initializeVelocityTemplate(_clazz, _classInstance);

    Method populateMessage = _clazz.getDeclaredMethod("_populateMessage", String.class, String.class,
            String.class, Session.class);

    populateMessage.setAccessible(true);

    Message message = (Message) populateMessage.invoke(_classInstance, "user@test.com",
            "Resubscribe Successful", "resubscribe_email.vm", _session);

    Assert.assertEquals("Auction Alert <test@test.com>", message.getFrom()[0].toString());
    Assert.assertEquals("Resubscribe Successful", message.getSubject());
    Assert.assertEquals(_RESUBSCRIBE_EMAIL, message.getContent());

    InternetAddress[] internetAddresses = new InternetAddress[1];

    internetAddresses[0] = new InternetAddress("user@test.com");

    Assert.assertArrayEquals(internetAddresses, message.getRecipients(Message.RecipientType.TO));
}