package org.geoserver.catalog.impl; import org.geoserver.catalog.*; import org.geoserver.catalog.event.*; import org.geoserver.catalog.event.impl.CatalogRemoveEventImpl; import org.geoserver.ows.Dispatcher; import org.geoserver.ows.LocalLayer; import org.geoserver.ows.LocalWorkspace; import org.geoserver.security.AdminRequest; import org.geoserver.security.filter.GeoServerBasicAuthenticationFilter; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.security.core.context.SecurityContextHolder; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; /** * Created by poursb on 20/09/2016. */ public class ConcurrentCatalogModificationTest { protected Catalog catalog; protected WorkspaceInfo ws; protected WorkspaceInfo wsA; protected WorkspaceInfo wsB; protected NamespaceInfo ns; protected NamespaceInfo nsA; protected NamespaceInfo nsB; protected DataStoreInfo ds; protected DataStoreInfo dsA; protected DataStoreInfo dsB; protected CoverageStoreInfo cs; protected WMSStoreInfo wms; protected FeatureTypeInfo ft; protected CoverageInfo cv; protected WMSLayerInfo wl; protected LayerInfo l; protected StyleInfo s; protected LayerGroupInfo lg; @Before public void setUp() throws Exception { catalog = createCatalog(); CatalogFactory factory = catalog.getFactory(); ns = factory.createNamespace(); //ns prefix has to match workspace name, until we break that relationship //ns.setPrefix( "nsPrefix" ); ns.setPrefix( "wsName" ); ns.setURI( "nsURI" ); nsA = factory.createNamespace(); //ns prefix has to match workspace name, until we break that relationship //nsA.setPrefix( "nsPrefix" ); nsA.setPrefix( "aaa" ); nsA.setURI( "nsURIaaa" ); nsB = factory.createNamespace(); //ns prefix has to match workspace name, until we break that relationship //nsB.setPrefix( "nsPrefix" ); nsB.setPrefix( "bbb" ); nsB.setURI( "nsURIbbb" ); ws = factory.createWorkspace(); ws.setName( "wsName"); wsA = factory.createWorkspace(); wsA.setName( "aaa"); wsB = factory.createWorkspace(); wsB.setName( "bbb"); ds = factory.createDataStore(); ds.setEnabled(true); ds.setName( "dsName"); ds.setDescription("dsDescription"); ds.setWorkspace( ws ); dsA = factory.createDataStore(); dsA.setEnabled(true); dsA.setName( "dsNameA"); dsA.setDescription("dsDescription"); dsA.setWorkspace( wsA ); dsB = factory.createDataStore(); dsB.setEnabled(true); dsB.setName( "dsNameB"); dsB.setDescription("dsDescription"); dsB.setWorkspace( wsB ); ft = factory.createFeatureType(); ft.setEnabled(true); ft.setName( "ftName" ); ft.setAbstract( "ftAbstract" ); ft.setDescription( "ftDescription" ); ft.setStore( ds ); ft.setNamespace( ns ); cs = factory.createCoverageStore(); cs.setName("csName"); cs.setType("fakeCoverageType"); cs.setURL("file://fake"); cv = factory.createCoverage(); cv.setName("cvName"); cv.setStore(cs); wms = factory.createWebMapServer(); wms.setName("wmsName"); wms.setType("WMS"); wms.setCapabilitiesURL("http://fake.url"); wms.setWorkspace(ws); wl = factory.createWMSLayer(); wl.setEnabled(true); wl.setName("wmsLayer"); wl.setStore(wms); wl.setNamespace(ns); s = factory.createStyle(); s.setName( "styleName" ); s.setFilename( "styleFilename" ); l = factory.createLayer(); l.setResource( ft ); l.setEnabled(true); l.setDefaultStyle( s ); lg = factory.createLayerGroup(); lg.setName("layerGroup"); lg.getLayers().add(l); lg.getStyles().add(s); } protected Catalog createCatalog() { return new CatalogImpl(); } @Test public void testConcurrentCatalogModification() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(5); for (int i = 0; i < 20; i++) { executor.execute(new Worker()); } executor.shutdown(); while(!executor.isTerminated()){ } } private class Worker implements Runnable { @Override public void run() { System.out.println("/test add/remove listeners ...."); TestListener l1 = new TestListener(); catalog.addListener( l1 ); TestListener l2 = new TestListener(); catalog.addListener( l2 ); TestListener l3 = new TestListener(); catalog.addListener( l3 ); TestListener l4 = new TestListener(); catalog.addListener( l4 ); TestListener l5 = new TestListener(); catalog.addListener( l5 ); CatalogInfo catalogInfo = new CoverageInfoImpl(); catalog.fireRemoved(catalogInfo); CatalogInfo catalogInfo1 = new CoverageInfoImpl(); catalog.fireRemoved(catalogInfo1); CatalogInfo catalogInfo2 = new CoverageInfoImpl(); catalog.fireRemoved(catalogInfo2); CatalogInfo catalogInfo3 = new CoverageInfoImpl(); catalog.fireRemoved(catalogInfo3); catalog.removeListener(l1); catalog.removeListener(l2); catalog.removeListener(l3); catalog.removeListener(l4); catalog.removeListener(l5); } } static class TestListener implements CatalogListener { public List added = new ArrayList(); public List modified = new ArrayList(); public List removed = new ArrayList(); public void handleAddEvent(CatalogAddEvent event) { added.add( event ); } public void handleModifyEvent(CatalogModifyEvent event) { modified.add( event ); } public void handlePostModifyEvent(CatalogPostModifyEvent event) { } public void handleRemoveEvent(CatalogRemoveEvent event) { removed.add( event ); } public void reloaded() { } } }