/*
 * 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.netbeans.modules.java.source;

import java.io.IOException;
import java.net.URL;
import org.netbeans.junit.NbTestCase;
import org.netbeans.modules.java.source.indexing.TransactionContext;
import org.netbeans.modules.java.source.parsing.FileManagerTransaction;
import org.netbeans.modules.java.source.parsing.ProcessorGenerated;
import org.netbeans.modules.java.source.usages.ClassIndexEventsTransaction;
import org.netbeans.modules.java.source.usages.ClassIndexImpl.State;
import org.netbeans.modules.java.source.usages.ClassIndexManager;
import org.netbeans.modules.java.source.usages.PersistentClassIndex;
import org.netbeans.modules.parsing.api.ParsingTestBase;

/**
 *
 * @author sdedic
 */
public class ClassIndexTestCase extends NbTestCase {
    public ClassIndexTestCase(String name) {
        super(name);
    }
    
    /**
     * Ensures that a CP root becomes valid, by creating ClassIndex for it; if TransactionContext
     * does not exist, creates a local one + commits it.
     * 
     * @param url
     * @throws IOException 
     */
    protected void ensureRootValid(URL url) throws IOException {
        TransactionContext testTx = beginTx();
        
        PersistentClassIndex pi = (PersistentClassIndex)ClassIndexManager.getDefault().createUsagesQuery(url, true);
        pi.setState(State.INITIALIZED);
        
        TransactionContext.get().commit();

        if (testTx == null) {
            beginTx();
        }
    }
    
    protected void createClassIndex(URL url) throws Exception {
        ClassIndexManager.getDefault().createUsagesQuery(url, true);
    }
    
    public static TransactionContext beginTx() throws IOException {
        try {
            return TransactionContext.beginTrans().
                    register(ClassIndexEventsTransaction.class, 
                             ClassIndexEventsTransaction.create(true, ()->true)).
                    register(FileManagerTransaction.class, FileManagerTransaction.writeThrough()).
                    register(ProcessorGenerated.class, ProcessorGenerated.nullWrite());
        } catch (IllegalStateException ise) {
            // ignore, tx may have been created by a previous test
            return null;
        }
    }

    public static void commitTx() throws IOException {
        try {
            TransactionContext.get().commit();
        } catch (IllegalStateException e) {
            // ignore silently
        }
    }
    
}
