openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r1038008 - /openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestInstanceGraphAndMetaModelFormat.java
Date Tue, 23 Nov 2010 07:33:11 GMT
Author: ppoddar
Date: Tue Nov 23 07:33:10 2010
New Revision: 1038008

URL: http://svn.apache.org/viewvc?rev=1038008&view=rev
Log:
OPENJPA-1851: Add a test

Added:
    openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestInstanceGraphAndMetaModelFormat.java
  (with props)

Added: openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestInstanceGraphAndMetaModelFormat.java
URL: http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestInstanceGraphAndMetaModelFormat.java?rev=1038008&view=auto
==============================================================================
--- openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestInstanceGraphAndMetaModelFormat.java
(added)
+++ openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestInstanceGraphAndMetaModelFormat.java
Tue Nov 23 07:33:10 2010
@@ -0,0 +1,117 @@
+/*
+ * 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.openjpa.persistence.jest;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.w3c.dom.Document;
+
+
+/**
+ * Tests combination of following dimensions
+ * 1. which Format to use    : XML, DOJO, HTML
+ * 2. which Object to encode : single/multiple instances or domain model
+ * 
+ * @author Pinaki Poddar
+ *
+ */
+public class TestInstanceGraphAndMetaModelFormat extends LocalJESTTest {
+    private static String JPQL = "select m from Movie m";;
+    private static String JPQL_SINGLE = "select m from Movie m where m.title = '" + DataLoader.MOVIE_DATA[1][1]
+ "'";
+    
+    /* Query for a single instance or collection? */
+    public static boolean SINGLE = true;
+    /* Format used to encode */
+    public static enum Format {XML, DOJO, HTML};
+    
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+    
+    public void testXMLEncoder() {
+        encodeAndValidateQueryResultInXMLForamt(Format.XML, SINGLE);
+        encodeAndValidateQueryResultInXMLForamt(Format.XML, !SINGLE);
+        encodeDomainModel(Format.XML);
+    }
+    
+    public void testDojoEncoderForColllection() {
+        encodeAndValidateQueryResultInXMLForamt(Format.DOJO, SINGLE);
+        encodeAndValidateQueryResultInXMLForamt(Format.DOJO, !SINGLE);
+        encodeDomainModel(Format.DOJO);
+    }
+        
+    public void testHTMLEncoderForColllection() {
+        encodeAndValidateQueryResultInXMLForamt(Format.HTML, SINGLE);
+        encodeAndValidateQueryResultInXMLForamt(Format.HTML, !SINGLE);
+        encodeDomainModel(Format.HTML);
+    }
+
+    void encodeAndValidateQueryResultInXMLForamt(Format format, boolean single) {
+        ObjectFormatter<?> formatter = getFormatter(format);
+        List<OpenJPAStateManager> result = getQueryResult(single ? JPQL_SINGLE : JPQL,
single);
+        Object doc = formatter.encode(result, _model);
+        try {
+            PrintWriter writer = getWriter(format, single ? "single" : "instances");
+            formatter.writeOut(result, _model, writer);
+            if (format == Format.XML) ((XMLFormatter)formatter).validate((Document)doc);
+        } catch (Exception e) {
+            e.printStackTrace();
+            Assert.fail(e.getMessage());
+        }
+    }
+    
+    void encodeDomainModel(Format format) {
+        ObjectFormatter<?> formatter = getFormatter(format);
+        PrintWriter writer = getWriter(format, "domain");
+        try {
+            formatter.writeOut(_model, writer);
+        } catch (IOException e) {
+            e.printStackTrace();
+            fail(e.getMessage());
+        }
+    }
+    
+    ObjectFormatter<?> getFormatter(Format format) {
+        ObjectFormatter<?> formatter = null;
+        switch (format) {
+            case XML : formatter  = new XMLFormatter();       break; 
+            case HTML : formatter = new HTMLTableFormatter(); break; 
+            case DOJO : formatter = new DojoFormatter();      break; 
+            default   : fail("Bad format " + format);
+        }
+        return formatter;
+    }
+    
+    PrintWriter getWriter(Format format, String name) {
+        String ext  = null;
+        switch (format) {
+            case XML  : ext = "xml";       break; 
+            case HTML : ext = "html";      break; 
+            case DOJO : ext = "dojo.html"; break; 
+            default   : fail("Bad format " + format);
+        }
+        return createTestOutput(name + "." + ext);
+    }
+}

Propchange: openjpa/sandboxes/jest/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jest/TestInstanceGraphAndMetaModelFormat.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message