-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from imperva/3.9.1_junit_linda
Added first unit tests
- Loading branch information
Showing
6 changed files
with
244 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/java/com/imperva/stepping/ContainerServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.imperva.stepping; | ||
|
||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Author: Linda Nasredin | ||
* Date: 06 May 2020 | ||
*/ | ||
class ContainerServiceTest { | ||
|
||
@Test | ||
void getTickCallbackRunning() { | ||
RunningScheduled value = mock(RunningScheduled.class); | ||
|
||
Container innerContainer = new ContainerService(); | ||
innerContainer.add(value, "stepId3" + ContainerService.RUNNING_SCHEDULED); | ||
|
||
ContainerService containerService = new ContainerService(); | ||
containerService.add(innerContainer, ContainerService.STEPPING_PRIVATE_CONTAINER); | ||
|
||
RunningScheduled actual = containerService.getTickCallbackRunning("stepId3"); | ||
|
||
Assert.assertEquals(value, actual); | ||
} | ||
|
||
@Test | ||
void getQSize() { | ||
IStepDecorator decoratorMock = mock(IStepDecorator.class); | ||
when(decoratorMock.getQSize()).thenReturn(34); | ||
|
||
Container innerContainer = new ContainerService(); | ||
innerContainer.add(decoratorMock, "stepId1" + ContainerService.DECORATOR); | ||
|
||
ContainerService containerService = new ContainerService(); | ||
containerService.add(innerContainer, ContainerService.STEPPING_PRIVATE_CONTAINER); | ||
|
||
int qSize = containerService.getQSize("stepId1"); | ||
|
||
Assert.assertEquals(34, qSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.imperva.stepping; | ||
|
||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* Author: Linda Nasredin | ||
* Date: 06 May 2020 | ||
*/ | ||
class DataTest { | ||
|
||
@Test | ||
void getSize_nullValue() { | ||
Data data = new Data(null); | ||
Assert.assertEquals(0, data.getSize()); | ||
} | ||
|
||
@Test | ||
void getSize_stringValue() { | ||
Data data = new Data("value"); | ||
Assertions.assertEquals(1, data.getSize()); | ||
} | ||
|
||
@Test | ||
void getSize_listValue() { | ||
Data data = new Data(Arrays.asList(1, 2, 3)); | ||
Assertions.assertEquals(3, data.getSize()); | ||
} | ||
|
||
@Test | ||
void getSize_listValueChange() { | ||
List<Integer> list = new ArrayList<>(); | ||
list.add(1); | ||
list.add(2); | ||
Data data = new Data(list); | ||
Assertions.assertEquals(2, data.getSize()); | ||
list.add(3); | ||
Assertions.assertEquals(2, data.getSize()); | ||
} | ||
|
||
@Test | ||
void getValue() { | ||
Data data = new Data("value"); | ||
Assertions.assertEquals("value", data.getValue()); | ||
} | ||
|
||
@Test | ||
void isExpirable() { | ||
Data data = new Data("value"); | ||
Assertions.assertFalse(data.isExpirable()); | ||
} | ||
|
||
@Test | ||
void tryGrabAndExpire() { | ||
Data data = new Data("value"); | ||
data.setExpirationCondition((data1, context) -> false, 1); | ||
Assert.assertTrue(data.isExpirable()); | ||
Assertions.assertFalse(data.tryGrabAndExpire()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.imperva.stepping; | ||
|
||
import com.sun.source.tree.AssertTree; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* Author: Linda Nasredin | ||
* Date: 06 May 2020 | ||
*/ | ||
class QTest { | ||
|
||
@Test | ||
void q_zeroCapacityIsMaxCapacity() { | ||
Q<Integer> q = new Q<>(); | ||
// TODO actual capacity of the inner queue is Integer.MAX_VALUE | ||
Assert.assertEquals(0, q.getCapacity()); | ||
} | ||
|
||
@Test | ||
void q_positiveCapacity() { | ||
Q<Integer> q = new Q<>(10); | ||
Assert.assertEquals(10, q.getCapacity()); | ||
} | ||
|
||
@Test | ||
void q_negativeCapacity() { | ||
Assertions.assertThrows(SteppingException.class, () -> new Q<>(-1)); | ||
} | ||
|
||
@Test | ||
void peek_found() { | ||
Q<String> q = new Q<>(2); | ||
q.queue("item1"); | ||
q.queue("item2"); | ||
Assertions.assertEquals("item1", q.peek()); | ||
} | ||
|
||
@Test | ||
void peek_empty() { | ||
Q<String> q = new Q<>(2); | ||
Assertions.assertNull(q.peek()); | ||
} | ||
|
||
@Test | ||
void queue_wait_take() throws InterruptedException { | ||
Q<String> q = new Q<>(2); | ||
q.queue("item1"); | ||
|
||
Runnable runnable = () -> { | ||
q.queue("item2"); | ||
q.queue("item3"); | ||
}; | ||
new Thread(runnable).start(); | ||
|
||
Thread.sleep(50); | ||
Assertions.assertEquals(2, q.size()); | ||
q.take(); | ||
Thread.sleep(50); | ||
Assertions.assertEquals(2, q.size()); | ||
} | ||
|
||
@Test | ||
void contains() { | ||
Q<String> q = new Q<>(2); | ||
q.queue("item1"); | ||
q.queue("item2"); | ||
Assertions.assertTrue(q.contains()); | ||
} | ||
|
||
@Test | ||
void take_wait() throws InterruptedException { | ||
Q<String> q = new Q<>(2); | ||
Runnable runnable = () -> { | ||
try { | ||
q.take(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
}; | ||
new Thread(runnable).start(); | ||
|
||
Thread.sleep(50); | ||
q.queue("item1"); | ||
Assertions.assertEquals(1, q.size()); | ||
Thread.sleep(50); | ||
Assertions.assertEquals(0, q.size()); | ||
} | ||
|
||
@Test | ||
void clear() { | ||
Q<String> q = new Q<>(2); | ||
q.queue("item1"); | ||
q.queue("item2"); | ||
q.clear(); | ||
Assertions.assertEquals(0, q.size()); | ||
} | ||
|
||
@Test | ||
void offer() { | ||
Q<String> q = new Q<>(1); | ||
boolean offered1 = q.offer("item1"); | ||
Assertions.assertTrue(offered1); | ||
boolean offered2 = q.offer("item2"); | ||
Assertions.assertFalse(offered2); | ||
} | ||
|
||
} |