diff --git a/test/org/apache/catalina/storeconfig/TestStoreAppender.java b/test/org/apache/catalina/storeconfig/TestStoreAppender.java new file mode 100644 index 000000000000..61adb14125dc --- /dev/null +++ b/test/org/apache/catalina/storeconfig/TestStoreAppender.java @@ -0,0 +1,295 @@ +/* + * 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.catalina.storeconfig; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link StoreAppender}. + */ +public class TestStoreAppender { + + @Test + public void testPrintCloseTag() throws Exception { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + StoreDescription desc = new StoreDescription(); + desc.setTag("Server"); + + appender.printCloseTag(pw, desc); + pw.flush(); + + Assert.assertEquals("" + System.lineSeparator(), sw.toString()); + } + + + @Test + public void testPrintOpenTag() throws Exception { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + StoreDescription desc = new StoreDescription(); + desc.setTag("Server"); + desc.setAttributes(false); + + appender.printOpenTag(pw, 0, null, desc); + pw.flush(); + + Assert.assertEquals("" + System.lineSeparator(), sw.toString()); + } + + + @Test + public void testPrintTag() throws Exception { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + StoreDescription desc = new StoreDescription(); + desc.setTag("Listener"); + desc.setAttributes(false); + + appender.printTag(pw, 0, null, desc); + pw.flush(); + + Assert.assertEquals("" + System.lineSeparator(), sw.toString()); + } + + + @Test + public void testPrintTagContent() throws Exception { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + appender.printTagContent(pw, "WatchedResource", "WEB-INF/web.xml"); + pw.flush(); + + Assert.assertEquals("WEB-INF/web.xml" + + System.lineSeparator(), sw.toString()); + } + + + @Test + public void testPrintTagContentWithSpecialChars() throws Exception { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + appender.printTagContent(pw, "Value", "avalue1")); + Assert.assertTrue(result.contains("value2")); + } + + + @Test + public void testPrintTagArrayNull() throws Exception { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + appender.printTagArray(pw, "Element", 0, null); + pw.flush(); + + Assert.assertEquals("", sw.toString()); + } + + + @Test + public void testPrintTagValueArray() { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + String[] elements = {"TLSv1.2", "TLSv1.3"}; + appender.printTagValueArray(pw, "Protocols", 0, elements); + pw.flush(); + + String result = sw.toString(); + Assert.assertTrue(result.contains("")); + Assert.assertTrue(result.contains("TLSv1.2")); + Assert.assertTrue(result.contains("TLSv1.3")); + Assert.assertTrue(result.contains("")); + } + + + @Test + public void testPrintTagValueArrayNull() { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + appender.printTagValueArray(pw, "Protocols", 0, null); + pw.flush(); + + Assert.assertEquals("", sw.toString()); + } + + + @Test + public void testPrintTagValueArrayEmpty() { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + appender.printTagValueArray(pw, "Protocols", 0, new String[0]); + pw.flush(); + + Assert.assertEquals("", sw.toString()); + } + + + @Test + public void testPrintValue() { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + appender.printValue(pw, 0, "port", "8080"); + pw.flush(); + + String result = sw.toString(); + Assert.assertTrue(result.contains("port=\"8080\"")); + } + + + @Test + public void testIsPrintValue() { + StoreAppender appender = new StoreAppender(); + StoreDescription desc = new StoreDescription(); + + // Default always returns true + Assert.assertTrue(appender.isPrintValue("bean", "bean2", "attr", desc)); + } + + + @Test + public void testDefaultInstance() throws Exception { + StoreAppender appender = new StoreAppender(); + + StoreDescription original = new StoreDescription(); + original.setTag("Server"); + + Object defaultObj = appender.defaultInstance(original); + + Assert.assertNotNull(defaultObj); + Assert.assertTrue(defaultObj instanceof StoreDescription); + Assert.assertNotSame(original, defaultObj); + } + + + @Test + public void testPrintOpenTagWithAttributes() throws Exception { + StoreAppender appender = new StoreAppender(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + StoreDescription desc = new StoreDescription(); + desc.setTag("Connector"); + desc.setAttributes(true); + desc.setStandard(true); + + // Use a simple bean with non-default values + SimpleBean bean = new SimpleBean(); + bean.setPort(8443); + + appender.printOpenTag(pw, 0, bean, desc); + pw.flush(); + + String result = sw.toString(); + Assert.assertTrue("Should start with ", result.trim().endsWith("/>")); + } + + + /** + * Simple JavaBean for testing attribute printing. + */ + public static class SimpleBean { + private int port = 8080; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + } +} diff --git a/test/org/apache/catalina/storeconfig/TestStoreDescription.java b/test/org/apache/catalina/storeconfig/TestStoreDescription.java new file mode 100644 index 000000000000..29a27e86c913 --- /dev/null +++ b/test/org/apache/catalina/storeconfig/TestStoreDescription.java @@ -0,0 +1,203 @@ +/* + * 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.catalina.storeconfig; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link StoreDescription}. + */ +public class TestStoreDescription { + + @Test + public void testDefaultValues() { + StoreDescription desc = new StoreDescription(); + + Assert.assertNull(desc.getTag()); + Assert.assertNull(desc.getTagClass()); + Assert.assertNull(desc.getStoreFactoryClass()); + Assert.assertNull(desc.getStoreFactory()); + Assert.assertNull(desc.getStoreWriterClass()); + Assert.assertNull(desc.getTransientAttributes()); + Assert.assertNull(desc.getTransientChildren()); + + Assert.assertFalse(desc.isStandard()); + Assert.assertFalse(desc.isBackup()); + Assert.assertFalse(desc.isExternalAllowed()); + Assert.assertFalse(desc.isExternalOnly()); + Assert.assertFalse(desc.isDefault()); + Assert.assertFalse(desc.isStoreSeparate()); + Assert.assertFalse(desc.isChildren()); + Assert.assertTrue(desc.isAttributes()); + } + + + @Test + public void testGetSetProperties() { + StoreDescription desc = new StoreDescription(); + + desc.setTag("Server"); + Assert.assertEquals("Server", desc.getTag()); + + desc.setTagClass("org.apache.catalina.core.StandardServer"); + Assert.assertEquals("org.apache.catalina.core.StandardServer", desc.getTagClass()); + + desc.setStandard(true); + Assert.assertTrue(desc.isStandard()); + + desc.setBackup(true); + Assert.assertTrue(desc.isBackup()); + + desc.setExternalAllowed(true); + Assert.assertTrue(desc.isExternalAllowed()); + + desc.setExternalOnly(true); + Assert.assertTrue(desc.isExternalOnly()); + + desc.setDefault(true); + Assert.assertTrue(desc.isDefault()); + + desc.setAttributes(false); + Assert.assertFalse(desc.isAttributes()); + + desc.setChildren(true); + Assert.assertTrue(desc.isChildren()); + + desc.setStoreSeparate(true); + Assert.assertTrue(desc.isStoreSeparate()); + + desc.setStoreFactoryClass("org.apache.catalina.storeconfig.StandardServerSF"); + Assert.assertEquals("org.apache.catalina.storeconfig.StandardServerSF", + desc.getStoreFactoryClass()); + + desc.setStoreWriterClass("org.example.Writer"); + Assert.assertEquals("org.example.Writer", desc.getStoreWriterClass()); + } + + + @Test + public void testIdReturnsTagClassWhenIdIsNull() { + StoreDescription desc = new StoreDescription(); + desc.setTagClass("org.apache.catalina.core.StandardServer"); + + Assert.assertEquals("org.apache.catalina.core.StandardServer", desc.getId()); + + desc.setId("customId"); + Assert.assertEquals("customId", desc.getId()); + } + + + @Test + public void testTransientAttributes() { + StoreDescription desc = new StoreDescription(); + + Assert.assertFalse(desc.isTransientAttribute("available")); + + desc.addTransientAttribute("available"); + desc.addTransientAttribute("configFile"); + + Assert.assertTrue(desc.isTransientAttribute("available")); + Assert.assertTrue(desc.isTransientAttribute("configFile")); + Assert.assertFalse(desc.isTransientAttribute("unknown")); + + Assert.assertNotNull(desc.getTransientAttributes()); + Assert.assertEquals(2, desc.getTransientAttributes().size()); + + desc.removeTransientAttribute("available"); + Assert.assertFalse(desc.isTransientAttribute("available")); + Assert.assertEquals(1, desc.getTransientAttributes().size()); + } + + + @Test + public void testTransientChildren() { + StoreDescription desc = new StoreDescription(); + + Assert.assertFalse(desc.isTransientChild("org.example.Child")); + + desc.addTransientChild("org.example.Child"); + desc.addTransientChild("org.example.Other"); + + Assert.assertTrue(desc.isTransientChild("org.example.Child")); + Assert.assertTrue(desc.isTransientChild("org.example.Other")); + Assert.assertFalse(desc.isTransientChild("org.example.Unknown")); + + Assert.assertNotNull(desc.getTransientChildren()); + Assert.assertEquals(2, desc.getTransientChildren().size()); + + desc.removeTransientChild("org.example.Child"); + Assert.assertFalse(desc.isTransientChild("org.example.Child")); + } + + + @Test + public void testSetTransientAttributesList() { + StoreDescription desc = new StoreDescription(); + + List attrs = new ArrayList<>(); + attrs.add("attr1"); + attrs.add("attr2"); + desc.setTransientAttributes(attrs); + + Assert.assertEquals(attrs, desc.getTransientAttributes()); + Assert.assertTrue(desc.isTransientAttribute("attr1")); + } + + + @Test + public void testSetTransientChildrenList() { + StoreDescription desc = new StoreDescription(); + + List children = new ArrayList<>(); + children.add("child1"); + children.add("child2"); + desc.setTransientChildren(children); + + Assert.assertEquals(children, desc.getTransientChildren()); + Assert.assertTrue(desc.isTransientChild("child1")); + } + + + @Test + public void testRemoveTransientAttributeWhenNull() { + StoreDescription desc = new StoreDescription(); + // Should not throw NPE + desc.removeTransientAttribute("anything"); + } + + + @Test + public void testRemoveTransientChildWhenNull() { + StoreDescription desc = new StoreDescription(); + // Should not throw NPE + desc.removeTransientChild("anything"); + } + + + @Test + public void testStoreFactory() { + StoreDescription desc = new StoreDescription(); + StoreFactoryBase factory = new StoreFactoryBase(); + + desc.setStoreFactory(factory); + Assert.assertSame(factory, desc.getStoreFactory()); + } +} diff --git a/test/org/apache/catalina/storeconfig/TestStoreFactoryBase.java b/test/org/apache/catalina/storeconfig/TestStoreFactoryBase.java new file mode 100644 index 000000000000..57824de46c0c --- /dev/null +++ b/test/org/apache/catalina/storeconfig/TestStoreFactoryBase.java @@ -0,0 +1,204 @@ +/* + * 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.catalina.storeconfig; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link StoreFactoryBase}. + */ +public class TestStoreFactoryBase { + + @Test + public void testGetInfo() { + StoreFactoryBase factory = new StoreFactoryBase(); + Assert.assertNotNull(factory.getInfo()); + Assert.assertTrue(factory.getInfo().contains("StoreFactoryBase")); + } + + + @Test + public void testGetSetStoreAppender() { + StoreFactoryBase factory = new StoreFactoryBase(); + + Assert.assertNotNull(factory.getStoreAppender()); + + StoreAppender customAppender = new StoreAppender(); + factory.setStoreAppender(customAppender); + Assert.assertSame(customAppender, factory.getStoreAppender()); + } + + + @Test + public void testGetSetRegistry() { + StoreFactoryBase factory = new StoreFactoryBase(); + + Assert.assertNull(factory.getRegistry()); + + StoreRegistry registry = new StoreRegistry(); + factory.setRegistry(registry); + Assert.assertSame(registry, factory.getRegistry()); + } + + + @Test + public void testStoreXMLHead() { + StoreFactoryBase factory = new StoreFactoryBase(); + StoreRegistry registry = new StoreRegistry(); + factory.setRegistry(registry); + + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + factory.storeXMLHead(pw); + pw.flush(); + + String result = sw.toString(); + Assert.assertTrue(result.contains("")); + } + + + @Test + public void testStoreWithChildren() throws Exception { + StoreFactoryBase factory = new StoreFactoryBase(); + StoreRegistry registry = new StoreRegistry(); + factory.setRegistry(registry); + + StoreDescription desc = new StoreDescription(); + desc.setTag("Server"); + desc.setTagClass(SimpleBean.class.getName()); + desc.setStandard(true); + desc.setAttributes(false); + desc.setChildren(true); + desc.setStoreFactory(factory); + + registry.registerDescription(desc); + + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + factory.store(pw, 0, new SimpleBean()); + pw.flush(); + + String result = sw.toString(); + Assert.assertTrue("Should have open tag", result.contains("")); + Assert.assertTrue("Should have close tag", result.contains("")); + } + + + @Test + public void testStoreNoDescription() throws Exception { + StoreFactoryBase factory = new StoreFactoryBase(); + StoreRegistry registry = new StoreRegistry(); + factory.setRegistry(registry); + + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + // No description registered — should log warning but not throw + factory.store(pw, 0, new SimpleBean()); + pw.flush(); + + // No output should be generated + Assert.assertEquals("", sw.toString()); + } + + + @Test + public void testStoreChildrenDefault() throws Exception { + StoreFactoryBase factory = new StoreFactoryBase(); + StoreRegistry registry = new StoreRegistry(); + factory.setRegistry(registry); + + StoreDescription desc = new StoreDescription(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + + // Default storeChildren does nothing — verify it doesn't throw + factory.storeChildren(pw, 0, new SimpleBean(), desc); + pw.flush(); + + Assert.assertEquals("", sw.toString()); + } + + + /** + * Simple JavaBean for testing. + */ + public static class SimpleBean { + private int port = 8080; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + } +} diff --git a/test/org/apache/catalina/storeconfig/TestStoreFileMover.java b/test/org/apache/catalina/storeconfig/TestStoreFileMover.java new file mode 100644 index 000000000000..949c233b38b1 --- /dev/null +++ b/test/org/apache/catalina/storeconfig/TestStoreFileMover.java @@ -0,0 +1,199 @@ +/* + * 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.catalina.storeconfig; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.startup.TomcatBaseTest; + +/** + * Tests for {@link StoreFileMover}. + */ +public class TestStoreFileMover extends TomcatBaseTest { + + @Test + public void testConstructorWithParameters() { + File tmpDir = getTemporaryDirectory(); + String basename = tmpDir.getAbsolutePath(); + + StoreFileMover mover = new StoreFileMover(basename, "conf/server.xml", "UTF-8"); + + Assert.assertEquals(basename, mover.getBasename()); + Assert.assertEquals("conf/server.xml", mover.getFilename()); + Assert.assertEquals("UTF-8", mover.getEncoding()); + + Assert.assertNotNull(mover.getConfigOld()); + Assert.assertNotNull(mover.getConfigNew()); + Assert.assertNotNull(mover.getConfigSave()); + } + + + @Test + public void testGetSetProperties() { + File tmpDir = getTemporaryDirectory(); + String basename = tmpDir.getAbsolutePath(); + + StoreFileMover mover = new StoreFileMover(basename, "conf/server.xml", "UTF-8"); + + mover.setEncoding("ISO-8859-1"); + Assert.assertEquals("ISO-8859-1", mover.getEncoding()); + + mover.setFilename("conf/context.xml"); + Assert.assertEquals("conf/context.xml", mover.getFilename()); + + mover.setBasename("/new/base"); + Assert.assertEquals("/new/base", mover.getBasename()); + } + + + @Test + public void testConfigFileObjects() { + File tmpDir = getTemporaryDirectory(); + String basename = tmpDir.getAbsolutePath(); + + StoreFileMover mover = new StoreFileMover(basename, "conf/server.xml", "UTF-8"); + + File configOld = mover.getConfigOld(); + File configNew = mover.getConfigNew(); + File configSave = mover.getConfigSave(); + + Assert.assertTrue("configOld should be under basename", + configOld.getAbsolutePath().contains("conf" + File.separator + "server.xml")); + Assert.assertTrue("configNew should have .new suffix", + configNew.getName().endsWith(".new")); + Assert.assertNotNull("configSave should not be null", configSave); + } + + + @Test + public void testGetWriter() throws Exception { + File tmpDir = getTemporaryDirectory(); + String basename = tmpDir.getAbsolutePath(); + + StoreFileMover mover = new StoreFileMover(basename, "conf/server.xml", "UTF-8"); + + // Ensure the .new file's parent directory exists for the writer + File configNew = mover.getConfigNew(); + configNew.getParentFile().mkdirs(); + + try (PrintWriter writer = mover.getWriter()) { + Assert.assertNotNull(writer); + writer.print("test content"); + } + + // Cleanup + configNew.delete(); + } + + + @Test + public void testMoveNewToOld() throws Exception { + File tmpDir = getTemporaryDirectory(); + String basename = tmpDir.getAbsolutePath(); + + // Create a fresh config file pair + String configPath = "test-server.xml"; + StoreFileMover mover = new StoreFileMover(basename, configPath, "UTF-8"); + + File configNew = mover.getConfigNew(); + configNew.getParentFile().mkdirs(); + + // Write content to new file + try (PrintWriter pw = mover.getWriter()) { + pw.print(""); + } + + Assert.assertTrue("configNew should exist before move", configNew.exists()); + + // Move: since configOld doesn't exist, new->old + mover.move(); + + File configOld = mover.getConfigOld(); + Assert.assertTrue("configOld should exist after move", configOld.exists()); + Assert.assertFalse("configNew should not exist after move", configNew.exists()); + + // Cleanup + configOld.delete(); + } + + + @Test + public void testMoveWithExistingOld() throws Exception { + File tmpDir = getTemporaryDirectory(); + String basename = tmpDir.getAbsolutePath(); + + // Create config file pair + String configPath = "test-server2.xml"; + StoreFileMover mover = new StoreFileMover(basename, configPath, "UTF-8"); + + File configOld = mover.getConfigOld(); + File configNew = mover.getConfigNew(); + + configOld.getParentFile().mkdirs(); + + // Create the old file + try (PrintWriter pw = new PrintWriter(configOld)) { + pw.print("old"); + } + + // Create the new file + try (PrintWriter pw = mover.getWriter()) { + pw.print("new"); + } + + Assert.assertTrue(configOld.exists()); + Assert.assertTrue(configNew.exists()); + + mover.move(); + + Assert.assertTrue("configOld should still exist (renamed from new)", + configOld.exists()); + Assert.assertFalse("configNew should not exist after move", + configNew.exists()); + + File configSave = mover.getConfigSave(); + Assert.assertTrue("configSave should exist (backup of old)", + configSave.exists()); + + // Cleanup + configOld.delete(); + configSave.delete(); + } + + + @Test + public void testMoveFailsWhenNewCannotRename() throws Exception { + File tmpDir = getTemporaryDirectory(); + String basename = tmpDir.getAbsolutePath(); + + StoreFileMover mover = new StoreFileMover(basename, "nonexistent-move.xml", "UTF-8"); + + // Neither old nor new exists — should throw IOException + try { + mover.move(); + Assert.fail("Expected IOException"); + } catch (IOException e) { + // Expected + Assert.assertNotNull(e.getMessage()); + } + } +} diff --git a/test/org/apache/catalina/storeconfig/TestStoreRegistry.java b/test/org/apache/catalina/storeconfig/TestStoreRegistry.java new file mode 100644 index 000000000000..c7ba4fbd0242 --- /dev/null +++ b/test/org/apache/catalina/storeconfig/TestStoreRegistry.java @@ -0,0 +1,155 @@ +/* + * 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.catalina.storeconfig; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link StoreRegistry}. + */ +public class TestStoreRegistry { + + @Test + public void testDefaultValues() { + StoreRegistry registry = new StoreRegistry(); + + Assert.assertEquals("UTF-8", registry.getEncoding()); + Assert.assertNull(registry.getName()); + Assert.assertNull(registry.getVersion()); + } + + + @Test + public void testGetSetProperties() { + StoreRegistry registry = new StoreRegistry(); + + registry.setName("Tomcat"); + Assert.assertEquals("Tomcat", registry.getName()); + + registry.setVersion("12.0"); + Assert.assertEquals("12.0", registry.getVersion()); + + registry.setEncoding("ISO-8859-1"); + Assert.assertEquals("ISO-8859-1", registry.getEncoding()); + } + + + @Test + public void testRegisterAndFindDescription() { + StoreRegistry registry = new StoreRegistry(); + + StoreDescription desc = new StoreDescription(); + desc.setTag("Server"); + desc.setTagClass("org.apache.catalina.core.StandardServer"); + desc.setStoreFactoryClass("org.apache.catalina.storeconfig.StandardServerSF"); + + registry.registerDescription(desc); + + StoreDescription found = registry.findDescription( + "org.apache.catalina.core.StandardServer"); + Assert.assertNotNull(found); + Assert.assertEquals("Server", found.getTag()); + } + + + @Test + public void testFindDescriptionByClass() { + StoreRegistry registry = new StoreRegistry(); + + StoreDescription desc = new StoreDescription(); + desc.setTag("TestTag"); + desc.setTagClass(StoreRegistry.class.getName()); + + registry.registerDescription(desc); + + StoreDescription found = registry.findDescription(StoreRegistry.class); + Assert.assertNotNull(found); + Assert.assertEquals("TestTag", found.getTag()); + } + + + @Test + public void testFindDescriptionNotFound() { + StoreRegistry registry = new StoreRegistry(); + + Assert.assertNull(registry.findDescription("com.nonexistent.Class")); + } + + + @Test + public void testUnregisterDescription() { + StoreRegistry registry = new StoreRegistry(); + + StoreDescription desc = new StoreDescription(); + desc.setTag("Server"); + desc.setTagClass("org.apache.catalina.core.StandardServer"); + + registry.registerDescription(desc); + + StoreDescription removed = registry.unregisterDescription(desc); + Assert.assertNotNull(removed); + Assert.assertEquals("Server", removed.getTag()); + + // Should be gone now + Assert.assertNull(registry.findDescription( + "org.apache.catalina.core.StandardServer")); + } + + + @Test + public void testRegisterDescriptionWithId() { + StoreRegistry registry = new StoreRegistry(); + + StoreDescription desc = new StoreDescription(); + desc.setId("customId"); + desc.setTag("Custom"); + desc.setTagClass("org.example.Custom"); + + registry.registerDescription(desc); + + StoreDescription found = registry.findDescription("customId"); + Assert.assertNotNull(found); + Assert.assertEquals("Custom", found.getTag()); + } + + + @Test + public void testFindStoreFactoryNotRegistered() { + StoreRegistry registry = new StoreRegistry(); + + Assert.assertNull(registry.findStoreFactory("com.nonexistent.Class")); + Assert.assertNull(registry.findStoreFactory(String.class)); + } + + + @Test + public void testFindStoreFactoryRegistered() { + StoreRegistry registry = new StoreRegistry(); + + StoreDescription desc = new StoreDescription(); + desc.setTagClass(StoreRegistry.class.getName()); + StoreFactoryBase factory = new StoreFactoryBase(); + desc.setStoreFactory(factory); + + registry.registerDescription(desc); + + IStoreFactory found = registry.findStoreFactory(StoreRegistry.class); + Assert.assertNotNull(found); + Assert.assertSame(factory, found); + } +}