Java 类org.mockito.Answers 实例源码

项目:NioSmtpClient    文件:SmtpSessionTest.java   
private SslHandler getSslHandler() throws Exception {
  // get SslHandler if it was added to the pipeline
  ArgumentCaptor<ChannelHandler> captor = ArgumentCaptor.forClass(ChannelHandler.class);
  verify(pipeline).addFirst(captor.capture());
  SslHandler sslHandler = (SslHandler) captor.getValue();

  // mock and store the context so we can get the handshake future
  ChannelHandlerContext context = mock(ChannelHandlerContext.class);
  when(context.executor()).thenReturn(ImmediateEventExecutor.INSTANCE);
  when(context.channel()).thenReturn(mock(Channel.class, Answers.RETURNS_MOCKS.get()));

  // add the handler but prevent the handshake from running automatically
  when(channel.isActive()).thenReturn(false);
  sslHandler.handlerAdded(context);

  return sslHandler;
}
项目:rheem    文件:MockFactory.java   
public static ExecutionOperator createExecutionOperator(String name, int numInputs, int numOutputs, Platform platform) {
    final ExecutionOperator mockedExecutionOperator = mock(ExecutionOperator.class, Answers.CALLS_REAL_METHODS);
    when(mockedExecutionOperator.toString()).thenReturn("ExecutionOperator[" + name + "]");
    when(mockedExecutionOperator.getPlatform()).thenReturn(platform);

    // Mock input slots.
    final InputSlot[] inputSlots = new InputSlot[numInputs];
    for (int inputIndex = 0; inputIndex < numInputs; inputIndex++) {
        inputSlots[inputIndex] = new InputSlot("input-" + inputIndex, mockedExecutionOperator, mock(DataSetType.class));
    }
    when(mockedExecutionOperator.getAllInputs()).thenReturn(inputSlots);
    when(mockedExecutionOperator.getNumInputs()).thenCallRealMethod();

    // Mock output slots.
    final OutputSlot[] outputSlots = new OutputSlot[numOutputs];
    for (int outputIndex = 0; outputIndex < numOutputs; outputIndex++) {
        outputSlots[outputIndex] = new OutputSlot("output" + outputIndex, mockedExecutionOperator, mock(DataSetType.class));
    }
    when(mockedExecutionOperator.getAllOutputs()).thenReturn(outputSlots);
    when(mockedExecutionOperator.getNumOutputs()).thenCallRealMethod();
    return mockedExecutionOperator;
}
项目:FreeBuilder    文件:FilerUtilsTest.java   
@Test
public void testConstructor_avoidsEclipseWriterBug() throws IOException {
  // Due to a bug in Eclipse, we *must* call close on the object returned from openWriter().
  // Eclipse proxies a Writer but does not implement the fluent API correctly.
  // Here, we implement the fluent Writer API with the same bug:
  Writer mockWriter = Mockito.mock(Writer.class, new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      if (Writer.class.isAssignableFrom(invocation.getMethod().getReturnType())) {
        // Erroneously return the delegate writer (matching the Eclipse bug!)
        return source;
      } else {
        return Answers.RETURNS_SMART_NULLS.get().answer(invocation);
      }
    }
  });
  when(sourceFile.openWriter()).thenReturn(mockWriter);

  FilerUtils.writeCompilationUnit(filer, CLASS_TO_WRITE, originatingElement, "Hello!");
  verify(mockWriter).close();
}
项目:elide    文件:JsonApiTest.java   
@BeforeTest
void init() {
    EntityDictionary dictionary = new EntityDictionary(TestCheckMappings.MAPPINGS);
    dictionary.bindEntity(Parent.class);
    dictionary.bindEntity(Child.class);
    dictionary.bindInitializer(Parent::doInit, Parent.class);
    mapper = new JsonApiMapper(dictionary);
    AuditLogger testLogger = new TestAuditLogger();
    userScope = new RequestScope(null, new JsonApiDocument(),
            mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS), new User(0), null,
            new ElideSettingsBuilder(null)
                    .withJsonApiMapper(mapper)
                    .withAuditLogger(testLogger)
                    .withEntityDictionary(dictionary)
                    .build(), false);
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testUpdateToOneRelationHookInAddRelation() {
    FunWithPermissions fun = new FunWithPermissions();
    Child child = newChild(1);

    User goodUser = new User(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope);
    funResource.addRelation("relation3", childResource);

    verify(tx, times(1)).updateToOneRelation(eq(tx), eq(fun), any(), any(), eq(goodScope));
    verify(tx, never()).updateToOneRelation(eq(tx), eq(child), any(), any(), eq(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testUpdateToOneRelationHookInUpdateRelation() {
    FunWithPermissions fun = new FunWithPermissions();
    Child child1 = newChild(1);
    Child child2 = newChild(2);
    fun.setRelation1(Sets.newHashSet(child1));
    User goodUser = new User(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodScope);
    PersistentResource<Child> child2Resource = new PersistentResource<>(child2, null, "1", goodScope);
    funResource.updateRelation("relation3", Sets.newHashSet(child2Resource));

    verify(tx, times(1)).updateToOneRelation(eq(tx), eq(fun), any(), any(), eq(goodScope));
    verify(tx, never()).updateToOneRelation(eq(tx), eq(child1), any(), any(), eq(goodScope));
    verify(tx, never()).updateToOneRelation(eq(tx), eq(child2), any(), any(), eq(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testUpdateToOneRelationHookInRemoveRelation() {
    FunWithPermissions fun = new FunWithPermissions();
    Child child = newChild(1);
    fun.setRelation3(child);
    User goodUser = new User(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope);
    funResource.removeRelation("relation3", childResource);

    verify(tx, times(1)).updateToOneRelation(eq(tx), eq(fun), any(), any(), eq(goodScope));
    verify(tx, never()).updateToOneRelation(eq(tx), eq(child), any(), any(), eq(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testUpdateToManyRelationHookInAddRelationBidirection() {
    Parent parent = new Parent();
    Child child = newChild(1);

    User goodUser = new User(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, null, "3", goodScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope);
    parentResource.addRelation("children", childResource);
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(parent),
            any(), any(), any(), eq(goodScope));
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child),
            any(), any(), any(), eq(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testUpdateToManyRelationHookInRemoveRelationBidirection() {
    Parent parent = new Parent();
    Child child = newChild(1);
    parent.setChildren(Sets.newHashSet(child));
    child.setParents(Sets.newHashSet(parent));
    User goodUser = new User(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, null, "3", goodScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope);
    parentResource.removeRelation("children", childResource);

    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(parent),
            any(), any(), any(), eq(goodScope));
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child),
            any(), any(), any(), eq(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testUpdateToManyRelationHookInClearRelationBidirection() {
    Parent parent = new Parent();
    Child child1 = newChild(1);
    Child child2 = newChild(2);
    parent.setChildren(Sets.newHashSet(child1, child2));
    child1.setParents(Sets.newHashSet(parent));
    child2.setParents(Sets.newHashSet(parent));
    User goodUser = new User(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, null, "3", goodScope);
    parentResource.clearRelation("children");

    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(parent),
            any(), any(), any(), eq(goodScope));
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child1),
            any(), any(), any(), eq(goodScope));
    verify(tx, times(1)).updateToManyRelation(eq(tx), eq(child2),
            any(), any(), any(), eq(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testSuccessfulOneToOneRelationshipAdd() throws Exception {
    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    Left left = new Left();
    Right right = new Right();
    left.setId(2);
    right.setId(3);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);

    PersistentResource<Left> leftResource = new PersistentResource<>(left, null, "2", goodScope);

    Relationship ids = new Relationship(null, new Data<>(new ResourceIdentifier("right", "3").castToResource()));

    when(tx.loadObject(eq(Right.class), eq(3L), any(), any())).thenReturn(right);
    boolean updated = leftResource.updateRelation("one2one", ids.toPersistentResources(goodScope));
    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(left, goodScope);
    verify(tx, times(1)).save(right, goodScope);
    Assert.assertEquals(updated, true, "The one-2-one relationship should be added.");
    Assert.assertEquals(left.getOne2one().getId(), 3, "The correct object was set in the one-2-one relationship");
}
项目:elide    文件:PersistentResourceTest.java   
@Test
void testDeleteCascades() {
    Invoice invoice = new Invoice();
    invoice.setId(1);

    LineItem item = new LineItem();
    invoice.setItems(Sets.newHashSet(item));
    item.setInvoice(invoice);

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);

    PersistentResource<Invoice> invoiceResource = new PersistentResource<>(invoice, null, "1", goodScope);

    invoiceResource.deleteResource();

    verify(tx).delete(invoice, goodScope);

    /* The inverse relation should not be touched for cascading deletes */
    verify(tx, never()).save(item, goodScope);
    Assert.assertEquals(invoice.getItems().size(), 1);
}
项目:elide    文件:PersistentResourceTest.java   
@Test
void testDeleteResourceUpdateRelationshipSuccess() {
    Parent parent = new Parent();
    Child child = newChild(100);
    parent.setChildren(Sets.newHashSet(child));
    parent.setSpouses(Sets.newHashSet());
    child.setParents(Sets.newHashSet(parent));

    Assert.assertFalse(parent.getChildren().isEmpty());

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);

    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, null, "1", goodScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, parentResource, "1", goodScope);

    childResource.deleteResource();
    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).delete(child, goodScope);
    verify(tx, times(1)).save(parent, goodScope);
    verify(tx, never()).delete(parent, goodScope);
    Assert.assertTrue(parent.getChildren().isEmpty());
}
项目:elide    文件:PersistentResourceTest.java   
@Test
void testAddRelationSuccess() {
    FunWithPermissions fun = new FunWithPermissions();
    fun.setRelation1(Sets.newHashSet());

    Child child = newChild(1);

    User goodUser = new User(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "3", goodScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope);
    funResource.addRelation("relation1", childResource);

    goodScope.saveOrCreateObjects();
    verify(tx, never()).save(child, goodScope); // Child wasn't modified
    verify(tx, times(1)).save(fun, goodScope);

    Assert.assertTrue(fun.getRelation1().contains(child), "The correct element should be added to the relation");
}
项目:elide    文件:PersistentResourceTest.java   
@Test()
public void testRemoveToManyRelationSuccess() {
    Child child = newChild(1);
    Parent parent1 = newParent(1, child);
    Parent parent2 = newParent(2, child);
    Parent parent3 = newParent(3, child);
    child.setParents(Sets.newHashSet(parent1, parent2, parent3));

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Child> childResource = new PersistentResource<>(child, null, "1", goodScope);
    PersistentResource<Object> removeResource = new PersistentResource<>(parent1, null, "1", goodScope);
    childResource.removeRelation("parents", removeResource);

    Assert.assertEquals(child.getParents().size(), 2, "The many-2-many relationship should be cleared");
    Assert.assertEquals(parent1.getChildren().size(), 0, "The many-2-many inverse relationship should be cleared");
    Assert.assertEquals(parent3.getChildren().size(), 1, "The many-2-many inverse relationship should not be cleared");
    Assert.assertEquals(parent3.getChildren().size(), 1, "The many-2-many inverse relationship should not be cleared");

    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(child, goodScope);
    verify(tx, times(1)).save(parent1, goodScope);
    verify(tx, never()).save(parent2, goodScope);
    verify(tx, never()).save(parent3, goodScope);
}
项目:elide    文件:PersistentResourceTest.java   
@Test()
public void testRemoveToOneRelationSuccess() {
    FunWithPermissions fun = new FunWithPermissions();
    Child child = newChild(1);
    fun.setRelation3(child);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);

    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "1", goodScope);
    PersistentResource<Object> removeResource = new PersistentResource<>(child, null, "1", goodScope);

    funResource.removeRelation("relation3", removeResource);

    Assert.assertEquals(fun.getRelation3(), null, "The one-2-one relationship should be cleared");

    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(fun, goodScope);
    verify(tx, never()).save(child, goodScope);
}
项目:elide    文件:PersistentResourceTest.java   
@Test()
public void testRemoveNonexistingToOneRelation() {
    FunWithPermissions fun = new FunWithPermissions();
    Child ownedChild = newChild(1);
    Child unownedChild = newChild(2);
    fun.setRelation3(ownedChild);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);

    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "1", goodScope);
    PersistentResource<Object> removeResource = new PersistentResource<>(unownedChild, null, "1", goodScope);

    funResource.removeRelation("relation3", removeResource);

    Assert.assertEquals(fun.getRelation3(), ownedChild, "The one-2-one relationship should NOT be cleared");

    verify(tx, never()).save(fun, goodScope);
    verify(tx, never()).save(ownedChild, goodScope);
}
项目:elide    文件:PersistentResourceTest.java   
@Test()
public void testClearToOneRelationSuccess() {
    FunWithPermissions fun = new FunWithPermissions();
    Child child = newChild(1);
    fun.setRelation3(child);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "1", goodScope);
    funResource.clearRelation("relation3");

    Assert.assertEquals(fun.getRelation3(), null, "The one-2-one relationship should be cleared");

    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(fun, goodScope);
    verify(tx, times(1)).save(child, goodScope);
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testClearRelationInvalidToOneUpdatePermission() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();
    right.setId(1);
    left.noUpdateOne2One = right;
    right.noUpdateOne2One = left;

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, null, "1", goodScope);
    leftResource.clearRelation("noUpdateOne2One");
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testNoChangeRelationInvalidToOneUpdatePermission() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();
    right.setId(1);
    left.noUpdateOne2One = right;
    right.noUpdateOne2One = left;

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, null, "1", goodScope);
    leftResource.updateRelation("noUpdateOne2One", getRelation(leftResource, "noUpdateOne2One"));
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testClearRelationInvalidToManyUpdatePermission() {
    Left left = new Left();
    left.setId(1);
    Right right1 = new Right();
    right1.setId(1);
    Right right2 = new Right();
    right2.setId(2);
    left.noInverseUpdate = Sets.newHashSet(right1, right2);
    right1.noUpdate = Sets.newHashSet(left);
    right2.noUpdate = Sets.newHashSet(left);


    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, null, "1", goodScope);
    leftResource.clearRelation("noInverseUpdate");
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testClearRelationInvalidToOneDeletePermission() {
    Left left = new Left();
    left.setId(1);
    NoDeleteEntity noDelete = new NoDeleteEntity();
    noDelete.setId(1);
    left.noDeleteOne2One = noDelete;

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, null, "1", goodScope);
    Assert.assertTrue(leftResource.clearRelation("noDeleteOne2One"));
    Assert.assertNull(leftResource.getObject().noDeleteOne2One);

}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testUpdateAttributeSuccess() {
    Parent parent = newParent(1);

    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Parent> parentResource = new PersistentResource<>(parent, null, "1", goodScope);
    parentResource.updateAttribute("firstName", "foobar");

    Assert.assertEquals(parent.getFirstName(), "foobar", "The attribute was updated successfully");

    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(parent, goodScope);
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testUpdateAttributeInvalidUpdatePermission() {
    FunWithPermissions fun = new FunWithPermissions();
    fun.setId(1);


    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User badUser = new User(-1);

    RequestScope badScope = new RequestScope(null, null, tx, badUser, null, elideSettings, false);

    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, null, "1", badScope);
    funResource.updateAttribute("field4", "foobar");
    // Updates will defer and wait for the end!
    funResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
项目:elide    文件:PersistentResourceTest.java   
@Test()
public void testCreateObjectSuccess() {
    Parent parent = newParent(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    User goodUser = new User(1);

    when(tx.createNewObject(Parent.class)).thenReturn(parent);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Parent> created = PersistentResource.createObject(null, Parent.class, goodScope, Optional.of("uuid"));
    parent.setChildren(new HashSet<>());
    created.getRequestScope().getPermissionExecutor().executeCommitChecks();

    Assert.assertEquals(created.getObject(), parent,
            "The create function should return the requested parent object"
    );
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testDeletePermissionCheckedOnInverseRelationship() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();
    right.setId(2);

    left.fieldLevelDelete = Sets.newHashSet(right);
    right.allowDeleteAtFieldLevel = Sets.newHashSet(left);

    //Bad User triggers the delete permission failure
    User badUser = new User(-1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    RequestScope badScope = new RequestScope(null, null, tx, badUser, null, elideSettings, false);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, null, badScope.getUUIDFor(left), badScope);

    Assert.assertTrue(leftResource.clearRelation("fieldLevelDelete"));
    Assert.assertEquals(leftResource.getObject().fieldLevelDelete.size(), 0);
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testUpdatePermissionCheckedOnInverseRelationship() {
    Left left = new Left();
    left.setId(1);
    Right right = new Right();

    left.noInverseUpdate = Sets.newHashSet(right);
    right.noUpdate = Sets.newHashSet(left);

    List<Resource> empty = new ArrayList<>();
    Relationship ids = new Relationship(null, new Data<>(empty));

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<Left> leftResource = new PersistentResource<>(left, null, goodScope.getUUIDFor(left), goodScope);

    leftResource.updateRelation("noInverseUpdate", ids.toPersistentResources(goodScope));
    // Modifications have a deferred check component:
    leftResource.getRequestScope().getPermissionExecutor().executeCommitChecks();
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testSharePermissionErrorOnUpdateSingularRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);

    NoShareEntity noShare = new NoShareEntity();
    noShare.setId(1);

    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    when(tx.loadObject(eq(NoShareEntity.class), eq(1L), any(), any())).thenReturn(noShare);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, null, goodScope.getUUIDFor(userModel), goodScope);

    userResource.updateRelation("noShare", ids.toPersistentResources(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testSharePermissionErrorOnUpdateRelationshipPackageLevel() {
    ContainerWithPackageShare containerWithPackageShare = new ContainerWithPackageShare();

    UnshareableWithEntityUnshare unshareableWithEntityUnshare = new UnshareableWithEntityUnshare();
    unshareableWithEntityUnshare.setContainerWithPackageShare(containerWithPackageShare);

    List<Resource> unShareableList = new ArrayList<>();
    unShareableList.add(new ResourceIdentifier("unshareableWithEntityUnshare", "1").castToResource());
    Relationship unShareales = new Relationship(null, new Data<>(unShareableList));

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    when(tx.loadObject(eq(UnshareableWithEntityUnshare.class), eq(1L), any(), any())).thenReturn(unshareableWithEntityUnshare);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<ContainerWithPackageShare> containerResource = new PersistentResource<>(containerWithPackageShare, null, goodScope.getUUIDFor(containerWithPackageShare), goodScope);

    containerResource.updateRelation("unshareableWithEntityUnshares", unShareales.toPersistentResources(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testSharePermissionSuccessOnUpdateManyRelationshipPackageLevel() {
    ContainerWithPackageShare containerWithPackageShare = new ContainerWithPackageShare();

    ShareableWithPackageShare shareableWithPackageShare = new ShareableWithPackageShare();
    shareableWithPackageShare.setContainerWithPackageShare(containerWithPackageShare);

    List<Resource> shareableList = new ArrayList<>();
    shareableList.add(new ResourceIdentifier("shareableWithPackageShare", "1").castToResource());
    Relationship shareables = new Relationship(null, new Data<>(shareableList));

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    when(tx.loadObject(eq(ShareableWithPackageShare.class), eq(1L), any(), any())).thenReturn(shareableWithPackageShare);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<ContainerWithPackageShare> containerResource = new PersistentResource<>(containerWithPackageShare, null, goodScope.getUUIDFor(containerWithPackageShare), goodScope);

    containerResource.updateRelation("shareableWithPackageShares", shareables.toPersistentResources(goodScope));

    Assert.assertEquals(containerWithPackageShare.getShareableWithPackageShares().size(), 1);
    Assert.assertTrue(containerWithPackageShare.getShareableWithPackageShares().contains(shareableWithPackageShare));
}
项目:elide    文件:PersistentResourceTest.java   
@Test(expectedExceptions = ForbiddenAccessException.class)
public void testSharePermissionErrorOnUpdateManyRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);

    NoShareEntity noShare1 = new NoShareEntity();
    noShare1.setId(1);
    NoShareEntity noShare2 = new NoShareEntity();
    noShare2.setId(2);

    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    idList.add(new ResourceIdentifier("noshare", "2").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    when(tx.loadObject(eq(NoShareEntity.class), eq(1L), any(), any())).thenReturn(noShare1);
    when(tx.loadObject(eq(NoShareEntity.class), eq(2L), any(), any())).thenReturn(noShare2);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, null, goodScope.getUUIDFor(userModel), goodScope);

    userResource.updateRelation("noShares", ids.toPersistentResources(goodScope));
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testSharePermissionSuccessOnUpdateSingularRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);

    NoShareEntity noShare = new NoShareEntity();

    /* The noshare already exists so no exception should be thrown */
    userModel.setNoShare(noShare);

    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("noshare", "1").castToResource());
    Relationship ids = new Relationship(null, new Data<>(idList));

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
    when(tx.loadObject(eq(NoShareEntity.class), eq(1L), any(), any())).thenReturn(noShare);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, null, goodScope.getUUIDFor(userModel), goodScope);

    boolean returnVal = userResource.updateRelation("noShare", ids.toPersistentResources(goodScope));

    Assert.assertFalse(returnVal);
    Assert.assertEquals(userModel.getNoShare(), noShare);
}
项目:elide    文件:PersistentResourceTest.java   
@Test
public void testSharePermissionSuccessOnClearSingularRelationship() {
    example.User userModel = new example.User();
    userModel.setId(1);

    NoShareEntity noShare = new NoShareEntity();

    /* The noshare already exists so no exception should be thrown */
    userModel.setNoShare(noShare);

    List<Resource> empty = new ArrayList<>();
    Relationship ids = new Relationship(null, new Data<>(empty));

    User goodUser = new User(1);
    DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);

    RequestScope goodScope = new RequestScope(null, null, tx, goodUser, null, elideSettings, false);
    PersistentResource<example.User> userResource = new PersistentResource<>(userModel, null, goodScope.getUUIDFor(userModel), goodScope);

    boolean returnVal = userResource.updateRelation("noShare", ids.toPersistentResources(goodScope));

    Assert.assertTrue(returnVal);
    Assert.assertNull(userModel.getNoShare());
}
项目:mparticle-android-sdk    文件:MessageManagerTest.java   
@Test
@PrepareForTest({MessageManager.class, MPUtility.class})
public void testGetStateInfo() throws Exception {
    PowerMockito.mockStatic(MPUtility.class, Answers.RETURNS_MOCKS.get());
    JSONObject stateInfo = manager.getStateInfo();
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_AVAILABLE_MEMORY));
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_TOTAL_MEMORY));
    assertNotNull(stateInfo.getDouble(Constants.MessageKey.STATE_INFO_BATTERY_LVL));
    assertNotNull(stateInfo.getDouble(Constants.MessageKey.STATE_INFO_TIME_SINCE_START));
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_AVAILABLE_DISK));
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_AVAILABLE_EXT_DISK));
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_APP_MEMORY_USAGE));
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_APP_MEMORY_AVAIL));
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_APP_MEMORY_MAX));
    assertNotNull(stateInfo.getString(Constants.MessageKey.STATE_INFO_DATA_CONNECTION));
    assertNotNull(stateInfo.getInt(Constants.MessageKey.STATE_INFO_ORIENTATION));
    assertNotNull(stateInfo.getInt(Constants.MessageKey.STATE_INFO_BAR_ORIENTATION));
    assertNotNull(stateInfo.getBoolean(Constants.MessageKey.STATE_INFO_MEMORY_LOW));
    assertNotNull(stateInfo.getBoolean(Constants.MessageKey.STATE_INFO_GPS));
    assertNotNull(stateInfo.getLong(Constants.MessageKey.STATE_INFO_MEMORY_THRESHOLD));
    assertNotNull(stateInfo.getInt(Constants.MessageKey.STATE_INFO_NETWORK_TYPE));
}
项目:jabref    文件:XMPUtilTest.java   
/**
 * Create a temporary PDF-file with a single empty page.
 */
@Before
public void setUp() throws IOException, COSVisitorException {

    pdfFile = tempFolder.newFile("JabRef.pdf");

    try (PDDocument pdf = new PDDocument()) {
        //Need page to open in Acrobat
        pdf.addPage(new PDPage());
        pdf.save(pdfFile.getAbsolutePath());
    }

    importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
    when(importFormatPreferences.getEncoding()).thenReturn(StandardCharsets.UTF_8);
    xmpPreferences = mock(XMPPreferences.class);
    // The code assumes privacy filters to be off
    when(xmpPreferences.isUseXMPPrivacyFilter()).thenReturn(false);

    when(xmpPreferences.getKeywordSeparator()).thenReturn(',');
}
项目:jabref    文件:HtmlExportFormatTest.java   
@Before
public void setUp() {
    Map<String, TemplateExporter> customFormats = new HashMap<>();
    LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS);
    SavePreferences savePreferences = mock(SavePreferences.class);
    ExporterFactory exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences);

    exportFormat = exporterFactory.getExporterByName("html").get();

    databaseContext = new BibDatabaseContext();
    charset = StandardCharsets.UTF_8;
    BibEntry entry = new BibEntry();
    entry.setField("title", "my paper title");
    entry.setField("author", "Stefan Kolb");
    entry.setCiteKey("mykey");
    entries = Arrays.asList(entry);
}
项目:jabref    文件:IsbnViaEbookDeFetcherTest.java   
@BeforeEach
public void setUp() {
    bibEntry = new BibEntry();
    bibEntry.setType(BiblatexEntryTypes.BOOK);
    bibEntry.setField("bibtexkey", "9780134685991");
    bibEntry.setField("title", "Effective Java");
    bibEntry.setField("publisher", "ADDISON WESLEY PUB CO INC");
    bibEntry.setField("pagetotal", "416");
    bibEntry.setField("year", "2018");
    bibEntry.setField("author", "Bloch, Joshua");
    bibEntry.setField("date", "2018-01-06");
    bibEntry.setField("ean", "9780134685991");
    bibEntry.setField("isbn", "0134685997");
    bibEntry.setField("url", "http://www.ebook.de/de/product/28983211/joshua_bloch_effective_java.html");

    fetcher = new IsbnViaEbookDeFetcher(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS));
}
项目:jabref    文件:DoiFetcherTest.java   
@BeforeEach
public void setUp() {
    fetcher = new DoiFetcher(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS));

    bibEntryBurd2011 = new BibEntry();
    bibEntryBurd2011.setType(BiblatexEntryTypes.BOOK);
    bibEntryBurd2011.setField("bibtexkey", "Burd_2011");
    bibEntryBurd2011.setField("title", "Java{\\textregistered} For Dummies{\\textregistered}");
    bibEntryBurd2011.setField("publisher", "Wiley Publishing, Inc.");
    bibEntryBurd2011.setField("year", "2011");
    bibEntryBurd2011.setField("author", "Barry Burd");
    bibEntryBurd2011.setField("month", "jul");
    bibEntryBurd2011.setField("doi", "10.1002/9781118257517");

    bibEntryDecker2007 = new BibEntry();
    bibEntryDecker2007.setType(BiblatexEntryTypes.INPROCEEDINGS);
    bibEntryDecker2007.setField("bibtexkey", "Decker_2007");
    bibEntryDecker2007.setField("author", "Gero Decker and Oliver Kopp and Frank Leymann and Mathias Weske");
    bibEntryDecker2007.setField("booktitle", "{IEEE} International Conference on Web Services ({ICWS} 2007)");
    bibEntryDecker2007.setField("month", "jul");
    bibEntryDecker2007.setField("publisher", "{IEEE}");
    bibEntryDecker2007.setField("title", "{BPEL}4Chor: Extending {BPEL} for Modeling Choreographies");
    bibEntryDecker2007.setField("year", "2007");
    bibEntryDecker2007.setField("doi", "10.1109/icws.2007.59");
}
项目:jabref    文件:IsbnFetcherTest.java   
@BeforeEach
public void setUp() {
    fetcher = new IsbnFetcher(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS));

    bibEntry = new BibEntry();
    bibEntry.setType(BiblatexEntryTypes.BOOK);
    bibEntry.setField("bibtexkey", "9780134685991");
    bibEntry.setField("title", "Effective Java");
    bibEntry.setField("publisher", "ADDISON WESLEY PUB CO INC");
    bibEntry.setField("pagetotal", "416");
    bibEntry.setField("year", "2018");
    bibEntry.setField("author", "Bloch, Joshua");
    bibEntry.setField("date", "2018-01-06");
    bibEntry.setField("ean", "9780134685991");
    bibEntry.setField("isbn", "0134685997");
    bibEntry.setField("url", "http://www.ebook.de/de/product/28983211/joshua_bloch_effective_java.html");

}
项目:jabref    文件:EntryEditorTest.java   
@Override
public void start(Stage stage) throws Exception {
    area = new CodeArea();
    area.appendText("some example\n text to go here\n across a couple of \n lines....");
    JabRefPreferences preferences = mock(JabRefPreferences.class, Answers.RETURNS_DEEP_STUBS);
    sourceTab = new SourceTab(new BibDatabaseContext(), new CountingUndoManager(), new LatexFieldFormatterPreferences(), preferences, new DummyFileUpdateMonitor());
    pane = new TabPane(
            new Tab("main area", area),
            new Tab("other tab", new Label("some text")),
            sourceTab
    );
    scene = new Scene(pane);
    this.stage = stage;

    stage.setScene(scene);
    stage.setWidth(400);
    stage.setHeight(400);
    stage.show();

    // select the area's tab
    pane.getSelectionModel().select(0);
}