LOG4J2-1883 moved JPA classed into new module
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f8705e65 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f8705e65 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f8705e65 Branch: refs/heads/master Commit: f8705e65849832748f9ab5c330e7da5781368b27 Parents: 87ad487 Author: rpopma <[email protected]> Authored: Sun Jan 28 20:56:36 2018 +0900 Committer: rpopma <[email protected]> Committed: Sun Jan 28 20:56:36 2018 +0900 ---------------------------------------------------------------------- .../converter/InstantAttributeConverter.java | 57 ----------------- .../InstantAttributeConverterTest.java | 67 -------------------- .../converter/InstantAttributeConverter.java | 57 +++++++++++++++++ .../InstantAttributeConverterTest.java | 67 ++++++++++++++++++++ 4 files changed, 124 insertions(+), 124 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8705e65/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java deleted file mode 100644 index 9e0120e..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ -package org.apache.logging.log4j.core.appender.db.jpa.converter; - -import org.apache.logging.log4j.core.time.Instant; -import org.apache.logging.log4j.core.time.MutableInstant; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.Strings; - -import javax.persistence.AttributeConverter; -import javax.persistence.Converter; - -/** - * A JPA 2.1 attribute converter for {@link Instant}s in {@link org.apache.logging.log4j.core.LogEvent}s. This - * converter is capable of converting both to and from {@link String}s. - */ -@Converter(autoApply = false) -public class InstantAttributeConverter implements AttributeConverter<Instant, String> { - private static final StatusLogger LOGGER = StatusLogger.getLogger(); - - @Override - public String convertToDatabaseColumn(final Instant instant) { - if (instant == null) { - return null; - } - return instant.getEpochSecond() + "," + instant.getNanoOfSecond(); - } - - @Override - public Instant convertToEntityAttribute(final String s) { - if (Strings.isEmpty(s)) { - return null; - } - - int pos = s.indexOf(","); - long epochSecond = Long.parseLong(s.substring(0, pos)); - int nanos = Integer.parseInt(s.substring(pos + 1, s.length())); - - MutableInstant result = new MutableInstant(); - result.initFromEpochSecond(epochSecond, nanos); - return result; - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8705e65/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java deleted file mode 100644 index f4970a9..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ -package org.apache.logging.log4j.core.appender.db.jpa.converter; - -import org.apache.logging.log4j.categories.Appenders; -import org.apache.logging.log4j.core.time.Instant; -import org.apache.logging.log4j.core.time.MutableInstant; -import org.apache.logging.log4j.status.StatusLogger; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import static org.junit.Assert.*; - -@Category(Appenders.Jpa.class) -public class InstantAttributeConverterTest { - private static final StatusLogger LOGGER = StatusLogger.getLogger(); - - private InstantAttributeConverter converter; - - @Before - public void setUp() { - this.converter = new InstantAttributeConverter(); - } - - @Test - public void testConvert01() { - final MutableInstant instant = new MutableInstant(); - instant.initFromEpochSecond(1234567, 89012); - - final String converted = this.converter.convertToDatabaseColumn(instant); - - assertNotNull("The converted value should not be null.", converted); - assertEquals("The converted value is not correct.", "1234567,89012", converted); - - final Instant reversed = this.converter.convertToEntityAttribute(converted); - - assertNotNull("The reversed value should not be null.", reversed); - assertEquals("epoch sec", 1234567, reversed.getEpochSecond()); - assertEquals("nanoOfSecond", 89012, reversed.getNanoOfSecond()); - } - - @Test - public void testConvertNullToDatabaseColumn() { - assertNull("The converted value should be null.", this.converter.convertToDatabaseColumn(null)); - } - - @Test - public void testConvertNullOrBlankToEntityAttribute() { - assertNull("The converted attribute should be null (1).", this.converter.convertToEntityAttribute(null)); - assertNull("The converted attribute should be null (2).", this.converter.convertToEntityAttribute("")); - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8705e65/log4j-jpa/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java ---------------------------------------------------------------------- diff --git a/log4j-jpa/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java b/log4j-jpa/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java new file mode 100644 index 0000000..9e0120e --- /dev/null +++ b/log4j-jpa/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverter.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.logging.log4j.core.appender.db.jpa.converter; + +import org.apache.logging.log4j.core.time.Instant; +import org.apache.logging.log4j.core.time.MutableInstant; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.Strings; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +/** + * A JPA 2.1 attribute converter for {@link Instant}s in {@link org.apache.logging.log4j.core.LogEvent}s. This + * converter is capable of converting both to and from {@link String}s. + */ +@Converter(autoApply = false) +public class InstantAttributeConverter implements AttributeConverter<Instant, String> { + private static final StatusLogger LOGGER = StatusLogger.getLogger(); + + @Override + public String convertToDatabaseColumn(final Instant instant) { + if (instant == null) { + return null; + } + return instant.getEpochSecond() + "," + instant.getNanoOfSecond(); + } + + @Override + public Instant convertToEntityAttribute(final String s) { + if (Strings.isEmpty(s)) { + return null; + } + + int pos = s.indexOf(","); + long epochSecond = Long.parseLong(s.substring(0, pos)); + int nanos = Integer.parseInt(s.substring(pos + 1, s.length())); + + MutableInstant result = new MutableInstant(); + result.initFromEpochSecond(epochSecond, nanos); + return result; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f8705e65/log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java ---------------------------------------------------------------------- diff --git a/log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java b/log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java new file mode 100644 index 0000000..f4970a9 --- /dev/null +++ b/log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/InstantAttributeConverterTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.logging.log4j.core.appender.db.jpa.converter; + +import org.apache.logging.log4j.categories.Appenders; +import org.apache.logging.log4j.core.time.Instant; +import org.apache.logging.log4j.core.time.MutableInstant; +import org.apache.logging.log4j.status.StatusLogger; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static org.junit.Assert.*; + +@Category(Appenders.Jpa.class) +public class InstantAttributeConverterTest { + private static final StatusLogger LOGGER = StatusLogger.getLogger(); + + private InstantAttributeConverter converter; + + @Before + public void setUp() { + this.converter = new InstantAttributeConverter(); + } + + @Test + public void testConvert01() { + final MutableInstant instant = new MutableInstant(); + instant.initFromEpochSecond(1234567, 89012); + + final String converted = this.converter.convertToDatabaseColumn(instant); + + assertNotNull("The converted value should not be null.", converted); + assertEquals("The converted value is not correct.", "1234567,89012", converted); + + final Instant reversed = this.converter.convertToEntityAttribute(converted); + + assertNotNull("The reversed value should not be null.", reversed); + assertEquals("epoch sec", 1234567, reversed.getEpochSecond()); + assertEquals("nanoOfSecond", 89012, reversed.getNanoOfSecond()); + } + + @Test + public void testConvertNullToDatabaseColumn() { + assertNull("The converted value should be null.", this.converter.convertToDatabaseColumn(null)); + } + + @Test + public void testConvertNullOrBlankToEntityAttribute() { + assertNull("The converted attribute should be null (1).", this.converter.convertToEntityAttribute(null)); + assertNull("The converted attribute should be null (2).", this.converter.convertToEntityAttribute("")); + } +}
