List of usage examples for java.lang.reflect Method setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.app.test.mail.DefaultMailSenderTest.java
@Test public void testPopulateWelcomeMessage() 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", "Welcome", "welcome_email.vm", _session); Assert.assertEquals("Auction Alert <test@test.com>", message.getFrom()[0].toString()); Assert.assertEquals("Welcome", message.getSubject()); Assert.assertEquals(_WELCOME_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:org.echocat.redprecursor.annotations.utils.AccessAlsoProtectedMembersReflectivePropertyAccessor.java
@Override protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) { Method result = null;//ww w .j a va 2 s. co m final PropertyDescriptor propertyDescriptor = findPropertyDescriptorFor(clazz, propertyName); if (propertyDescriptor != null) { result = propertyDescriptor.getReadMethod(); } if (result == null) { Class<?> current = clazz; final String getterName = "get" + StringUtils.capitalize(propertyName); while (result == null && !Object.class.equals(current)) { try { final Method potentialMethod = current.getDeclaredMethod(getterName); if (!mustBeStatic || Modifier.isStatic(potentialMethod.getModifiers())) { if (!potentialMethod.isAccessible()) { potentialMethod.setAccessible(true); } result = potentialMethod; } } catch (NoSuchMethodException ignored) { } current = current.getSuperclass(); } } return result; }
From source file:com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolderTest.java
@Test public void testGetAnnotationValuesWrongArg() throws ReflectiveOperationException { SuppressWarningsHolder holder = new SuppressWarningsHolder(); Method getAllAnnotationValues = holder.getClass().getDeclaredMethod("getAnnotationValues", DetailAST.class); getAllAnnotationValues.setAccessible(true); DetailAST methodDef = new DetailAST(); methodDef.setType(TokenTypes.METHOD_DEF); methodDef.setText("Method Def"); methodDef.setLineNo(0);//from www. j a v a2 s .com methodDef.setColumnNo(0); try { getAllAnnotationValues.invoke(holder, methodDef); fail("Exception expected"); } catch (InvocationTargetException ex) { assertTrue(ex.getCause() instanceof IllegalArgumentException); assertEquals("Expression or annotation array" + " initializer AST expected: Method Def[0x0]", ex.getCause().getMessage()); } }
From source file:com.app.test.mail.SendGridMailSenderTest.java
@Test public void testPopulateEmailMessage() throws Exception { _initializeVelocityTemplate(_clazz, _classInstance); Method populateEmailMessageMethod = _clazz.getDeclaredMethod("_populateEmailMessage", Map.class, String.class, String.class); populateEmailMessageMethod.setAccessible(true); List<SearchResult> searchResults = new ArrayList<>(); SearchQuery searchQuery = new SearchQuery(1, _USER_ID, "Test keywords"); SearchResult searchResult = new SearchResult(1, "1234", "itemTitle", "$14.99", "$29.99", "http://www.ebay.com/itm/1234", "http://www.ebay.com/123.jpg"); searchResults.add(searchResult);/*from w w w . ja v a2s .c o m*/ Map<SearchQuery, List<SearchResult>> searchQueryResultMap = new HashMap<>(); searchQueryResultMap.put(searchQuery, searchResults); Mail mail = (Mail) populateEmailMessageMethod.invoke(_classInstance, searchQueryResultMap, "user@test.com", "unsubscribeToken"); Assert.assertTrue(mail.getSubject().contains("New Search Results - ")); List<Content> mailContent = mail.getContent(); Content content = mailContent.get(0); Assert.assertEquals("text/html", content.getType()); Assert.assertEquals(_EMAIL_CONTENT, content.getValue()); }
From source file:com.egreen.tesla.server.api.component.Component.java
public void LoadClass() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class sysClass = URLClassLoader.class; Method sysMethod = sysClass.getDeclaredMethod("addURL", new Class[] { URL.class }); sysMethod.setAccessible(true); sysMethod.invoke(sysLoader, new Object[] { jarFile }); // for (Object object : classeNames) { // Class classFromName = sysLoader.loadClass(object + ""); // LOGGER.info(classFromName.getSimpleName()); // component.setControllers(object + "", sysClass); // }/*w w w . j a v a 2s . co m*/ }
From source file:com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolderTest.java
@Test public void testGetAnnotationTargetWrongArg() throws ReflectiveOperationException { SuppressWarningsHolder holder = new SuppressWarningsHolder(); Method getAnnotationTarget = holder.getClass().getDeclaredMethod("getAnnotationTarget", DetailAST.class); getAnnotationTarget.setAccessible(true); DetailAST methodDef = new DetailAST(); methodDef.setType(TokenTypes.METHOD_DEF); methodDef.setText("Method Def"); DetailAST parent = new DetailAST(); parent.setType(TokenTypes.ASSIGN);// w ww . java2s . com parent.setText("Parent ast"); parent.addChild(methodDef); parent.setLineNo(0); parent.setColumnNo(0); try { getAnnotationTarget.invoke(holder, methodDef); fail("Exception expected"); } catch (InvocationTargetException ex) { assertTrue(ex.getCause() instanceof IllegalArgumentException); assertEquals("Unexpected container AST: Parent ast[0x0]", ex.getCause().getMessage()); } }
From source file:com.shazam.dataengineering.pipelinebuilder.DeploymentActionTest.java
@Test(expected = InvocationTargetException.class) @WithoutJenkins// w w w .j a v a2 s . c om public void failingS3DeploymentShouldThrowDeploymentException() throws Exception { testFolder.newFolder("scripts"); testFolder.newFile("scripts/script.pig"); HashMap<S3Environment, String> s3Urls = new HashMap<S3Environment, String>(); s3Urls.put(new S3Environment("test.json", "script.pig"), "s3://bucket/"); DeploymentAction action = new DeploymentAction(getMockAbstractBuild(), s3Urls, new AnonymousAWSCredentials()); Field pipelineFileField = action.getClass().getDeclaredField("pipelineFile"); pipelineFileField.setAccessible(true); pipelineFileField.set(action, "test.json"); Method method = action.getClass().getDeclaredMethod("deployScriptsToS3"); method.setAccessible(true); method.invoke(action); }
From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java
@Test public void testBuildNodes() throws Exception { Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class; Method method = class1.getDeclaredMethod("buildNodes", new Class[] { JsonNode.class }); method.setAccessible(true); List<PhysicalNode> result = new ArrayList<PhysicalNode>(); JsonNode nodesRoot = mock(JsonNode.class); JsonNode nodes = mock(JsonNode.class); JsonNode node = mock(JsonNode.class); JsonNode node_temp_buildnode = mock(JsonNode.class); JsonNode ports = mock(JsonNode.class); JsonNode attributes = mock(JsonNode.class); JsonNode port = mock(JsonNode.class); JsonNode port_temp_buildport = mock(JsonNode.class); JsonNode portAttributes = mock(JsonNode.class); JsonNode portAttribute = mock(JsonNode.class); JsonNode port_temp_buildPortAttribute = mock(JsonNode.class); JsonNode portAttribute_father = mock(JsonNode.class); JsonNode attribute_temp_father = mock(JsonNode.class); when(nodesRoot.path(any(String.class))).thenReturn(nodes); when(nodes.size()).thenReturn(1);//from w w w . j a v a2 s. com when(nodes.get(any(Integer.class))).thenReturn(node); //get into method "build Node" when(node.get(any(String.class))).thenReturn(node_temp_buildnode); when(node_temp_buildnode.asText()).thenReturn(new String(""))//test null .thenReturn(new String("test"))//node id .thenReturn(new String("switch"));// node type result = (List<PhysicalNode>) method.invoke(physicalResourceLoader, nodesRoot); Assert.assertTrue(result.size() == 0); verify(node_temp_buildnode).asText(); when(node.path(any(String.class))).thenReturn(ports)//get into method "build ports" .thenReturn(attributes); //get into method "build attributes" ////get into method"build ports" when(ports.size()).thenReturn(1); when(ports.get(any(Integer.class))).thenReturn(port); //////get into method "build port" when(port.get(any(String.class))).thenReturn(port_temp_buildport); when(port_temp_buildport.asText()).thenReturn(new String("test"))//port id .thenReturn(new String("external"))//port type .thenReturn(new String("00:11:22:33:44:55"))//if(!(port.get("port-mac-address").asText().equals(""))) get in .thenReturn(new String("00:11:22:33:44:55"))//mac address .thenReturn(new String(""));// if(port.get("bandwidth").asText().equals("")) get in when(port.path(any(String.class))).thenReturn(portAttributes); ////////get into method "buildPortAttributes" args(portAttributes) when(portAttributes.size()).thenReturn(1); when(portAttributes.get(any(Integer.class))).thenReturn(portAttribute); ////////// get into method "buildPortAttribute" args(portAttribute) when(portAttribute.path(any(String.class))).thenReturn(port_temp_buildPortAttribute); when(port_temp_buildPortAttribute.asText()).thenReturn(new String("test"))//ATTRIBUTE_NAME .thenReturn(new String("test"));//ATTRIBUTE_VALUE ////return to method "buildnode" and get into method "..............buildNodeAttributes" when(attributes.size()).thenReturn(1); when(attributes.get(any(Integer.class))).thenReturn(portAttribute_father); //////get into method "..............buildNodeAttribute" when(portAttribute_father.path(any(String.class))).thenReturn(attribute_temp_father); when(attribute_temp_father.asText()).thenReturn(new String("test"))//ATTRIBUTE_NAME .thenReturn(new String("test"));//ATTRIBUTE_VALUE result = (List<PhysicalNode>) method.invoke(physicalResourceLoader, nodesRoot); //return empty list Assert.assertTrue(result.size() == 1); verify(port_temp_buildport, times(5)).asText(); verify(attribute_temp_father, times(2)).asText(); }
From source file:de.thkwalter.et.schlupfbezifferung.SchlupfbezifferungControllerTest.java
/** * Test der Methode {@link SchlupfbezifferungController#inversionszentrumBerechnen}. * /*from www . j a va 2s . c om*/ * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException */ @Test public void testInversionszentrumBerechnen() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Die zu testende Methode wird aufgerufen. Method methode = SchlupfbezifferungController.class.getDeclaredMethod("inversionszentrumBerechnen", (Class<?>[]) null); methode.setAccessible(true); Vector2D inversionszentrum = (Vector2D) methode.invoke(this.schlupfbezifferungController, (Object[]) null); // Es wird berprft, ob das Inversionszentrum korrekt berechnet worden ist. assertEquals(9.257, inversionszentrum.getX(), 9.257 / 1000.0); assertEquals(-1.339, inversionszentrum.getY(), 1.339 / 1000.0); }
From source file:de.thkwalter.et.schlupfbezifferung.SchlupfbezifferungControllerTest.java
/** * Test der Methode {@link SchlupfbezifferungController#drehpunktSchlupfgeradeBerechnen()}. * /*from w w w . j a v a 2 s . c o m*/ * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException */ @Test public void testDrehpunktSchlupfgeradeBerechnen() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Die zu testende Methode wird aufgerufen. Method methode = SchlupfbezifferungController.class.getDeclaredMethod("drehpunktSchlupfgeradeBerechnen", (Class<?>[]) null); methode.setAccessible(true); Vector2D drehpunktSchlupfgerade = (Vector2D) methode.invoke(this.schlupfbezifferungController, (Object[]) null); // Es wird berprft, ob das Inversionszentrum korrekt berechnet worden ist. assertEquals(6.077, drehpunktSchlupfgerade.getX(), 6.077 / 1000.0); assertEquals(-2.656, drehpunktSchlupfgerade.getY(), 2.656 / 1000.0); }