Skip to content

Commit 54b0a04

Browse files
Test Capabilities for mini filter
1 parent e560b39 commit 54b0a04

File tree

8 files changed

+125
-19
lines changed

8 files changed

+125
-19
lines changed

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/GenericListCatalogConfig.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ public GenericListCatalogConfig( String attTagDataList, String attTagData ) {
107107
this.generalProps = new Properties();
108108
this.orderedId = new ConcurrentSkipListSet<String>();
109109
}
110+
111+
/**
112+
* General configurazion property for checking duplicate id
113+
*/
114+
public static final String CONFIG_CHECK_DUPLICATE_ID = "check-duplicate-id";
115+
116+
/**
117+
* If 'check-duplicate-id' is se to fail, the duplicate will cause the configuration to fail
118+
*/
119+
public static final String CONFIG_CHECK_DUPLICATE_ID_FAIL = "fail";
120+
121+
/**
122+
* If 'check-duplicate-id' is se to warn, the duplicate will only be logged
123+
*/
124+
public static final String CONFIG_CHECK_DUPLICATE_ID_WARN = "warn";
125+
126+
/**
127+
* Default vaule for 'check-duplicate-id' property.
128+
* ('warn' for compatibility reason, strongly recommended setting to 'fail' )
129+
*/
130+
public static final String CONFIG_CHECK_DUPLICATE_ID_DEFAULT = CONFIG_CHECK_DUPLICATE_ID_WARN;
110131

111132
/**
112133
* Default configuration element for a a data catalog config element
@@ -130,7 +151,7 @@ public GenericListCatalogConfig( String attTagDataList, String attTagData ) {
130151
public static final String ATT_TAG_MODULE_LIST = "module-list";
131152

132153
public static final String ATT_TAG_MODULE = "module";
133-
154+
134155
/**
135156
* Configuration attribute for module 'id'
136157
*/
@@ -189,6 +210,8 @@ public void configure(Element tag) throws ConfigException {
189210
}
190211
logger.info( "general props : "+this.getGeneralProps() );
191212

213+
String checkDuplicateId = this.getGeneralProps().getProperty( CONFIG_CHECK_DUPLICATE_ID , CONFIG_CHECK_DUPLICATE_ID_DEFAULT );
214+
192215
String type = this.getGeneralProps().getProperty( ATT_TYPE );
193216
if ( StringUtils.isEmpty( type ) ) {
194217
throw new ConfigException( "No type defined" );
@@ -216,6 +239,14 @@ public void configure(Element tag) throws ConfigException {
216239
Collection<T> listCurrent = this.newCollection( typeSample );
217240
NodeList schemaIt = currentListTag.getElementsByTagName( dataElementName );
218241
String idList = currentListTag.getAttribute( "id" );
242+
if ( this.getIdSet().contains( idList ) ) {
243+
String message = "Duplicate id found : "+idList;
244+
if ( CONFIG_CHECK_DUPLICATE_ID_FAIL.equalsIgnoreCase( checkDuplicateId ) ) {
245+
throw new ConfigException( message );
246+
} else {
247+
logger.warn( "["+this.getClass().getSimpleName()+"]"+message );
248+
}
249+
}
219250
String extendsAtt = currentListTag.getAttribute( "extends" );
220251
if ( StringUtils.isNotEmpty( extendsAtt ) ) {
221252
String[] extendsAttList = extendsAtt.split( "," );

fj-core/src/main/java/org/fugerit/java/core/util/filterchain/MiniFilterConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ public MiniFilterChain getChainCache( String id ) throws Exception {
6161
return chain;
6262
}
6363

64+
public static MiniFilterConfig initFromClassLoaderWithRuntimeException( String path ) {
65+
MiniFilterConfig config = new MiniFilterConfig();
66+
try {
67+
MiniFilterConfig.loadConfig( ClassHelper.loadFromDefaultClassLoader( path ) , config );
68+
} catch (Exception e) {
69+
throw new RuntimeException( e );
70+
}
71+
return config;
72+
}
73+
6474
}

fj-core/src/main/java/org/fugerit/java/core/util/filterchain/MiniFilterConfigEntry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,10 @@ public String getParam01() {
8080
public void setParam01(String param01) {
8181
this.param01 = param01;
8282
}
83+
84+
public String toString() {
85+
return this.getClass().getSimpleName()+"[id:"+this.getId()+"]";
86+
}
87+
8388

8489
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.fugerit.java.core.util.filterchain;
2+
3+
import java.io.InputStream;
4+
import java.io.PrintWriter;
5+
import java.util.Iterator;
6+
7+
public class MiniFilterDebug {
8+
9+
public static void dumpConfig( PrintWriter writer, InputStream is ) throws Exception {
10+
MiniFilterConfig config = new MiniFilterConfig();
11+
MiniFilterConfig.loadConfig( is , config );
12+
dumpConfig(writer, config);
13+
}
14+
15+
public static void dumpConfig( PrintWriter writer, MiniFilterConfig config ) throws Exception {
16+
try {
17+
writer.println( "General config properties : "+config.getGeneralProps().toString() );
18+
Iterator<String> itConfig = config.getIdSet().iterator();
19+
while ( itConfig.hasNext() ) {
20+
String currentId = itConfig.next();
21+
writer.println( "***********************************************" );
22+
writer.println( "AnprOperationBase.getChain() id='"+currentId+"'" );
23+
writer.println( "***********************************************" );
24+
Iterator<MiniFilterConfigEntry> itEntry = config.getDataList( currentId ).iterator();
25+
while ( itEntry.hasNext() ) {
26+
MiniFilterConfigEntry entry = itEntry.next();
27+
writer.println( "entry chain : "+entry.getId()+" - "+entry.getType() );
28+
}
29+
}
30+
} catch (Exception e) {
31+
writer.println( "Error dumping config "+e );
32+
e.printStackTrace( writer );
33+
throw e;
34+
}
35+
}
36+
37+
}

fj-core/src/test/java/test/org/fugerit/java/core/fixed/parser/ValidatorTester.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.File;
44
import java.io.FileInputStream;
5-
import java.io.FileReader;
65
import java.util.Iterator;
76
import java.util.Properties;
87

fj-core/src/test/java/test/org/fugerit/java/core/util/filterchain/TestMiniFilter.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.util.Iterator;
66

7-
import org.fugerit.java.core.lang.helpers.ClassHelper;
87
import org.fugerit.java.core.util.filterchain.MiniFilterChain;
98
import org.fugerit.java.core.util.filterchain.MiniFilterConfig;
109
import org.fugerit.java.core.util.filterchain.MiniFilterContext;
@@ -19,17 +18,9 @@ public class TestMiniFilter {
1918

2019
private static final String CONF_PATH = "core/util/filterchain/minifilter-test-config.xml";
2120

22-
public static MiniFilterConfig init() {
23-
MiniFilterConfig config = new MiniFilterConfig();
24-
try {
25-
MiniFilterConfig.loadConfig( ClassHelper.loadFromDefaultClassLoader( CONF_PATH ) , config );
26-
} catch (Exception e) {
27-
throw new RuntimeException( e );
28-
}
29-
return config;
30-
}
21+
private static final String CONF_PATH_DUPLICATE = "core/util/filterchain/minifilter-test-duplicate-fail.xml";
3122

32-
private static MiniFilterConfig CONFIG = init();
23+
private static MiniFilterConfig CONFIG = MiniFilterConfig.initFromClassLoaderWithRuntimeException( CONF_PATH );
3324

3425
public void testWorker( String chainId ) {
3526
this.testWorker(chainId, false);
@@ -110,4 +101,26 @@ public void testPrintConfig() {
110101
logger.info( "**********************************************" );
111102
}
112103

104+
@Test
105+
public void testDuplicate() {
106+
logger.info( "**********************************************" );
107+
logger.info( "**********************************************" );
108+
logger.info( "* TEST DUPLICATE *" );
109+
boolean ok = false;
110+
try {
111+
MiniFilterConfig config = MiniFilterConfig.initFromClassLoaderWithRuntimeException( CONF_PATH_DUPLICATE );
112+
logger.info( config.toString() );
113+
} catch (Exception e) {
114+
logger.info( e.getMessage() );
115+
if ( e.getMessage().toLowerCase().contains( "duplicate" ) ) {
116+
ok = true;
117+
}
118+
}
119+
if ( !ok ) {
120+
fail( "Test failed, duplicate exception non triggered" );
121+
}
122+
logger.info( "**********************************************" );
123+
logger.info( "**********************************************" );
124+
}
125+
113126
}

fj-core/src/test/resources/core/util/filterchain/minifilter-test-config.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@
4141
type="test.org.fugerit.java.core.util.filterchain.ClassNotFoundStep"/>
4242
</data-list>
4343

44-
<data-list id="chain-base-notfound">
45-
<data id="step-07" defaultBehaviour="CONTINUE"
46-
description="Step 07 Will not be printed as the class does not exist"
47-
type="test.org.fugerit.java.core.util.filterchain.ClassNotFoundStep"/>
48-
</data-list>
49-
5044
<data-list id="chain-base-loadsafe-00">
5145
<data id="step-08" defaultBehaviour="CONTINUE"
5246
description="Step 08 will be printed"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<data-catalog-config
2+
check-duplicate-id="fail"
3+
type="org.fugerit.java.core.util.filterchain.MiniFilterConfigEntry">
4+
5+
<data-list id="chain-base-duplicate" >
6+
<data id="step-01" defaultBehaviour="CONTINUE"
7+
description="Step 01 Continue (this one will be printed)"
8+
type="test.org.fugerit.java.core.util.filterchain.ContinueStep"/>
9+
</data-list>
10+
11+
<data-list id="chain-base-duplicate">
12+
<data id="step-02" defaultBehaviour="CONTINUE"
13+
description="Step 02 Skip (this one will be printed)"
14+
type="test.org.fugerit.java.core.util.filterchain.SkipStep"/>
15+
</data-list>
16+
17+
</data-catalog-config>

0 commit comments

Comments
 (0)