|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Otávio Santana and others |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * and Apache License v2.0 which accompanies this distribution. |
| 6 | + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. |
| 8 | + * |
| 9 | + * You may elect to redistribute this code under either of these licenses. |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * |
| 13 | + * Otavio Santana |
| 14 | + */ |
| 15 | + |
| 16 | +package org.jnosql.diana.redis.key; |
| 17 | + |
| 18 | + |
| 19 | +import org.jnosql.diana.api.key.BucketManagerFactory; |
| 20 | +import org.junit.After; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.util.NoSuchElementException; |
| 25 | +import java.util.Queue; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertEquals; |
| 28 | +import static org.junit.Assert.assertNotNull; |
| 29 | +import static org.junit.Assert.assertNull; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | + |
| 32 | + |
| 33 | +public class RedisQueueStringTest { |
| 34 | + |
| 35 | + |
| 36 | + private BucketManagerFactory keyValueEntityManagerFactory; |
| 37 | + |
| 38 | + private Queue<String> lineBank; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void init() { |
| 42 | + |
| 43 | + keyValueEntityManagerFactory = RedisTestUtils.get(); |
| 44 | + lineBank = keyValueEntityManagerFactory.getQueue("physical-bank-string", String.class); |
| 45 | + |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void shouldPushInTheLine() { |
| 51 | + assertTrue(lineBank.add("Otavio")); |
| 52 | + assertTrue(lineBank.size() == 1); |
| 53 | + String otavio = lineBank.poll(); |
| 54 | + assertEquals(otavio, "Otavio"); |
| 55 | + assertNull(lineBank.poll()); |
| 56 | + assertTrue(lineBank.isEmpty()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void shouldPeekInTheLine() { |
| 61 | + lineBank.add("Otavio"); |
| 62 | + String otavio = lineBank.peek(); |
| 63 | + assertNotNull(otavio); |
| 64 | + assertNotNull(lineBank.peek()); |
| 65 | + String otavio2 = lineBank.remove(); |
| 66 | + assertEquals(otavio, otavio2); |
| 67 | + boolean happendException = false; |
| 68 | + try { |
| 69 | + lineBank.remove(); |
| 70 | + } catch (NoSuchElementException e) { |
| 71 | + happendException = true; |
| 72 | + } |
| 73 | + assertTrue(happendException); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void shouldElementInTheLine() { |
| 78 | + lineBank.add("Otavio"); |
| 79 | + assertNotNull(lineBank.element()); |
| 80 | + assertNotNull(lineBank.element()); |
| 81 | + lineBank.remove("Otavio"); |
| 82 | + boolean happendException = false; |
| 83 | + try { |
| 84 | + lineBank.element(); |
| 85 | + } catch (NoSuchElementException e) { |
| 86 | + happendException = true; |
| 87 | + } |
| 88 | + assertTrue(happendException); |
| 89 | + } |
| 90 | + |
| 91 | + @SuppressWarnings("unused") |
| 92 | + @Test |
| 93 | + public void shouldIterate() { |
| 94 | + lineBank.add("Otavio"); |
| 95 | + lineBank.add("Gama"); |
| 96 | + int count = 0; |
| 97 | + for (String line : lineBank) { |
| 98 | + count++; |
| 99 | + } |
| 100 | + assertTrue(count == 2); |
| 101 | + lineBank.remove(); |
| 102 | + lineBank.remove(); |
| 103 | + count = 0; |
| 104 | + for (String line : lineBank) { |
| 105 | + count++; |
| 106 | + } |
| 107 | + assertTrue(count == 0); |
| 108 | + } |
| 109 | + |
| 110 | + @After |
| 111 | + public void dispose() { |
| 112 | + lineBank.clear(); |
| 113 | + } |
| 114 | +} |
0 commit comments