| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
* |
| 19 |
*/ |
| 20 |
package org.apache.qpid.proton.engine.impl; |
| 21 |
|
| 22 |
import static org.junit.Assert.assertEquals; |
| 23 |
import static org.junit.Assert.assertFalse; |
| 24 |
import static org.junit.Assert.assertTrue; |
| 25 |
|
| 26 |
import org.junit.Test; |
| 27 |
import org.mockito.Mockito; |
| 28 |
|
| 29 |
public class EndpointImplTest |
| 30 |
{ |
| 31 |
@Test |
| 32 |
public void testRepeatOpenDoesNotModifyEndpoint() |
| 33 |
{ |
| 34 |
ConnectionImpl mockConnection = Mockito.mock(ConnectionImpl.class); |
| 35 |
MockEndpointImpl endpoint = new MockEndpointImpl(mockConnection); |
| 36 |
|
| 37 |
// Check starting state |
| 38 |
assertFalse("Should not be modified", endpoint.isModified()); |
| 39 |
assertEquals("Unexpected localOpen call count", 0, endpoint.getLocalOpenCallCount()); |
| 40 |
Mockito.verify(mockConnection, Mockito.times(0)).addModified(Mockito.any(EndpointImpl.class)); |
| 41 |
|
| 42 |
endpoint.open(); |
| 43 |
|
| 44 |
// Check endpoint was modified |
| 45 |
assertTrue("Should be modified", endpoint.isModified()); |
| 46 |
assertEquals("Unexpected localOpen call count", 1, endpoint.getLocalOpenCallCount()); |
| 47 |
Mockito.verify(mockConnection, Mockito.times(1)).addModified(Mockito.any(EndpointImpl.class)); |
| 48 |
|
| 49 |
// Clear the modified state, open again, verify no change |
| 50 |
endpoint.clearModified(); |
| 51 |
assertFalse("Should no longer be modified", endpoint.isModified()); |
| 52 |
|
| 53 |
endpoint.open(); |
| 54 |
|
| 55 |
assertFalse("Should not be modified", endpoint.isModified()); |
| 56 |
assertEquals("Unexpected localOpen call count", 1, endpoint.getLocalOpenCallCount()); |
| 57 |
Mockito.verify(mockConnection, Mockito.times(1)).addModified(Mockito.any(EndpointImpl.class)); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void testRepeatCloseDoesNotModifyEndpoint() |
| 62 |
{ |
| 63 |
ConnectionImpl mockConnection = Mockito.mock(ConnectionImpl.class); |
| 64 |
MockEndpointImpl endpoint = new MockEndpointImpl(mockConnection); |
| 65 |
|
| 66 |
// Open endpoint, clear the modified state, verify current state |
| 67 |
endpoint.open(); |
| 68 |
endpoint.clearModified(); |
| 69 |
assertFalse("Should no longer be modified", endpoint.isModified()); |
| 70 |
assertEquals("Unexpected localClose call count", 0, endpoint.getLocalCloseCallCount()); |
| 71 |
Mockito.verify(mockConnection, Mockito.times(1)).addModified(Mockito.any(EndpointImpl.class)); |
| 72 |
|
| 73 |
// Now close, verify changes |
| 74 |
endpoint.close(); |
| 75 |
|
| 76 |
// Check endpoint was modified |
| 77 |
assertTrue("Should be modified", endpoint.isModified()); |
| 78 |
assertEquals("Unexpected localClose call count", 1, endpoint.getLocalCloseCallCount()); |
| 79 |
Mockito.verify(mockConnection, Mockito.times(2)).addModified(Mockito.any(EndpointImpl.class)); |
| 80 |
|
| 81 |
// Clear the modified state, close again, verify no change |
| 82 |
endpoint.clearModified(); |
| 83 |
assertFalse("Should no longer be modified", endpoint.isModified()); |
| 84 |
|
| 85 |
endpoint.close(); |
| 86 |
|
| 87 |
assertFalse("Should not be modified", endpoint.isModified()); |
| 88 |
assertEquals("Unexpected localClose call count", 1, endpoint.getLocalCloseCallCount()); |
| 89 |
Mockito.verify(mockConnection, Mockito.times(2)).addModified(Mockito.any(EndpointImpl.class)); |
| 90 |
} |
| 91 |
|
| 92 |
private class MockEndpointImpl extends EndpointImpl |
| 93 |
{ |
| 94 |
private int localOpenCallCount; |
| 95 |
private int localCloseCallCount; |
| 96 |
private ConnectionImpl connectionImpl; |
| 97 |
public MockEndpointImpl(ConnectionImpl connectionImpl) |
| 98 |
{ |
| 99 |
this.connectionImpl = connectionImpl; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
void localOpen() |
| 104 |
{ |
| 105 |
localOpenCallCount++; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
void localClose() |
| 110 |
{ |
| 111 |
localCloseCallCount++; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
protected ConnectionImpl getConnectionImpl() |
| 116 |
{ |
| 117 |
return connectionImpl; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
void doFree() { } |
| 122 |
|
| 123 |
@Override |
| 124 |
void postFinal() { } |
| 125 |
|
| 126 |
public int getLocalOpenCallCount() |
| 127 |
{ |
| 128 |
return localOpenCallCount; |
| 129 |
} |
| 130 |
|
| 131 |
public int getLocalCloseCallCount() |
| 132 |
{ |
| 133 |
return localCloseCallCount; |
| 134 |
} |
| 135 |
} |
| 136 |
} |