Stack project
<?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>teardrop</groupId>
<artifactId>Stack</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package teardrop.stack;
import org.testng.Assert;
import org.testng.annotations.Test;
public class StackTest {
@Test
public void stackShouldBeEmptyUponCreation() throws Exception {
Stack<String> stack = new Stack<String>();
Assert.assertTrue(stack.isEmpty());
Assert.assertEquals(stack.size(), 0);
}
@Test
public void stackShouldNotBeEmptyAfterPush() throws Exception {
Stack<String> stack = new Stack<String>();
stack.push("test");
Assert.assertFalse(stack.isEmpty());
Assert.assertEquals(stack.size(), 1);
}
@Test(expectedExceptions = StackException.class)
public void popShouldThrowExceptionIfStackIsEmpty() throws Exception {
Stack<String> stack = new Stack<String>();
stack.pop();
}
@Test(expectedExceptions = StackException.class)
public void topShouldThrowExceptionIfStackIsEmpty() throws Exception {
Stack<String> stack = new Stack<String>();
stack.pop();
}
@Test
public void pushShouldPutElementIntoStack() throws Exception {
Stack<String> stack = new Stack<String>();
stack.push("hello");
Assert.assertEquals(stack.top(), "hello");
Assert.assertEquals(stack.size(), 1);
stack.push("world");
Assert.assertEquals(stack.top(), "world");
Assert.assertEquals(stack.size(), 2);
}
@Test
public void popShouldRemoveElementsFromStack() throws Exception {
Stack<String> stack = new Stack<String>();
stack.push("hello");
stack.push("world");
Assert.assertEquals(stack.pop(), "world");
Assert.assertEquals(stack.size(), 1);
Assert.assertEquals(stack.pop(), "hello");
Assert.assertEquals(stack.size(), 0);
Assert.assertTrue(stack.isEmpty());
}
}
package teardrop.stack;
public class StackException extends RuntimeException {
public StackException(String message) {
super(message);
}
}
package teardrop.stack;
public class Stack<T> {
/**
* Adds the element to the top of the stack.
*/
public void push(T element) {
}
/**
* Removes the element from the top of the stack.
*
* @throws StackException if stack is empty
* @return the element removed from the top of the stack
*/
public T pop() {
return null;
}
/**
* Returns top element of the stack. The element is kept in the stack and not removed.
*
* @throws StackException if stack is empty
* @return the element from the top of the stack
*/
public T top() {
return null;
}
/**
* @return number of elements in the stack
*/
public long size() {
return 0;
}
/**
* @return whether the stack is empty
*/
public boolean isEmpty() {
return false;
}
}