Skip to content

Commit

Permalink
Added missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
johan.haleby committed Sep 22, 2009
1 parent 865530c commit 32b5d26
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.powermock.api.mockito.expectation;

import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Stubber;

/**
*Setup stubbing for void methods in final class, final void methods, or static
* (final) methods. Note that for private void methods you should use the
* standard <code>when</code> method in <code>PowerMockito</code>.
*/
public interface PowerMockitoStubber extends Stubber {
/**
* Allows to choose a static void method when stubbing in
* doThrow()|doAnswer()|doNothing()|doReturn() style
* <p>
* Example:
*
* <pre>
* doThrow(new RuntimeException()).when();
* StaticList.clear();
*
* //following throws RuntimeException:
* StaticList.clear();
* </pre>
*
* Read more about those methods:
* <p>
* {@link Mockito#doThrow(Throwable)}
* <p>
* {@link Mockito#doAnswer(Answer)}
* <p>
* {@link Mockito#doNothing()}
* <p>
* {@link Mockito#doReturn(Object)}
* <p>
*
* See examples in javadoc for {@link Mockito}
*/
void when(Class<?> classMock);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.powermock.api.mockito.internal;

import org.mockito.Mockito;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.expectation.PowerMockitoStubber;
import org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl;
import org.powermock.reflect.Whitebox;

public class PowerMockitoCore {
@SuppressWarnings("unchecked")
public PowerMockitoStubber doAnswer(Answer answer) {
getMockingProgress().stubbingStarted();
getMockingProgress().resetOngoingStubbing();
return (PowerMockitoStubber) new PowerMockitoStubberImpl().doAnswer(answer);
}

private MockingProgress getMockingProgress() {
return Whitebox.getInternalState(Mockito.class, MockingProgress.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.powermock.api.mockito.internal.expectation;

import java.util.List;

import org.mockito.internal.stubbing.StubberImpl;
import org.mockito.stubbing.Stubber;
import org.powermock.api.mockito.expectation.PowerMockitoStubber;
import org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl;
import org.powermock.core.MockRepository;
import org.powermock.reflect.Whitebox;

/**
* Extension of the standard Mocktio stubber implementation that also support
* PowerMockito created mocks.
*/
public class PowerMockitoStubberImpl extends StubberImpl implements PowerMockitoStubber {

/**
* {@inheritDoc}
*/
public void when(Class<?> classMock) {
MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository
.getStaticMethodInvocationControl(classMock);
addAnswersForStubbing(invocationControl);
}

/**
* Supports PowerMockito mocks. If <code>mock</code> is not a PowerMockito
* mock it will delegate to Mockito.
*
* @see Stubber#when(Object)
*/
@Override
public <T> T when(T instanceMock) {
MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository
.getInstanceMethodInvocationControl(instanceMock);
final T returnValue;
if (invocationControl == null) {
returnValue = super.when(instanceMock);
} else {
addAnswersForStubbing(invocationControl);
returnValue = instanceMock;
}
return returnValue;
}

@SuppressWarnings("unchecked")
private void addAnswersForStubbing(MockitoMethodInvocationControl invocationControl) {
invocationControl.getInvocationHandler().getMockHandler().setAnswersForStubbing(Whitebox.getInternalState(this, List.class));
}
}

0 comments on commit 32b5d26

Please sign in to comment.