You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by rm...@apache.org on 2011/01/15 18:46:50 UTC

svn commit: r1059380 - in /incubator/isis/trunk: applib/src/main/java/org/apache/isis/applib/adapters/ core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/parseable/ core/progmodel/src/test/java/org/apache/isis/core/progmodel/face...

Author: rmatthews
Date: Sat Jan 15 17:46:50 2011
New Revision: 1059380

URL: http://svn.apache.org/viewvc?rev=1059380&view=rev
Log:
When parsing value entries typical parsing exceptions are wrapped as TextEntryParseExecption (which is dealt with by all UIs).  This allows parsers in domain models to be included without having to link into the metamodel. 

Added:
    incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/adapters/ParsingException.java   (with props)
    incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParserTest.java   (with props)
    incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ValueProxy.java   (with props)
Modified:
    incubator/isis/trunk/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParser.java
    incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateValueSemanticsProviderTest.java

Added: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/adapters/ParsingException.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/adapters/ParsingException.java?rev=1059380&view=auto
==============================================================================
--- incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/adapters/ParsingException.java (added)
+++ incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/adapters/ParsingException.java Sat Jan 15 17:46:50 2011
@@ -0,0 +1,44 @@
+/*
+ *  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.isis.applib.adapters;
+
+/**
+ * Indicates that parsing has failed, ie the entry is illegal (rather than invalid).
+ */
+public class ParsingException extends RuntimeException {
+    private static final long serialVersionUID = 1L;
+
+    public ParsingException() {
+        super();
+    }
+
+    public ParsingException(final String msg) {
+        super(msg);
+    }
+
+    public ParsingException(final String msg, final Throwable cause) {
+        super(msg, cause);
+    }
+
+    public ParsingException(final Throwable cause) {
+        super(cause);
+    }
+
+}

Propchange: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/adapters/ParsingException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/isis/trunk/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParser.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParser.java?rev=1059380&r1=1059379&r2=1059380&view=diff
==============================================================================
--- incubator/isis/trunk/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParser.java (original)
+++ incubator/isis/trunk/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParser.java Sat Jan 15 17:46:50 2011
@@ -20,7 +20,10 @@
 
 package org.apache.isis.core.progmodel.facets.object.parseable;
 
+import java.util.IllegalFormatException;
+
 import org.apache.isis.applib.adapters.Parser;
+import org.apache.isis.applib.adapters.ParsingException;
 import org.apache.isis.core.commons.authentication.AuthenticationSessionProvider;
 import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
 import org.apache.isis.core.metamodel.adapter.map.AdapterMap;
@@ -30,6 +33,7 @@ import org.apache.isis.core.metamodel.co
 import org.apache.isis.core.metamodel.facetapi.FacetAbstract;
 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
 import org.apache.isis.core.metamodel.facets.object.parseable.ParseableFacet;
+import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException;
 import org.apache.isis.core.metamodel.facets.object.value.ValueFacet;
 import org.apache.isis.core.metamodel.interactions.InteractionUtils;
 import org.apache.isis.core.metamodel.interactions.ObjectValidityContext;
@@ -88,20 +92,28 @@ public class ParseableFacetUsingParser e
 
         getDependencyInjector().injectDependenciesInto(parser);
 
-		final Object parsed = parser.parseTextEntry(context, entry);
-        if (parsed == null) {
-			return null;
-		}
-        
-        // check resultant object is also valid
-        // (eg pick up any validate() methods on it)
-		ObjectAdapter adapter = getAdapterMap().adapterFor(parsed);
-		ObjectSpecification specification = adapter.getSpecification();
-		ObjectValidityContext validateContext = 
-		    specification.createValidityInteractionContext(getAuthenticationSessionProvider().getAuthenticationSession(), InteractionInvocationMethod.BY_USER, adapter);
-		validate(validateContext);
-		
-		return adapter;
+        try {
+    		final Object parsed = parser.parseTextEntry(context, entry);
+            if (parsed == null) {
+    			return null;
+    		}
+            
+            // check resultant object is also valid
+            // (eg pick up any validate() methods on it)
+            ObjectAdapter adapter = getAdapterMap().adapterFor(parsed);
+            ObjectSpecification specification = adapter.getSpecification();
+            ObjectValidityContext validateContext = 
+                specification.createValidityInteractionContext(getAuthenticationSessionProvider().getAuthenticationSession(), InteractionInvocationMethod.BY_USER, adapter);
+            validate(validateContext);
+            
+            return adapter;
+        } catch (NumberFormatException e) {
+            throw new TextEntryParseException(e.getMessage(), e);
+        } catch (IllegalFormatException e) {
+            throw new TextEntryParseException(e.getMessage(), e);
+        } catch (ParsingException e) {
+            throw new TextEntryParseException(e.getMessage(), e);
+        }
 	}
 
 	private void validate(ValidityContext<?> validityContext) {

Added: incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParserTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParserTest.java?rev=1059380&view=auto
==============================================================================
--- incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParserTest.java (added)
+++ incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParserTest.java Sat Jan 15 17:46:50 2011
@@ -0,0 +1,152 @@
+/*
+ *  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.isis.core.progmodel.facets.object.parseable;
+
+import java.util.IllegalFormatWidthException;
+
+import org.apache.isis.applib.adapters.Parser;
+import org.apache.isis.applib.adapters.ParsingException;
+import org.apache.isis.core.commons.authentication.AuthenticationSessionProvider;
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.adapter.map.AdapterMap;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException;
+import org.apache.isis.core.metamodel.facets.object.value.ValueFacet;
+import org.apache.isis.core.metamodel.runtimecontext.DependencyInjector;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.jmock.lib.legacy.ClassImposteriser;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ParseableFacetUsingParserTest {
+    
+    protected Mockery mockery = new JUnit4Mockery() {{
+        setImposteriser(ClassImposteriser.INSTANCE);
+    }};
+    
+    private FacetHolder mockFacetHolder;
+    private AuthenticationSessionProvider mockAuthenticationSessionProvider;
+    private DependencyInjector mockDependencyInjector;
+    private AdapterMap mockAdapterManager;
+
+    private ParseableFacetUsingParser parseableFacetUsingParser;
+
+//    private ObjectAdapter mockAdapter;
+
+//    private ObjectSpecification mockSpecification;
+
+    @Before
+    public void setUp() throws Exception {
+        
+        mockFacetHolder = mockery.mock(FacetHolder.class);
+        mockDependencyInjector = mockery.mock(DependencyInjector.class);
+        mockAdapterManager = mockery.mock(AdapterMap.class);
+        mockAuthenticationSessionProvider = mockery.mock(AuthenticationSessionProvider.class);
+
+
+        mockery.checking(new Expectations(){{
+            never(mockAuthenticationSessionProvider);
+            never(mockAdapterManager);
+
+            allowing(mockFacetHolder).containsFacet(ValueFacet.class);
+            will(returnValue(Boolean.FALSE));
+
+           allowing(mockDependencyInjector).injectDependenciesInto(with(any(Object.class)));
+        }});
+
+        Parser parser = new Parser<String>() {
+            public String parseTextEntry(Object contextPojo, String entry) {
+                if (entry.equals("invalid")) {
+                    throw new ParsingException();
+                }
+                if (entry.equals("number")) {
+                    throw new NumberFormatException();
+                }
+                if (entry.equals("format")) {
+                    throw new IllegalFormatWidthException(2);
+                }
+                return entry.toUpperCase();
+            }
+
+            public int typicalLength() {
+                return 0;
+            }
+
+            public String displayTitleOf(String object) {
+                return null;
+            }
+
+            public String displayTitleOf(String object, String usingMask) {
+                return null;
+            }
+
+            public String parseableTitleOf(String existing) {
+                return null;
+            }
+        };            
+        parseableFacetUsingParser = new ParseableFacetUsingParser(parser, mockFacetHolder, mockAuthenticationSessionProvider, mockDependencyInjector, mockAdapterManager);
+        
+      //  mockAdapter = mockery.mock(ObjectAdapter.class);
+      //  mockSpecification = mockery.mock(ObjectSpecification.class);
+    }
+
+    @Test
+    public void testParseNormalEntry() throws Exception {
+        // TODO why is this so complicated to check!!!
+        /*
+        final AuthenticationSession session = mockery.mock(AuthenticationSession.class);
+        
+        mockery.checking(new Expectations(){{
+            one(mockAdapterManager).adapterFor("XXX");
+            will(returnValue(mockAdapter));
+            
+            one(mockAdapter).getSpecification();
+            will(returnValue(mockSpecification));
+            
+            one(mockAuthenticationSessionProvider).getAuthenticationSession();
+            will(returnValue(session));
+            
+            allowing(mockSpecification).createValidityInteractionContext(session, InteractionInvocationMethod.BY_USER, mockAdapter);
+        }});
+        ObjectAdapter adapter = parseableFacetUsingParser.parseTextEntry(null, "xxx");
+        
+        adapter.getObject();
+        */
+    }
+    
+
+    @Test(expected=TextEntryParseException.class)
+    public void parsingExceptionRethrown() throws Exception {
+        parseableFacetUsingParser.parseTextEntry(null, "invalid");
+    }
+
+    @Test(expected=TextEntryParseException.class)
+    public void numberFormatExceptionRethrown() throws Exception {
+        parseableFacetUsingParser.parseTextEntry(null, "number");
+    }
+
+    @Test(expected=TextEntryParseException.class)
+    public void illegalFormatExceptionRethrown() throws Exception {
+        parseableFacetUsingParser.parseTextEntry(null, "format");
+    }
+}
+

Propchange: incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ParseableFacetUsingParserTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ValueProxy.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ValueProxy.java?rev=1059380&view=auto
==============================================================================
--- incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ValueProxy.java (added)
+++ incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ValueProxy.java Sat Jan 15 17:46:50 2011
@@ -0,0 +1,24 @@
+/*
+ *  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.isis.core.progmodel.facets.object.parseable;
+
+public class ValueProxy {
+
+}
+

Propchange: incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/parseable/ValueProxy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateValueSemanticsProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateValueSemanticsProviderTest.java?rev=1059380&r1=1059379&r2=1059380&view=diff
==============================================================================
--- incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateValueSemanticsProviderTest.java (original)
+++ incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateValueSemanticsProviderTest.java Sat Jan 15 17:46:50 2011
@@ -33,6 +33,7 @@ import org.apache.isis.applib.adapters.E
 import org.apache.isis.applib.value.Date;
 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
 import org.apache.isis.core.metamodel.facetapi.FacetHolderImpl;
+import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException;
 
 @RunWith(JMock.class)
 public class DateValueSemanticsProviderTest extends ValueSemanticsProviderAbstractTestCase {
@@ -138,6 +139,11 @@ public class DateValueSemanticsProviderT
         assertEquals(new Date(2003, 4, 17), parsed);
     }
 
+    @Test(expected=TextEntryParseException.class)
+    public void illegalEntry() throws Exception {
+        adapter.parseTextEntry(null, "xxx");
+    }
+
     @Test
     public void testRestoreOfInvalidDatal() throws Exception {
         try {