//========================================================================
//Copyright 2007-2008 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed 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 com.dyuproject.util.xml;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import junit.framework.TestCase;

/**
 * @author David Yu
 * @created Sep 18, 2008
 */

public class XMLParserTest extends TestCase
{
    
    public void testParseOpenId() throws Exception
    {
        String url = "http://davidyu.myopenid.com";
        HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
        con.setRequestMethod("GET");
        con.setDefaultUseCaches(false);
        con.setInstanceFollowRedirects(false);
        con.setDoInput(true);
        con.connect();
        SimpleHandler handler = new SimpleHandler();
        InputStreamReader reader = new InputStreamReader(con.getInputStream());
        try
        {
            XMLParser.parse(reader, handler, true);
            Node html = handler.getNode();
            Node head = html.getNode("head");
            List<Node> links = head.getNodes("link");
            for(Node n : links)
            {
                String rel = n.getAttribute("rel");
                if(rel.equals("openid.server"))
                {
                    String openIdServer = n.getAttribute("href");                    
                    assertTrue(openIdServer!=null);
                    System.err.println(openIdServer);
                    break;
                }
            }
        }
        finally
        {
            reader.close();
            con.disconnect();
        }   
    }
    
    public void testNamespace() throws Exception
    {
        String url = "http://open.login.yahooapis.com/openid20/www.yahoo.com/xrds";
        HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
        con.setRequestMethod("GET");
        con.setDefaultUseCaches(false);
        con.setInstanceFollowRedirects(false);
        con.setDoInput(true);
        con.connect();
        SimpleHandler handler = new SimpleHandler();
        InputStreamReader reader = new InputStreamReader(con.getInputStream());
        try
        {
            XMLParser.parse(reader, handler, true);
            Node xrds = handler.getNode();
            assertEquals("xrds", xrds.getNamespace());
            Node xrd = xrds.getNode("xrd");
            Node service = xrd.getNode("service");
            assertTrue(0!=service.getNodes("type").size());
            assertEquals("xrds", service.getLastNode().getNamespace());
        }
        finally
        {
            reader.close();
            con.disconnect();
        }   
    }

}

@@ -954,32 +954,8 @@
 RL;%0D
-%0Aimport java.util.List;%0D
 %0A%0D%0Ai
@@ -1106,1295 +1106,8 @@
   %0D%0A
-    public void testParseOpenId() throws Exception%0D%0A    %7B%0D%0A        String url = %22http://davidyu.myopenid.com%22;%0D%0A        HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();%0D%0A        con.setRequestMethod(%22GET%22);%0D%0A        con.setDefaultUseCaches(false);%0D%0A        con.setInstanceFollowRedirects(false);%0D%0A        con.setDoInput(true);%0D%0A        con.connect();%0D%0A        SimpleHandler handler = new SimpleHandler();%0D%0A        InputStreamReader reader = new InputStreamReader(con.getInputStream());%0D%0A        try%0D%0A        %7B%0D%0A            XMLParser.parse(reader, handler, true);%0D%0A            Node html = handler.getNode();%0D%0A            Node head = html.getNode(%22head%22);%0D%0A            List%3CNode%3E links = head.getNodes(%22link%22);%0D%0A            for(Node n : links)%0D%0A            %7B%0D%0A                String rel = n.getAttribute(%22rel%22);%0D%0A                if(rel.equals(%22openid.server%22))%0D%0A                %7B%0D%0A                    String openIdServer = n.getAttribute(%22href%22);                    %0D%0A                    assertTrue(openIdServer!=null);%0D%0A                    System.err.println(openIdServer);%0D%0A                    break;%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D%0D%0A        finally%0D%0A        %7B%0D%0A            reader.close();%0D%0A            con.disconnect();%0D%0A        %7D   %0D%0A    %7D%0D%0A    %0D%0A
     

remove duplicate test (already in openid module)