Skip to content
Open

lab2 #65

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions exercise-statepattern-361/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>kz.edu.nu.cs</groupId>
<artifactId>stateexercise-361</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>stateexercise-361</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kz.edu.nu.cs.exercise;

public abstract class State {
protected StateContext sc;
protected boolean accept = false;

public void actionA() {
}

public void actionB() {
}

public boolean isAccept() {
return this.accept;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kz.edu.nu.cs.exercise;

public class State1 extends State{

public State1(StateContext sc) {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kz.edu.nu.cs.exercise;

public class State2 extends State{

public State2(StateContext sc) {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kz.edu.nu.cs.exercise;

public class State3 extends State{

public State3(StateContext sc) {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kz.edu.nu.cs.exercise;

public class StateContext {
final State state1 = new State1(this);
final State state2 = new State2(this);
final State state3 = new State3(this);

private State currentState;

public StateContext() {
this.currentState = state1;
}

public void actionA() {
if(currentState.equals(state1)){
currentState = state2;
}else if(currentState.equals(state2)){
currentState = state3;
}else{
currentState = state3;
}
}

public void actionB() {
if(currentState.equals(state1)){
currentState = state1;
}else if(currentState.equals(state2)){
currentState = state1;
}else{
currentState = state2;
}
}

public boolean inAcceptState() {
if(currentState.equals(state3)){
return true;
}else {
return false;
}

}

public State getCurrentState() {
return currentState;
}

public void setCurrentState(State currentState) {
this.currentState = currentState;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package kz.edu.nu.cs.exercise;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

public class TestStatePattern {

StateContext sc;

@Before
public void setUp() {
sc = new StateContext();
}

/*
* Test Sequence: A
*/
@Test
public void test_1() {
sc.actionA();
assertFalse("Test Sequence A", sc.inAcceptState());
}

/*
* Test Sequence: AA
*/
@Test
public void test_2() {
sc.actionA();
sc.actionA();
assertTrue("Test Sequence AA", sc.inAcceptState());
}

/*
* Test Sequence: A (check state)
*/
@Test
public void test_3() {
sc.actionA();
assertTrue("Test Sequence: A (check state)", sc.getCurrentState().equals(sc.state2));
}

/*
* Test Sequence: AAB (check state)
*/
@Test
public void test_4() {
sc.actionA();
sc.actionA();
sc.actionB();
assertTrue("Test Sequence: AAB (check state)", sc.getCurrentState().equals(sc.state2));
}

/*
* Test Sequence: AABB (check state)
*/
@Test
public void test_5() {
sc.actionA();
sc.actionA();
sc.actionB();
sc.actionB();
assertTrue("Test Sequence: AABB (check state)", sc.getCurrentState().equals(sc.state1));
}

/*
* Test Sequence: AAA
*/
@Test
public void test_6() {
sc.actionA();
sc.actionA();
sc.actionA();
assertTrue("Test Sequence: AAA", sc.inAcceptState());
}
}