From b93a4807ef813ccb49ce22670badb7a5e50e0020 Mon Sep 17 00:00:00 2001 From: Sai Gandham Date: Thu, 12 Apr 2018 21:19:04 +0000 Subject: [PATCH] Adding more Junits to misc env Issue-ID: AAF-129 Change-Id: I1ef0cfe1ad6d8a71981ee8083acef992a5b32e6b Signed-off-by: Sai Gandham --- .../org/onap/aaf/misc/env/JU_APIExceptionTest.java | 71 ++++++++ .../org/onap/aaf/misc/env/JU_BasicTransTest.java | 109 +++++++++++++ .../onap/aaf/misc/env/impl/JU_EnvFactoryTest.java | 79 +++++++++ .../onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java | 180 +++++++++++++++++++++ .../misc/env/util/JU_DoubleOutputStreamTest.java | 104 ++++++++++++ .../misc/env/util/JU_IndentPrintWriterTest.java | 113 +++++++++++++ .../env/util/JU_StringBuilderOutputStreamTest.java | 135 ++++++++++++++++ .../misc/env/util/JU_StringBuilderWriterTest.java | 135 ++++++++++++++++ .../aaf/misc/env/util/test/JU_IPValidator.java | 137 ++++++++-------- .../onap/aaf/misc/env/util/test/JU_PoolTest.java | 81 ++++++++++ 10 files changed, 1074 insertions(+), 70 deletions(-) create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/JU_APIExceptionTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/JU_BasicTransTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_EnvFactoryTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_DoubleOutputStreamTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_IndentPrintWriterTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderWriterTest.java create mode 100644 misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_PoolTest.java diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_APIExceptionTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_APIExceptionTest.java new file mode 100644 index 00000000..b0c60878 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_APIExceptionTest.java @@ -0,0 +1,71 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class JU_APIExceptionTest { + + private static final String EXCEPTION_MESSAGE = "New API Exception for test"; + + @Before + public void setUp() throws Exception { + } + + @Test + public void testNewAPIExceptionWithMessage() { + APIException exception = new APIException(EXCEPTION_MESSAGE); + + assertEquals(exception.getMessage(), EXCEPTION_MESSAGE); + } + + @Test + public void testNewAPIExceptionCreatedWithMessageAndThrowable() { + Throwable throwable = new Throwable(); + APIException exception = new APIException(EXCEPTION_MESSAGE, throwable); + + assertEquals(exception.getMessage(), EXCEPTION_MESSAGE); + assertEquals(exception.getCause(), throwable); + } + + @Test + public void testNewAPIExceptionCreatedWithThrowable() { + Throwable throwable = new Throwable(); + APIException exception = new APIException(throwable); + + assertEquals(exception.getCause(), throwable); + } + + @Test + public void testPayloadSetter() { + Throwable throwable = new Throwable(); + Object payload = new Object(); + + APIException exception = new APIException(throwable); + + exception.setPayload(payload); + + assertEquals(exception.getPayload(), payload); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BasicTransTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BasicTransTest.java new file mode 100644 index 00000000..6a090167 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BasicTransTest.java @@ -0,0 +1,109 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.misc.env.impl.BasicTrans; + +@RunWith(MockitoJUnitRunner.class) +public class JU_BasicTransTest { + + BasicTrans trans = null; + + @Mock + private EnvJAXB env; + + @Mock + private TimeTaken timeTaken; + + @Before + public void setUp() throws Exception { + trans = new BasicTrans(env); + } + + @Test + public void testSlot() { + Slot slot = new Slot(1, "XML"); + when(env.slot("XML")).thenReturn(slot); + + Slot outputSlot = trans.slot("XML"); + Object[] state = new Object[2]; + + slot.put(state, "JSON"); + + assertEquals(slot.get(state), "JSON"); + assertEquals(slot.getKey(), outputSlot.getKey()); + assertEquals(slot.toString(), outputSlot.toString()); + } + + @Test + public void testGetStaticSlot() { + StaticSlot staticSlot = new StaticSlot(1, "XML"); + when(env.get(staticSlot)).thenReturn(staticSlot.toString()); + + assertEquals(staticSlot.toString(), trans.get(staticSlot)); + } + + @Test + public void testGetStaticSlotWithT() { + StaticSlot staticSlot = new StaticSlot(1, "XML"); + when(env.get(staticSlot, "XML")).thenReturn(staticSlot.getKey()); + + assertEquals(staticSlot.getKey(), trans.get(staticSlot, "XML")); + } + + @Test + public void testSetProperty() { + String tag = "tag"; + String value = "value"; + String defltValue = "diffValue"; + when(env.setProperty(tag, value)).thenReturn(value); + when(env.getProperty(tag)).thenReturn(value); + when(env.getProperty(tag, defltValue)).thenReturn(defltValue); + + assertEquals(value, trans.setProperty(tag, value)); + assertEquals(value, trans.getProperty(tag)); + assertEquals(defltValue, trans.getProperty(tag, defltValue)); + } + + @Test + public void testDecryptor() { + when(env.decryptor()).thenReturn(Decryptor.NULL); + + assertEquals(Decryptor.NULL, trans.decryptor()); + assertEquals("tag", trans.decryptor().decrypt("tag")); + } + + @Test + public void testEncryptor() { + when(env.encryptor()).thenReturn(Encryptor.NULL); + + assertEquals(Encryptor.NULL, trans.encryptor()); + assertEquals("tag", trans.encryptor().encrypt("tag")); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_EnvFactoryTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_EnvFactoryTest.java new file mode 100644 index 00000000..f6c6912d --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_EnvFactoryTest.java @@ -0,0 +1,79 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; +import org.onap.aaf.misc.env.EnvJAXB; +import org.onap.aaf.misc.env.TransCreate; +import org.onap.aaf.misc.env.TransJAXB; + +public class JU_EnvFactoryTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testSingleton() { + BasicEnv singleton = EnvFactory.singleton(); + + assertEquals(EnvFactory.singleton, singleton); + } + + @Test + public void testSetSingleton() { + String[] str = { "argument1" }; + BasicEnv env = new BasicEnv("tag", str); + EnvFactory.setSingleton(env); + + assertEquals(EnvFactory.singleton(), env); + } + + @Test + public void testNewTrans() { + TransJAXB newTrans = EnvFactory.newTrans(); + + assertTrue(newTrans instanceof BasicTrans); + } + + @Test + public void testNewTransEnvJAXB() { + EnvJAXB env = new BasicEnv(""); + + TransJAXB trans = EnvFactory.newTrans(env); + + assertTrue(trans instanceof BasicTrans); + } + + @Test + public void testTransCreator() { + TransCreate transCreator = EnvFactory.transCreator(); + + TransJAXB newTrans = transCreator.newTrans(); + + assertTrue(newTrans instanceof BasicTrans); + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java new file mode 100644 index 00000000..80de9b7b --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java @@ -0,0 +1,180 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.jaxb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.EnvJAXB; +import org.onap.aaf.misc.env.IOStringifier; +import org.onap.aaf.misc.env.old.Objectifier; +import org.onap.aaf.misc.env.old.Stringifier; + +public class JU_JAXBDataTest { + + @Mock + private Objectifier objfr; + + private String object = "Text"; + + @Mock + private Stringifier strfr; + + @Mock + private IOStringifier ioStrfr; + + @Mock + private JAXBDF df; + + @Mock + private Env env; + + @Mock + private Class typeClass; + + @Mock + private OutputStream os; + + @Mock + private Writer writer; + + @Mock + private EnvJAXB env1; + + @Before + public void setUp() throws Exception { + writer = mock(Writer.class); + os = mock(OutputStream.class); + strfr = mock(Stringifier.class); + ioStrfr = mock(IOStringifier.class); + objfr = mock(Objectifier.class); + env1 = mock(EnvJAXB.class); + } + + @Test + public void testJAXBDataEnv() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object, typeClass); + + when(objfr.objectify(env, object)).thenReturn("String1"); + + jaxb.to(os); + jaxb.to(writer); + + verify(writer).write(object); + verify(os).write(object.getBytes()); + + assertEquals(jaxb.asString(), object); + assertEquals(jaxb.asString(null), object); + assertEquals(jaxb.toString(), object); + assertEquals(jaxb.getTypeClass(), typeClass); + assertEquals(jaxb.out(null), jaxb); + assertEquals(jaxb.in(null), jaxb); + assertTrue(jaxb.getInputStream() instanceof ByteArrayInputStream); + assertEquals(jaxb.asObject(), "String1"); + assertEquals(jaxb.asObject(env1), "String1"); + assertEquals(jaxb.toString(), object); + } + + @Test + public void testJAXBDataEnvForObjectifier() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object, typeClass); + + when(objfr.objectify(env1, object)).thenReturn("String1"); + + assertEquals(jaxb.asObject(env1), "String1"); + } + + @Test + public void testJAXBDataEnvWithObject() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + jaxb.to(os); + + verify(os).write(object.getBytes()); + + assertEquals(jaxb.asString(), object); + assertEquals(jaxb.asString(null), object); + assertEquals(jaxb.toString(), object); + } + + @Test + public void testJAXBDataEnvForWriter() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + jaxb.to(writer); + + verify(writer).write(object); + + assertEquals(jaxb.asString(), object); + assertEquals(jaxb.asString(null), object); + assertEquals(jaxb.toString(), object); + assertEquals(jaxb.asObject(), object); + assertEquals(jaxb.asObject(null), object); + } + + @Test + public void testAsStringWithNullString() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + assertEquals(jaxb.asString(), object); + } + + @Test + public void testAsStringWithNullStringWithEnv() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env1, object)).thenReturn(object); + + assertEquals(jaxb.asString(env1), object); + } + + @Test + public void testToWithIOStrifier() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + jaxb.option(0); + + when(strfr.stringify(env1, object)).thenReturn(object); + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + assertTrue(jaxb.getInputStream() instanceof ByteArrayInputStream); + assertEquals(jaxb.asString(env1), object); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_DoubleOutputStreamTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_DoubleOutputStreamTest.java new file mode 100644 index 00000000..4b8c9dce --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_DoubleOutputStreamTest.java @@ -0,0 +1,104 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.util; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.io.OutputStream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class JU_DoubleOutputStreamTest { + + @Mock + private OutputStream stream1; + + @Mock + private OutputStream stream2; + + private DoubleOutputStream doubleOutputStream; + + @Before + public void setup() { + stream1 = mock(OutputStream.class); + stream2 = mock(OutputStream.class); + } + + @Test + public void testWriteInt() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + doubleOutputStream.write(123); + + verify(stream1, only()).write(123); + verify(stream2, only()).write(123); + } + + @Test + public void testWriteByteArray() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + byte[] bytes = { 1, 2, 3, 4 }; + + doubleOutputStream.write(bytes); + + verify(stream1, only()).write(bytes); + verify(stream2, only()).write(bytes); + + } + + @Test + public void testWriteByteArrayWithOffset() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + byte[] bytes = { 1, 2, 3, 4 }; + + doubleOutputStream.write(bytes, 1, 3); + verify(stream1, only()).write(bytes, 1, 3); + verify(stream2, only()).write(bytes, 1, 3); + } + + @Test + public void testFlush() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + doubleOutputStream.flush(); + + verify(stream1, only()).flush(); + verify(stream2, only()).flush(); + } + + @Test + public void testClose() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, false); + + doubleOutputStream.close(); + + verify(stream1, only()).close(); + verify(stream2, never()).close(); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_IndentPrintWriterTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_IndentPrintWriterTest.java new file mode 100644 index 00000000..b54026f1 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_IndentPrintWriterTest.java @@ -0,0 +1,113 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.util; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class JU_IndentPrintWriterTest { + + @Mock + private OutputStream stream; + + @Mock + private Writer writer; + + @Before + public void setUp() throws Exception { + stream = mock(OutputStream.class); + writer = mock(Writer.class); + } + + @Test + public void testWriteInt() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.write(123); + + verify(writer).write(123); + + assertEquals(indentWriter.getIndent(), 0); + } + + @Test + public void testWriteIntWithNewLineCharacter() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.setIndent(12); + + indentWriter.println(); + + indentWriter.write("123", 1, 2); + + verify(writer).write('\n'); + verify(writer).write('2'); + verify(writer).write('3'); + assertEquals(indentWriter.getIndent(), 12); + } + + @Test + public void testWriteString() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.inc(); + + indentWriter.write("123"); + + verify(writer).write('1'); + verify(writer).write('2'); + verify(writer).write('3'); + assertEquals(indentWriter.getIndent(), 1); + } + + @Test + public void testSetIndent() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(stream); + + indentWriter.setIndent(12); + indentWriter.dec(); + + assertEquals(indentWriter.getIndent(), 11); + } + + @Test + public void testToCol() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.toCol(5); + char[] chars = { 'a', 'b', 'c' }; + indentWriter.write(chars, 1, 2); + + verify(writer, times(5)).write(' '); + verify(writer).write('c'); + verify(writer).write('b'); + } +} \ No newline at end of file diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java new file mode 100644 index 00000000..377a2891 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java @@ -0,0 +1,135 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +public class JU_StringBuilderOutputStreamTest { + + StringBuilderOutputStream streamBuilder; + + StringBuilder builder = new StringBuilder(); + + @Before + public void setUp() throws Exception { + streamBuilder = new StringBuilderOutputStream(builder); + } + + @Test + public void testWriteIntAndReset() { + streamBuilder.write(123); + + assertEquals("123", streamBuilder.toString()); + streamBuilder.reset(); + assertEquals("", streamBuilder.toString()); + } + + @Test + public void testWriteByteArrayWithoutException() throws IOException { + byte[] bytes = { 1, 2, 3, 4 }; + streamBuilder.write(bytes); + assertEquals(4, streamBuilder.getBuffer().length()); + + streamBuilder.write(bytes, 1, 2); + assertEquals(6, streamBuilder.getBuffer().length()); + + streamBuilder.write(bytes, 1, 0); + assertEquals(6, streamBuilder.getBuffer().length()); + + streamBuilder.append(bytes[0]); + assertEquals(7, streamBuilder.getBuffer().length()); + } + + @Test + public void testWriteByteArrayWithIndexOutOfBoundException() { + byte[] bytes = { 1, 2, 3, 4 }; + + try { + streamBuilder.write(bytes, -1, 2); + fail("This is supposed to throw IndexOutOfBounds Excetpion"); + } catch (IndexOutOfBoundsException e) { + } catch (Exception e) { + fail("This should throw only IndexOutOfBounds Exception"); + } + assertEquals(0, streamBuilder.getBuffer().length()); + + } + + @Test + public void testDefaultConstructor() throws IOException { + StringBuilderOutputStream stream = new StringBuilderOutputStream(); + + assertNotNull(stream.getBuffer()); + stream.close(); + } + + @Test + public void testConstructorWithPositiveDefaultCapacity() throws IOException { + StringBuilderOutputStream stream = new StringBuilderOutputStream(10); + + assertNotNull(stream.getBuffer()); + assertEquals(10, stream.getBuffer().capacity()); + stream.close(); + } + + @Test + public void testConstructorWithNegativeCapacityException() { + try { + StringBuilderOutputStream stream = new StringBuilderOutputStream(-1); + fail("This should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } catch (Exception e) { + fail("This should throw only IllegalArgumentException"); + } + } + + @Test + public void testWriteString() { + streamBuilder.write("1234"); + + assertEquals("1234", streamBuilder.toString()); + + streamBuilder.write("1234", 1, 2); + assertEquals("12342", streamBuilder.toString()); + } + + @Test + public void testAppendCharSequence() { + streamBuilder.append("1234"); + assertEquals("1234", streamBuilder.toString()); + + streamBuilder.append(null); + assertEquals("1234null", streamBuilder.toString()); + + streamBuilder.append("1234", 1, 2); + assertEquals("1234null2", streamBuilder.toString()); + + streamBuilder.append(null, 1, 2); + assertEquals("1234null2u", streamBuilder.toString()); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderWriterTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderWriterTest.java new file mode 100644 index 00000000..6a06e866 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderWriterTest.java @@ -0,0 +1,135 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +public class JU_StringBuilderWriterTest { + + StringBuilderWriter streamWriter; + + StringBuilder builder = new StringBuilder(); + + @Before + public void setUp() throws Exception { + streamWriter = new StringBuilderWriter(builder); + } + + @Test + public void testWriteIntAndReset() { + streamWriter.write(1); + + assertEquals(1, streamWriter.getBuffer().length()); + streamWriter.reset(); + assertEquals("", streamWriter.toString()); + } + + @Test + public void testWriteByteArrayWithoutException() throws IOException { + char[] bytes = { 1, 2, 3, 4 }; + streamWriter.write(bytes); + assertEquals(4, streamWriter.getBuffer().length()); + + streamWriter.write(bytes, 1, 2); + assertEquals(6, streamWriter.getBuffer().length()); + + streamWriter.write(bytes, 1, 0); + assertEquals(6, streamWriter.getBuffer().length()); + + streamWriter.append(bytes[0]); + assertEquals(7, streamWriter.getBuffer().length()); + } + + @Test + public void testWriteByteArrayWithIndexOutOfBoundException() { + char[] bytes = { 1, 2, 3, 4 }; + + try { + streamWriter.write(bytes, -1, 2); + fail("This is supposed to throw IndexOutOfBounds Excetpion"); + } catch (IndexOutOfBoundsException e) { + } catch (Exception e) { + fail("This should throw only IndexOutOfBounds Exception"); + } + assertEquals(0, streamWriter.getBuffer().length()); + + } + + @Test + public void testDefaultConstructor() throws IOException { + StringBuilderWriter stream = new StringBuilderWriter(); + + assertNotNull(stream.getBuffer()); + stream.close(); + } + + @Test + public void testConstructorWithPositiveDefaultCapacity() throws IOException { + StringBuilderWriter stream = new StringBuilderWriter(10); + + assertNotNull(stream.getBuffer()); + assertEquals(10, stream.getBuffer().capacity()); + stream.close(); + } + + @Test + public void testConstructorWithNegativeCapacityException() { + try { + StringBuilderWriter stream = new StringBuilderWriter(-1); + fail("This should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } catch (Exception e) { + fail("This should throw only IllegalArgumentException"); + } + } + + @Test + public void testWriteString() { + streamWriter.write("1234"); + + assertEquals("1234", streamWriter.toString()); + + streamWriter.write("1234", 1, 2); + assertEquals("123423", streamWriter.toString()); + } + + @Test + public void testAppendCharSequence() { + streamWriter.append("1234"); + assertEquals("1234", streamWriter.toString()); + + streamWriter.append(null); + assertEquals("1234null", streamWriter.toString()); + + streamWriter.append("1234", 1, 2); + assertEquals("1234null2", streamWriter.toString()); + + streamWriter.append(null, 1, 2); + assertEquals("1234null2u", streamWriter.toString()); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java index 01acf218..3976718f 100644 --- a/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java @@ -1,70 +1,67 @@ -/** - * ============LICENSE_START==================================================== - * org.onap.aaf - * =========================================================================== - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * =========================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END==================================================== - * - */ - -package org.onap.aaf.misc.env.util.test; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; - -import org.junit.Test; -import org.onap.aaf.misc.env.util.IPValidator; - -public class JU_IPValidator { - - @Test - public void test() { - assertTrue(IPValidator.ipv4("10.10.10.10")); - assertTrue(IPValidator.ipv4("127.0.0.0")); - assertFalse(IPValidator.ipv4("10")); - assertFalse(IPValidator.ipv4("10.10.10")); - assertFalse(IPValidator.ipv4("10.10.10.")); - assertFalse(IPValidator.ipv4("10.10.10.10.")); - assertFalse(IPValidator.ipv4("10.10.10.10.10")); - assertFalse(IPValidator.ipv4("something10.10.10.10")); - assertTrue(IPValidator.ipv4("0.10.10.10")); - assertTrue(IPValidator.ipv4("0.0.0.0")); - assertTrue(IPValidator.ipv4("0.10.10.10")); - assertFalse(IPValidator.ipv4("011.255.255.255")); - assertFalse(IPValidator.ipv4("255.01.255.255")); - assertFalse(IPValidator.ipv4("255.255.255.256")); - assertFalse(IPValidator.ipv4("255.299.255.255")); - - - assertTrue(IPValidator.ipv6("0000:0000:0000:0000:0000:0000:0000:0000")); - assertTrue(IPValidator.ipv6("0:0:0:0:0:0:0:0")); - assertTrue(IPValidator.ipv6("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); - assertTrue(IPValidator.ipv6("2001:8DB:0:0:23:F422:FE3B:AC10")); - assertTrue(IPValidator.ipv6("2001:8DB::23:F422:FE3B:AC10")); - assertTrue(IPValidator.ipv6("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - assertTrue(IPValidator.ipv6("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")); - assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); - assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); - // more than one Double Colons - assertFalse(IPValidator.ipv6("0000:0000:0000::0000::0000")); - assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10:FFFF")); - - - - assertTrue(IPValidator.ip("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); - assertTrue(IPValidator.ip("192.168.7.2")); - } - -} +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.util.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.aaf.misc.env.util.IPValidator; + +public class JU_IPValidator { + + @Test + public void test() { + assertTrue(IPValidator.ipv4("10.10.10.10")); + assertTrue(IPValidator.ipv4("127.0.0.0")); + assertFalse(IPValidator.ipv4("10")); + assertFalse(IPValidator.ipv4("10.10.10")); + assertFalse(IPValidator.ipv4("10.10.10.")); + assertFalse(IPValidator.ipv4("10.10.10.10.")); + assertFalse(IPValidator.ipv4("10.10.10.10.10")); + assertFalse(IPValidator.ipv4("something10.10.10.10")); + assertTrue(IPValidator.ipv4("0.10.10.10")); + assertTrue(IPValidator.ipv4("0.0.0.0")); + assertTrue(IPValidator.ipv4("0.10.10.10")); + assertFalse(IPValidator.ipv4("011.255.255.255")); + assertFalse(IPValidator.ipv4("255.01.255.255")); + assertFalse(IPValidator.ipv4("255.255.255.256")); + assertFalse(IPValidator.ipv4("255.299.255.255")); + + assertTrue(IPValidator.ipv6("0000:0000:0000:0000:0000:0000:0000:0000")); + assertTrue(IPValidator.ipv6("0:0:0:0:0:0:0:0")); + assertTrue(IPValidator.ipv6("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); + assertTrue(IPValidator.ipv6("2001:8DB:0:0:23:F422:FE3B:AC10")); + assertTrue(IPValidator.ipv6("2001:8DB::23:F422:FE3B:AC10")); + assertTrue(IPValidator.ipv6("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); + assertTrue(IPValidator.ipv6("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")); + assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); + assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); + // more than one Double Colons + assertFalse(IPValidator.ipv6("0000:0000:0000::0000::0000")); + assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10:FFFF")); + + assertTrue(IPValidator.ip("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); + assertTrue(IPValidator.ip("192.168.7.2")); + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_PoolTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_PoolTest.java new file mode 100644 index 00000000..e3f90de1 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_PoolTest.java @@ -0,0 +1,81 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env.util.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.util.Pool; + +public class JU_PoolTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void test() { + Pool pool = new Pool(new Pool.Creator() { + + Integer content = 0; + + @Override + public Integer create() throws APIException { + return content++; + } + + @Override + public void destroy(Integer t) { + + } + + @Override + public boolean isValid(Integer t) { + return t == content; + } + + @Override + public void reuse(Integer t) { + content = t; + } + }); + try { + // pool.drain(); + assertEquals("Should return intial value", 0, pool.get().content); + // pooled.toss(); + pool.prime(LogTarget.SYSOUT, 23); + assertEquals("Should Return 23 as added at last prime", 23, pool.get(LogTarget.SYSOUT).content); + pool.prime(LogTarget.SYSERR, 13); + assertEquals("Should add another 13 from SysErr and remove 1", 35, pool.get(LogTarget.SYSERR).content); + assertEquals("Create a new creator with create method", 1, pool.get().content); + assertEquals("Create a new creator with create method", 2, pool.get().content); + assertEquals("Should remove last from pool", 34, pool.get(LogTarget.SYSOUT).content); + + pool.drain(); + assertEquals("Should remove last from pool", 17, pool.get(LogTarget.SYSOUT).content); + } catch (APIException e) { + } + } + +} -- 2.16.6