Author: simoneg
Date: Mon Nov 23 13:03:43 2009
New Revision: 883330

URL: http://svn.apache.org/viewvc?rev=883330&view=rev
Log:
LABS-365 : support for "not" and "last"

Added:
    
labs/magma/trunk/foundation-basics/src/test/resources/META-INF/specialchars.properties
Modified:
    
labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java
    
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java
    
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/PerformanceTest.java

Modified: 
labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java?rev=883330&r1=883329&r2=883330&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java
 (original)
+++ 
labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java
 Mon Nov 23 13:03:43 2009
@@ -63,7 +63,9 @@
         */
        protected void buildMatrix() {
                for (Map.Entry<Object, Object> entry : properties.entrySet()) {
-                       String[] parts = tokenize((String)entry.getKey(), 
(String)entry.getValue());
+                       String key = (String)entry.getKey();
+                       if (key.startsWith(".")) key = key.substring(1);
+                       String[] parts = tokenize(key, 
(String)entry.getValue());
                        String discriminator = parts[parts.length - 2];
                        List<String[]> list = firstLookup.get(discriminator);
                        if (list == null) {
@@ -186,15 +188,38 @@
                int total = -1;
                int upto = 0;
                int pos = 1;
+               boolean done = false;
+               List<String> notted = null;
+               List<String> lasted = null;
+               String acpart = parts[upto];
                for (String seg : mc) {
                        pos++;
-                       if (parts[upto].equals(seg)) {
-                               upto++;
-                               total += pos;
-                               if (upto == parts.length - 2) return total;
+                       if (acpart.charAt(0) == '!') {
+                               if (notted == null) notted = new 
ArrayList<String>();
+                               notted.add(acpart.substring(1));
+                               acpart = parts[++upto];
+                               done = (upto == parts.length - 2);
+                       } else {
+                               if (notted != null && notted.contains(seg)) 
return -1;
+                               if (lasted != null && lasted.contains(seg)) 
return -1;
+                               if (acpart.charAt(0) == '#') acpart = 
acpart.substring(1); 
+                               if (acpart.equals(seg)) {
+                                       notted = null;
+                                       if (parts[upto].charAt(0) == '#') {
+                                               if (lasted == null) lasted = 
new ArrayList<String>();
+                                               lasted.add(acpart);
+                                       }
+                                       if (upto == parts.length - 3 && lasted 
!= null && lasted.size() > 0) {
+                                               done = true;
+                                       } else {
+                                               acpart = parts[++upto];
+                                               total += pos;
+                                               if (upto == parts.length - 2) 
return total;
+                                       }
+                               }
                        }
                }
-               return -1;              
+               return done ? total : -1;               
        }
 
        /**

Modified: 
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java?rev=883330&r1=883329&r2=883330&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java
 (original)
+++ 
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java
 Mon Nov 23 13:03:43 2009
@@ -1,6 +1,7 @@
 package org.apache.magma.settings;
 
 import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.*;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.apache.magma.settings.ContextSettingsHolder.stringify;
@@ -106,4 +107,72 @@
                assertTrue(score1 + " is not > than " + score2, score1 > 
score2);               
        }
        
+       @Test
+       public void testNot() throws Exception {
+               SubRunningContext mc = RunningContext.get();
+               mc.push("BahHtmlProducer");
+               mc.push("BeanFormProducer");
+               mc.push("UserBean");
+               mc.push("BeanFormProducer");
+               mc.push("UserBean");
+               mc.push("field1");
+               mc.push("field1");
+               mc.push("field2");
+               mc.push("field2");
+               mc.push("field2");
+               
+               int score = 0;
+               score = getScore(tokenize("UserBean.field2.key", "key"), 
stringify(mc));
+               assertTrue("It should match this without any not", score > 0);
+               score = getScore(tokenize("UserBean.!field2.key", "key"), 
stringify(mc));
+               assertTrue("It should NOT match this with !field2", score < 0);
+               score = getScore(tokenize("UserBean.!unexistant.key", "key"), 
stringify(mc));
+               assertTrue("It should match with !unexistant", score > 0);
+               score = 
getScore(tokenize("UserBean.!unexistant.field1.!field2.key", "key"), 
stringify(mc));
+               assertTrue("It should NOT match this with !unexistant but 
!field2", score < 0);
+               score = getScore(tokenize("UserBean.!field1.UserBean.key", 
"key"), stringify(mc));
+               assertTrue("It should match with !field1 placed where there is 
none", score > 0);
+               score = getScore(tokenize("!UserBean.!field1.!field2.key", 
"key"), stringify(mc));
+               assertTrue("It should NOT match with all nots", score < 0);
+       }
+
+       @Test
+       public void testEnd() throws Exception {
+               SubRunningContext mc = RunningContext.get();
+               mc.push("BahHtmlProducer");
+               mc.push("BeanFormProducer");
+               mc.push("UserBean");
+               mc.push("BeanFormProducer");
+               mc.push("UserBean");
+               mc.push("field1");
+               mc.push("field1");
+               mc.push("field2");
+               mc.push("field2");
+               mc.push("field2");
+               
+               int score = 0;
+               score = getScore(tokenize("UserBean.field2.key", "key"), 
stringify(mc));
+               assertTrue("It should match this without any ended", score > 0);
+               score = getScore(tokenize("#UserBean.field2.key", "key"), 
stringify(mc));
+               assertTrue("It should NOT match this with #UserBean", score < 
0);
+               score = getScore(tokenize("UserBean.#unexistant.key", "key"), 
stringify(mc));
+               assertTrue("It should NOT match with #unexistant", score < 0);
+               score = getScore(tokenize("UserBean.field1.#field2.key", 
"key"), stringify(mc));
+               assertTrue("It should NOT match with #field2", score < 0);
+               score = 
getScore(tokenize("#BahHtmlProducer.UserBean.UserBean.key", "key"), 
stringify(mc));
+               assertTrue("It should match with #BahHtmlProducer cause it is 
unique", score > 0);
+               score = getScore(tokenize("#UserBean.#field1.#field2.key", 
"key"), stringify(mc));
+               assertTrue("It should NOT match with all nots", score < 0);
+       }
+       
+       @Test
+       public void charsPermitted() throws Exception {
+               SettingsHolder cth = new SettingsHolder();
+               
cth.overrideWith(getClass().getResource("/META-INF/specialchars.properties"));
+               cth.inited();
+               
+               assertTrue("Does not support #", cth.get("test.#pound.key") != 
null);
+               assertTrue("Does not support !", 
cth.get("test.!exclamation.key") != null);
+       }
+       
 }

Modified: 
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/PerformanceTest.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/PerformanceTest.java?rev=883330&r1=883329&r2=883330&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/PerformanceTest.java
 (original)
+++ 
labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/PerformanceTest.java
 Mon Nov 23 13:03:43 2009
@@ -29,8 +29,10 @@
                        String ele = "";
                        for (int k = 0; k < elements.length; k++) {
                                boolean add = (i & (1 << (k + 2))) != 0;
-                               if (add) 
+                               if (add) {
+                                       if ((i % elements.length) == k) 
ele+='!';
                                        ele += elements[k] + ".";
+                               }
                        }
                        if ((i & 1) != 0) {
                                ele += "notfound.";
@@ -40,6 +42,7 @@
                        } else {
                                ele += "incorrect";
                        }
+                       //System.out.println(i + " : " + ele);
                        lh.override(ele, "ele " + i);
                }
                lh.inited();
@@ -78,8 +81,8 @@
                System.out.println(nt.totals());
                System.out.println("Positives : " + positives + "   Negatives : 
" + negatives);
                System.out.println("Cache size : " + lh.cache.size());
-               assertEquals(600001, positives);
-               assertEquals(399999, negatives);
+               assertEquals(584616, positives);
+               assertEquals(415384, negatives);
                assertTrue("Poor performances", nt.totalIterationsPerSecond() > 
150000d);
        }
        

Added: 
labs/magma/trunk/foundation-basics/src/test/resources/META-INF/specialchars.properties
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/test/resources/META-INF/specialchars.properties?rev=883330&view=auto
==============================================================================
--- 
labs/magma/trunk/foundation-basics/src/test/resources/META-INF/specialchars.properties
 (added)
+++ 
labs/magma/trunk/foundation-basics/src/test/resources/META-INF/specialchars.properties
 Mon Nov 23 13:03:43 2009
@@ -0,0 +1,2 @@
+test.#pound.key=val
+test.!exclamation.key=val
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to