Skip to content

Commit

Permalink
Merge remote branch 'fork/359_mkscenario_and_mkstep'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 10, 2014
2 parents 8392141 + cd2c31e commit ad43317
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 22 deletions.
55 changes: 55 additions & 0 deletions src/main/java/com/thindeck/api/mock/MkScenario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2014, Thindeck.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the thindeck.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.thindeck.api.mock;

import com.jcabi.aspects.Immutable;
import com.thindeck.api.Scenario;
import com.thindeck.api.Step;
import java.util.Collections;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
* Mock of {@link com.thindeck.api.Scenario}.
*
* @author Slava Semushin ([email protected])
* @version $Id$
* @since 0.5
*/
@Immutable
@ToString
@EqualsAndHashCode
public final class MkScenario implements Scenario {

@Override
public Iterable<Step> steps() {
return Collections.<Step>singletonList(new MkStep());
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/thindeck/api/mock/MkStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2014, Thindeck.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the thindeck.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.thindeck.api.mock;

import com.jcabi.aspects.Immutable;
import com.thindeck.api.Context;
import com.thindeck.api.Step;
import java.io.IOException;
import java.util.Random;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
* Mock of {@link com.thindeck.api.Step}.
*
* @author Slava Semushin ([email protected])
* @version $Id$
* @since 0.5
*/
@Immutable
@ToString
@EqualsAndHashCode
public final class MkStep implements Step {

/**
* Step's name.
*/
private final transient String step = String.format(
"Step #%d",
new Random().nextInt()
);

@Override
public String name() {
return this.step;
}

@Override
public void exec(final Context ctx) throws IOException {
// do nothing
}

@Override
public void commit(final Context ctx) throws IOException {
// do nothing
}

@Override
public void rollback(final Context ctx) throws IOException {
// do nothing
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/thindeck/api/mock/MkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ public String command() {

@Override
public Scenario scenario() {
throw new UnsupportedOperationException("MkTask#scenario");
return new MkScenario();
}
}
23 changes: 2 additions & 21 deletions src/test/java/com/thindeck/life/LifecycleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@
package com.thindeck.life;

import com.thindeck.api.Base;
import com.thindeck.api.Repo;
import com.thindeck.api.Repos;
import com.thindeck.api.Task;
import com.thindeck.api.Tasks;
import com.thindeck.api.Txn;
import com.thindeck.api.mock.MkBase;
import java.io.IOException;
import java.util.Collections;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -75,26 +70,12 @@ public void initializedBaseAttribute() {
/**
* RoutineTxn can increment transactions.
* @throws IOException In case of error.
* @todo #351 Implement MkScenario and MkStep.
* Rewrite the test after that using mocks from
* com.thindeck.api.mock package instead of mockito.
*/
@Test
public void incrementsTransactionCount() throws IOException {
final Base base = Mockito.mock(Base.class);
final Repos repos = Mockito.mock(Repos.class);
final Repo repo = Mockito.mock(Repo.class);
final Tasks tasks = Mockito.mock(Tasks.class);
final Task task = Mockito.mock(Task.class);
final Txn txn = Mockito.mock(Txn.class);
Mockito.when(base.repos()).thenReturn(repos);
Mockito.when(repos.iterate()).thenReturn(Collections.singleton(repo));
Mockito.when(repo.tasks()).thenReturn(tasks);
Mockito.when(tasks.open()).thenReturn(Collections.singleton(task));
Mockito.when(base.txn(Mockito.any(Task.class))).thenReturn(txn);
final Base base = new MkBase();
final RoutineTxns routine = new RoutineTxns(base);
routine.close();
routine.run();
Mockito.verify(txn).increment();
}
}

0 comments on commit ad43317

Please sign in to comment.