Junit for AccessControlUtils methods | Cannot read the array length because "<local4>" is null | Community
Skip to main content
Uppari_Ramesh
New Participant
June 30, 2023
Solved

Junit for AccessControlUtils methods | Cannot read the array length because "<local4>" is null

  • June 30, 2023
  • 4 replies
  • 3881 views

Hi Everyone,

 

In my junit how can I pass this below line 

JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path");

 

at this line my junit is  getting an exception, so I want to ignore or pass this line. How can I control above line in my junit?

 

java.lang.NullPointerException: Cannot read the array length because "<local4>" is null
at org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils.getAccessControlList(AccessControlUtils.java:138)
at

 

@tanika02 @aanchal-sikka @manvisharma @lukasz-m 

 

Thanks!!

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Uppari_Ramesh

SOLUTION:

 

Hi @tanika02  @salamswapnil @tarunkumar @kautuk_sahni ,@lukasz-m 

 

I am successfully able to pass the line JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path");

without getting an exception.

 

The code for passing above line without exception is :

 

@Mock
private AccessControlPolicyIterator accessControlPolicyIterator;

@Mock
private AccessControlManager accessControlManager;

@Mock
private AccessControlPolicy accessControlPolicy;

//To build policies
ImmutableList.Builder<AccessControlPolicy> policies = ImmutableList.builder();
//add mocked access Control Policy
policies.add(accessControlPolicy);
//build policy
List<AccessControlPolicy> l = policies.build();
//convert to array
AccessControlPolicy[] array= l.toArray(new AccessControlPolicy[0]);

//this is the behaviour for internal implementation of method getAccessControlList()
when(accessControlManager.getApplicablePolicies("path")).thenReturn(accessControlPolicyIterator);
when(accessControlPolicyIterator.hasNext()).thenReturn(true,false);
when(accessControlManager.getPolicies("path")).thenReturn(array);

 

 

Thanks for your time!!

4 replies

Uppari_Ramesh
Uppari_RameshAuthorAccepted solution
New Participant
June 30, 2023

SOLUTION:

 

Hi @tanika02  @salamswapnil @tarunkumar @kautuk_sahni ,@lukasz-m 

 

I am successfully able to pass the line JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path");

without getting an exception.

 

The code for passing above line without exception is :

 

@Mock
private AccessControlPolicyIterator accessControlPolicyIterator;

@Mock
private AccessControlManager accessControlManager;

@Mock
private AccessControlPolicy accessControlPolicy;

//To build policies
ImmutableList.Builder<AccessControlPolicy> policies = ImmutableList.builder();
//add mocked access Control Policy
policies.add(accessControlPolicy);
//build policy
List<AccessControlPolicy> l = policies.build();
//convert to array
AccessControlPolicy[] array= l.toArray(new AccessControlPolicy[0]);

//this is the behaviour for internal implementation of method getAccessControlList()
when(accessControlManager.getApplicablePolicies("path")).thenReturn(accessControlPolicyIterator);
when(accessControlPolicyIterator.hasNext()).thenReturn(true,false);
when(accessControlManager.getPolicies("path")).thenReturn(array);

 

 

Thanks for your time!!

TarunKumar
New Participant
June 30, 2023

Hi @uppari_ramesh ,

In path, can you trying passing the json resource that you might have loaded and also create specific nodes in the resource that you load. Lets see if that resolve your issue.

New Participant
June 30, 2023

@uppari_ramesh 

You can mock static methods with Mockito library. You need mockito-core and mockito-inline dependency for this.

@Mock private AccessControlManager mockedAccessControlManager; @Mock private JackrabbitAccessControlList acl; @Test void testMethod() { try (MockedStatic<AccessControlUtils> util = Mockito.mockStatic(AccessControlUtils.class)) { util.when(() -> AccessControlUtils.getAccessControlList(mockedAccessControlManager, "path")).thenReturn(acl); } }

 

Uppari_Ramesh
New Participant
June 30, 2023

Hi @salamswapnil , @tanika02 

 

I have tried the solution that you have proposed but still the control is going inside the actual method getAccessControlList() and it is not taking the mocked object. Getting 

 

java.lang.NullPointerException: Cannot invoke "javax.jcr.security.AccessControlPolicyIterator.hasNext()" because "itr" is null
at org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils.getAccessControlList(AccessControlUtils.java:129)

 

 

Note : I have added subsequent when and then conditions for conditions inside method getAccessControlList but getting 

 

java.lang.NullPointerException: Cannot read the array length because "<local4>" is null
at org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils.getAccessControlList(AccessControlUtils.java:138)
at

Tanika02
New Participant
June 30, 2023

Hello @uppari_ramesh - 

 

To handle the exception and pass or ignore the mentioned line in your JUnit test, you can use a try-catch block. Here's an example of how you can control the line in your JUnit:

 

 

 

@Test public void yourTestMethod() { try { JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(accessControlManager, "path"); // Rest of your test code that depends on 'acl' } catch (NullPointerException e) { // Handle the exception or ignore it // You can choose to leave this block empty if you want to ignore the exception } }

 

 

 

By wrapping the problematic line inside the try block, any exceptions that occur during its execution will be caught by the catch block. In this case, a NullPointerException is caught. You can handle the exception within the catch block by providing custom logic or leave it empty to simply ignore the exception and continue with the rest of the test.

Uppari_Ramesh
New Participant
June 30, 2023

Hi @tanika02 ,

 

Thanks for the reply! 

 

My requirement is different to solution that you have proposed. I want control above line junit file with when() and then().

 

Thanks!!

Tanika02
New Participant
June 30, 2023

Hello @uppari_ramesh - 

 

import org.junit.Test; import org.mockito.Mockito; public class YourTestClass { @Test public void yourTestMethod() { // Create a mock of the AccessControlUtils class AccessControlUtils accessControlUtilsMock = Mockito.mock(AccessControlUtils.class); // Define the behavior of the mock Mockito.when(accessControlUtilsMock.getAccessControlList(Mockito.any(), Mockito.anyString())) .thenReturn(null); // Use the mock in your test // This will now return null instead of throwing a NullPointerException JackrabbitAccessControlList acl = accessControlUtilsMock.getAccessControlList(accessControlManager, "path"); // Rest of your test code... } }